Skip to content

Commit 59a9fe1

Browse files
authored
Update web container to include migrations (LAION-AI#2234)
Follow up to LAION-AI#2233 * update website container to include migrations * remove outdated prisma container * update other containers for better layer caching. This PR contains no behavioural changes. More context: We started using `prisma db push` to sync our web db prisma schema with the db, with the intention of moving to migrations soon™, but we never did this because we never needed it. Now I wanted to add a new table to the database and realized that the infrastructure for something like this is not in place. I created an initial migration from the already available db scheme, to be able to deploy this without losing any data, we would need to force prisma to mark this migration as applied. The command is as follows: ``` npx prisma migrate resolve --applied 20230326131923_initial_migration ``` This PR prepare the web docker container to have migrations as a first class citizen.
1 parent 6e9e25e commit 59a9fe1

File tree

6 files changed

+28
-29
lines changed

6 files changed

+28
-29
lines changed

.dockerignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
**/node_modules
2+
website/.next

docker/Dockerfile.backend-worker

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ RUN mkdir /worker
66
WORKDIR /worker
77

88
COPY ./oasst-shared /oasst-shared
9-
RUN pip install -e /oasst-shared
9+
RUN --mount=type=cache,target=/root/.cache/pip pip install -e /oasst-shared
1010

1111
COPY ./backend/requirements_worker.txt /worker/requirements.txt
12-
RUN pip install --no-cache-dir --upgrade -r /worker/requirements.txt
12+
RUN --mount=type=cache,target=/root/.cache/pip pip install --upgrade -r /worker/requirements.txt
1313

1414
ENV PORT 8080
1515
EXPOSE 8080

docker/Dockerfile.prisma

Lines changed: 0 additions & 10 deletions
This file was deleted.

docker/Dockerfile.website

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ COPY --from=builder /app/public ./public
3131

3232
# Copy over the prisma schema so we can to `npx prisma db push` and ensure the
3333
# database exists on startup.
34-
COPY --chown=nextjs:nodejs ./website/prisma/schema.prisma ./
34+
COPY --chown=nextjs:nodejs ./website/prisma/ prisma/
3535
# Copy over a startup script that'll run `npx prisma db push` before starting
3636
# the webserver. This ensures the webserver can actually check user accounts.
3737
# This is a prisma variant of the postgres solution suggested in

docker/inference/Dockerfile.worker-full

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ ARG SERVICE="worker"
1010
ARG APP_RELATIVE_PATH="${MODULE}/${SERVICE}"
1111

1212
WORKDIR /worker
13-
COPY ./oasst-shared /tmp/oasst-shared
13+
1414
RUN conda create -n worker python=3.10 -y
15+
16+
COPY ./oasst-shared /tmp/oasst-shared
1517
RUN /opt/miniconda/envs/worker/bin/pip install /tmp/oasst-shared
1618

1719
COPY ./${APP_RELATIVE_PATH}/requirements.txt .

website/wait-for-postgres.sh

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,31 @@
11
#!/bin/sh
2-
# wait-for-postgres.sh
3-
#
4-
# This script tries to connect to a postgres database and then runs
5-
# `npx prisma db push`
6-
# It repeats until that setup completes (running multiple times will be a
7-
# noop). Then, it'll run the next command provided.
8-
#
9-
# You don't need to provide the connection URL. This reads it from the
10-
# DATABASE_URL environment variable which should be set.
11-
#
12-
# To run we suggest:
13-
# ./wait-for-postgres.sh npm run dev
2+
3+
# Wait for db & run migrations
4+
5+
# You don't need to provide the connection URL. This reads it from the DATABASE_URL environment variable
6+
# which should be set.
147

158
set -e
169

17-
until npx prisma db push --skip-generate; do
18-
>&2 echo "Postgres is unavailable - sleeping"
10+
# validate schema
11+
npx prisma validate
12+
13+
# wait until the db is available
14+
until echo 'SELECT version();' | npx prisma db execute --stdin; do
15+
echo >&2 "Postgres is unavailable - sleeping"
1916
sleep 1
2017
done
2118

22-
>&2 echo "Postgres is up - executing command"
19+
echo >&2 "Postgres is up - executing command"
20+
21+
# TODO: replace this command with applying migrations: npx prisma migrate deploy
22+
# NOTE: because of our previous setup where we just synced the database, we have to "simulate"
23+
# the initial migration with this command:
24+
# npx prisma migrate resolve --applied 20230326131923_initial_migration
25+
# prisma will fail with the above command if resolve is already applied,
26+
# we might need to run the command with set +e
27+
npx prisma db push --skip-generate
28+
2329
# Print and execute all other arguments starting with `$1`
2430
# So `exec "$1" "$2" "$3" ...`
2531
exec "$@"

0 commit comments

Comments
 (0)