Skip to content

Commit f0c137a

Browse files
🐋 Put MySQL DB in docker (#492)
1 parent 8b16800 commit f0c137a

File tree

9 files changed

+102
-53
lines changed

9 files changed

+102
-53
lines changed

.env.example

+6-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@ NEXTAUTH_SECRET=changeme
77
NEXTAUTH_URL=http://localhost:3000
88

99
# Prisma
10-
DATABASE_URL=file:./db.sqlite
10+
DATABASE_USER=reworkd_platform
11+
DATABASE_PASSWORD=reworkd_platform
12+
DATABASE_HOST=db
13+
DATABASE_PORT=3306
14+
DATABASE_NAME=reworkd_platform
15+
DATABASE_URL="mysql://${DATABASE_USER}:${DATABASE_PASSWORD}@${DATABASE_HOST}:${DATABASE_PORT}/${DATABASE_NAME}"
1116

1217
# External APIs:
1318
OPENAI_API_KEY=changeme

db/Dockerfile

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
FROM mysql:8.0
2+
3+
ADD setup.sql /docker-entrypoint-initdb.d

db/setup.sql

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
-- Prisma requires DB creation privileges to create a shadow database (https://pris.ly/d/migrate-shadow)
2+
-- This is not available to our user by default, so we must manually add this
3+
4+
-- Create the user
5+
CREATE USER IF NOT EXISTS 'reworkd_platform'@'%' IDENTIFIED BY 'reworkd_platform';
6+
7+
-- Grant the necessary permissions
8+
GRANT CREATE, ALTER, DROP, INSERT, UPDATE, DELETE, SELECT ON *.* TO 'reworkd_platform'@'%';
9+
10+
-- Apply the changes
11+
FLUSH PRIVILEGES;

docker-compose.yml

+26-8
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ services:
44
next:
55
container_name: next
66
build:
7-
context: .
7+
context: ./next
88
dockerfile: Dockerfile
99
ports:
1010
- "3000:3000"
@@ -21,13 +21,31 @@ services:
2121
- "8000:8000"
2222
restart: always
2323
env_file:
24-
- .env
24+
- next/.env
2525
environment:
2626
REWORKD_PLATFORM_HOST: 0.0.0.0
27-
REWORKD_PLATFORM_DB_HOST: reworkd_platform-db
28-
REWORKD_PLATFORM_DB_PORT: 3306
29-
REWORKD_PLATFORM_DB_USER: reworkd_platform
30-
REWORKD_PLATFORM_DB_PASS: reworkd_platform
31-
REWORKD_PLATFORM_DB_BASE: reworkd_platform
32-
REWORKD_PLATFORM_DB_CA_PATH: /etc/ssl/certs/ca-certificates.crt
27+
REWORKD_PLATFORM_DB_HOST: db
28+
REWORKD_PLATFORM_DB_PORT: "3306"
29+
REWORKD_PLATFORM_DB_USER: "reworkd_platform"
30+
REWORKD_PLATFORM_DB_PASS: "reworkd_platform"
31+
REWORKD_PLATFORM_DB_BASE: "reworkd_platform"
32+
depends_on:
33+
- db
3334

35+
db:
36+
image: mysql:8.0
37+
container_name: db
38+
restart: always
39+
build:
40+
context: ./db
41+
environment:
42+
MYSQL_DATABASE: "reworkd_platform"
43+
MYSQL_USER: "reworkd_platform"
44+
MYSQL_PASSWORD: "reworkd_platform"
45+
MYSQL_ROOT_PASSWORD: "reworkd_platform"
46+
volumes:
47+
- db_data:/var/lib/mysql
48+
command: [ 'mysqld', '--character-set-server=utf8mb4', '--collation-server=utf8mb4_unicode_ci' ]
49+
50+
volumes:
51+
db_data:

next/Dockerfile

+6-5
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,25 @@ ARG NODE_ENV
66
ENV NODE_ENV=$NODE_ENV
77

88
# Set the working directory
9-
WORKDIR /app
9+
WORKDIR /next
1010

1111
# Copy package.json and package-lock.json to the working directory
1212
COPY package*.json ./
1313

1414
# Install dependencies
1515
RUN npm ci
1616

17+
# Copy the wait-for-db.sh script
18+
COPY wait-for-db.sh /usr/local/bin/wait-for-db.sh
19+
RUN chmod +x /usr/local/bin/wait-for-db.sh
20+
1721
# Copy the rest of the application code
1822
COPY . .
1923

20-
# Build the Next.js app
21-
RUN npm run build
22-
2324
# Expose the port the app will run on
2425
EXPOSE 3000
2526

2627
ENTRYPOINT ["sh", "entrypoint.sh"]
2728

2829
# Start the application
29-
CMD ["npm", "start"]
30+
CMD ["npm", "run", "dev"]

next/entrypoint.sh

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
#!/bin/env sh
22

33
# copy .env file if not exists
4-
[ ! -f .env ] && cp .env.example .env
4+
[ ! -f .env ] && [ -f .env.example ] && cp .env.example .env
5+
source .env
56

6-
# change schema.prisma
7-
sed -ie 's/mysql/sqlite/g' prisma/schema.prisma
8-
sed -ie 's/@db.Text//' prisma/schema.prisma
7+
# Ensure DB is available before running Prisma commands
8+
./wait-for-db.sh db 3306
99

10-
# Add Prisma and generate Prisma client
11-
npx prisma generate
12-
# Generate db when not exists
13-
source .env
10+
# Run Prisma commands
1411
if [[ ! -f "/app/prisma/${DATABASE_URL:5}" ]]; then
1512
npx prisma migrate dev --name init
1613
npx prisma db push
1714
fi
1815

16+
# Generate Prisma client
17+
npx prisma generate
18+
1919
# run cmd
2020
exec "$@"

next/prisma/schema.prisma

+25-31
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ generator client {
33
}
44

55
datasource db {
6-
provider = "sqlite"
6+
provider = "mysql"
77
url = env("DATABASE_URL")
88
relationMode = "prisma"
99
}
@@ -15,12 +15,12 @@ model Account {
1515
type String
1616
provider String
1717
providerAccountId String
18-
refresh_token String?
19-
access_token String?
18+
refresh_token String? @db.Text
19+
access_token String? @db.Text
2020
expires_at Int?
2121
token_type String?
2222
scope String?
23-
id_token String?
23+
id_token String? @db.Text
2424
session_state String?
2525
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
2626
@@ -45,14 +45,12 @@ model User {
4545
emailVerified DateTime?
4646
image String?
4747
role String?
48-
subscriptionId String?
49-
customerId String?
50-
51-
createDate DateTime @default(now())
52-
53-
accounts Account[]
54-
sessions Session[]
55-
Agent Agent[]
48+
subscriptionId String? @db.Text
49+
customerId String? @db.Text
50+
createDate DateTime @default(now())
51+
accounts Account[]
52+
sessions Session[]
53+
Agent Agent[]
5654
5755
@@index([email])
5856
}
@@ -66,33 +64,29 @@ model VerificationToken {
6664
}
6765

6866
model Agent {
69-
id String @id @default(cuid())
70-
userId String
71-
name String
72-
goal String
73-
67+
id String @id @default(cuid())
68+
userId String
69+
name String @db.Text
70+
goal String @db.Text
7471
deleteDate DateTime?
75-
createDate DateTime @default(now())
76-
77-
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
78-
tasks AgentTask[]
72+
createDate DateTime @default(now())
73+
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
74+
tasks AgentTask[]
7975
8076
@@index([userId, deleteDate, createDate])
8177
}
8278

8379
model AgentTask {
84-
id String @id @default(cuid())
85-
agentId String
86-
type String
87-
status String?
88-
value String
89-
info String?
90-
sort Int
91-
80+
id String @id @default(cuid())
81+
agentId String
82+
type String
83+
status String?
84+
value String @db.Text
85+
info String? @db.Text
86+
sort Int
9287
deleteDate DateTime?
9388
createDate DateTime @default(now())
94-
95-
agent Agent @relation(fields: [agentId], references: [id], onDelete: Cascade)
89+
agent Agent @relation(fields: [agentId], references: [id], onDelete: Cascade)
9690
9791
@@index([agentId])
9892
@@index([type])

next/wait-for-db.sh

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/bin/sh
2+
3+
host="$1"
4+
port="$2"
5+
6+
until echo "SELECT 1;" | nc "$host" "$port" > /dev/null 2>&1; do
7+
>&2 echo "Database is unavailable - Sleeping..."
8+
sleep 2
9+
done
10+
11+
>&2 echo "Database is available! Continuing..."

platform/reworkd_platform/db/utils.py

+6
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ def create_engine() -> AsyncEngine:
1212
1313
:return: SQLAlchemy engine instance.
1414
"""
15+
if settings.environment == "dev":
16+
return create_async_engine(
17+
str(settings.db_url),
18+
echo=settings.db_echo,
19+
)
20+
1521
ssl_context = ssl.create_default_context(cafile=settings.db_ca_path)
1622
ssl_context.verify_mode = ssl.CERT_REQUIRED
1723
connect_args = {"ssl": ssl_context}

0 commit comments

Comments
 (0)