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 <noreply@anthropic.com>
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
@@ -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, {
|
||||
|
||||
@@ -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
|
||||
}
|
||||
]
|
||||
}
|
||||
Reference in New Issue
Block a user