Skip to content

Commit

Permalink
🐋 Put MySQL DB in docker (#492)
Browse files Browse the repository at this point in the history
  • Loading branch information
asim-shrestha committed May 10, 2023
1 parent 8b16800 commit f0c137a
Show file tree
Hide file tree
Showing 9 changed files with 102 additions and 53 deletions.
7 changes: 6 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ NEXTAUTH_SECRET=changeme
NEXTAUTH_URL=http://localhost:3000

# Prisma
DATABASE_URL=file:./db.sqlite
DATABASE_USER=reworkd_platform
DATABASE_PASSWORD=reworkd_platform
DATABASE_HOST=db
DATABASE_PORT=3306
DATABASE_NAME=reworkd_platform
DATABASE_URL="mysql://${DATABASE_USER}:${DATABASE_PASSWORD}@${DATABASE_HOST}:${DATABASE_PORT}/${DATABASE_NAME}"

# External APIs:
OPENAI_API_KEY=changeme
3 changes: 3 additions & 0 deletions db/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
FROM mysql:8.0

ADD setup.sql /docker-entrypoint-initdb.d
11 changes: 11 additions & 0 deletions db/setup.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
-- Prisma requires DB creation privileges to create a shadow database (https://pris.ly/d/migrate-shadow)
-- This is not available to our user by default, so we must manually add this

-- Create the user
CREATE USER IF NOT EXISTS 'reworkd_platform'@'%' IDENTIFIED BY 'reworkd_platform';

-- Grant the necessary permissions
GRANT CREATE, ALTER, DROP, INSERT, UPDATE, DELETE, SELECT ON *.* TO 'reworkd_platform'@'%';

-- Apply the changes
FLUSH PRIVILEGES;
34 changes: 26 additions & 8 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ services:
next:
container_name: next
build:
context: .
context: ./next
dockerfile: Dockerfile
ports:
- "3000:3000"
Expand All @@ -21,13 +21,31 @@ services:
- "8000:8000"
restart: always
env_file:
- .env
- next/.env
environment:
REWORKD_PLATFORM_HOST: 0.0.0.0
REWORKD_PLATFORM_DB_HOST: reworkd_platform-db
REWORKD_PLATFORM_DB_PORT: 3306
REWORKD_PLATFORM_DB_USER: reworkd_platform
REWORKD_PLATFORM_DB_PASS: reworkd_platform
REWORKD_PLATFORM_DB_BASE: reworkd_platform
REWORKD_PLATFORM_DB_CA_PATH: /etc/ssl/certs/ca-certificates.crt
REWORKD_PLATFORM_DB_HOST: db
REWORKD_PLATFORM_DB_PORT: "3306"
REWORKD_PLATFORM_DB_USER: "reworkd_platform"
REWORKD_PLATFORM_DB_PASS: "reworkd_platform"
REWORKD_PLATFORM_DB_BASE: "reworkd_platform"
depends_on:
- db

db:
image: mysql:8.0
container_name: db
restart: always
build:
context: ./db
environment:
MYSQL_DATABASE: "reworkd_platform"
MYSQL_USER: "reworkd_platform"
MYSQL_PASSWORD: "reworkd_platform"
MYSQL_ROOT_PASSWORD: "reworkd_platform"
volumes:
- db_data:/var/lib/mysql
command: [ 'mysqld', '--character-set-server=utf8mb4', '--collation-server=utf8mb4_unicode_ci' ]

volumes:
db_data:
11 changes: 6 additions & 5 deletions next/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,25 @@ ARG NODE_ENV
ENV NODE_ENV=$NODE_ENV

# Set the working directory
WORKDIR /app
WORKDIR /next

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

# Install dependencies
RUN npm ci

# Copy the wait-for-db.sh script
COPY wait-for-db.sh /usr/local/bin/wait-for-db.sh
RUN chmod +x /usr/local/bin/wait-for-db.sh

# Copy the rest of the application code
COPY . .

# Build the Next.js app
RUN npm run build

# Expose the port the app will run on
EXPOSE 3000

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

# Start the application
CMD ["npm", "start"]
CMD ["npm", "run", "dev"]
16 changes: 8 additions & 8 deletions next/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
#!/bin/env sh

# copy .env file if not exists
[ ! -f .env ] && cp .env.example .env
[ ! -f .env ] && [ -f .env.example ] && cp .env.example .env
source .env

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

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

# Generate Prisma client
npx prisma generate

# run cmd
exec "$@"
56 changes: 25 additions & 31 deletions next/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ generator client {
}

datasource db {
provider = "sqlite"
provider = "mysql"
url = env("DATABASE_URL")
relationMode = "prisma"
}
Expand All @@ -15,12 +15,12 @@ model Account {
type String
provider String
providerAccountId String
refresh_token String?
access_token String?
refresh_token String? @db.Text
access_token String? @db.Text
expires_at Int?
token_type String?
scope String?
id_token String?
id_token String? @db.Text
session_state String?
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
Expand All @@ -45,14 +45,12 @@ model User {
emailVerified DateTime?
image String?
role String?
subscriptionId String?
customerId String?
createDate DateTime @default(now())
accounts Account[]
sessions Session[]
Agent Agent[]
subscriptionId String? @db.Text
customerId String? @db.Text
createDate DateTime @default(now())
accounts Account[]
sessions Session[]
Agent Agent[]
@@index([email])
}
Expand All @@ -66,33 +64,29 @@ model VerificationToken {
}

model Agent {
id String @id @default(cuid())
userId String
name String
goal String
id String @id @default(cuid())
userId String
name String @db.Text
goal String @db.Text
deleteDate DateTime?
createDate DateTime @default(now())
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
tasks AgentTask[]
createDate DateTime @default(now())
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
tasks AgentTask[]
@@index([userId, deleteDate, createDate])
}

model AgentTask {
id String @id @default(cuid())
agentId String
type String
status String?
value String
info String?
sort Int
id String @id @default(cuid())
agentId String
type String
status String?
value String @db.Text
info String? @db.Text
sort Int
deleteDate DateTime?
createDate DateTime @default(now())
agent Agent @relation(fields: [agentId], references: [id], onDelete: Cascade)
agent Agent @relation(fields: [agentId], references: [id], onDelete: Cascade)
@@index([agentId])
@@index([type])
Expand Down
11 changes: 11 additions & 0 deletions next/wait-for-db.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/sh

host="$1"
port="$2"

until echo "SELECT 1;" | nc "$host" "$port" > /dev/null 2>&1; do
>&2 echo "Database is unavailable - Sleeping..."
sleep 2
done

>&2 echo "Database is available! Continuing..."
6 changes: 6 additions & 0 deletions platform/reworkd_platform/db/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ def create_engine() -> AsyncEngine:
:return: SQLAlchemy engine instance.
"""
if settings.environment == "dev":
return create_async_engine(
str(settings.db_url),
echo=settings.db_echo,
)

ssl_context = ssl.create_default_context(cafile=settings.db_ca_path)
ssl_context.verify_mode = ssl.CERT_REQUIRED
connect_args = {"ssl": ssl_context}
Expand Down

1 comment on commit f0c137a

@vercel
Copy link

@vercel vercel bot commented on f0c137a May 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.