From dc1850126b89edb13b440f61a8b4c4756a469d3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Kr=C3=BCger?= Date: Sun, 8 Mar 2026 11:00:36 +0100 Subject: [PATCH] fix: run DB migrations automatically at backend startup Instead of relying on a manual `pnpm db:migrate` step (which was connecting to a different postgres than the Docker container), the backend now calls drizzle migrate() before the server starts. This ensures migrations always run against the correct database on startup. Also fixes the Dockerfile to copy migrations into dist/migrations so the path resolves correctly in the compiled output. Co-Authored-By: Claude Sonnet 4.6 --- Dockerfile.backend | 2 +- packages/backend/src/index.ts | 7 +++++++ packages/backend/src/migrations/meta/_journal.json | 7 +++++++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/Dockerfile.backend b/Dockerfile.backend index 0bc3397..342bb31 100644 --- a/Dockerfile.backend +++ b/Dockerfile.backend @@ -55,7 +55,7 @@ COPY --from=builder --chown=node:node /app/package.json ./package.json COPY --from=builder --chown=node:node /app/packages/backend/dist ./packages/backend/dist COPY --from=builder --chown=node:node /app/packages/backend/node_modules ./packages/backend/node_modules COPY --from=builder --chown=node:node /app/packages/backend/package.json ./packages/backend/package.json -COPY --from=builder --chown=node:node /app/packages/backend/src/migrations ./packages/backend/migrations +COPY --from=builder --chown=node:node /app/packages/backend/src/migrations ./packages/backend/dist/migrations RUN mkdir -p /data/uploads && chown node:node /data/uploads diff --git a/packages/backend/src/index.ts b/packages/backend/src/index.ts index f2cab34..f2f7b69 100644 --- a/packages/backend/src/index.ts +++ b/packages/backend/src/index.ts @@ -15,12 +15,19 @@ import { buildContext } from "./graphql/context"; import { db } from "./db/connection"; import { redis } from "./lib/auth"; import { logger } from "./lib/logger"; +import { migrate } from "drizzle-orm/node-postgres/migrator"; const PORT = parseInt(process.env.PORT || "4000"); const UPLOAD_DIR = process.env.UPLOAD_DIR || "/data/uploads"; const CORS_ORIGIN = process.env.CORS_ORIGIN || "http://localhost:3000"; async function main() { + // Run pending DB migrations before starting the server + const migrationsFolder = path.join(__dirname, "migrations"); + logger.info(`Running migrations from ${migrationsFolder}`); + await migrate(db, { migrationsFolder }); + logger.info("Migrations complete"); + const fastify = Fastify({ loggerInstance: logger }); await fastify.register(fastifyCookie, { diff --git a/packages/backend/src/migrations/meta/_journal.json b/packages/backend/src/migrations/meta/_journal.json index c45056d..55d4744 100644 --- a/packages/backend/src/migrations/meta/_journal.json +++ b/packages/backend/src/migrations/meta/_journal.json @@ -22,6 +22,13 @@ "when": 1741337600000, "tag": "0002_remove_archived_recording_status", "breakpoints": true + }, + { + "idx": 3, + "version": "7", + "when": 1741420000000, + "tag": "0003_model_photo", + "breakpoints": true } ] } \ No newline at end of file