diff --git a/docker-compose.prod.yml b/docker-compose.prod.yml index 8a978fe..4d7b356 100644 --- a/docker-compose.prod.yml +++ b/docker-compose.prod.yml @@ -1,4 +1,19 @@ services: + # Run pending Payload migrations before the app starts. + # Uses the `migrator` image stage (has full src/) so TS files are discoverable. + migrator: + build: + context: . + target: migrator + restart: "no" + networks: + - internal + env_file: + - .env.production + depends_on: + db: + condition: service_healthy + app: build: context: . @@ -20,6 +35,8 @@ services: depends_on: db: condition: service_healthy + migrator: + condition: service_completed_successfully db: image: postgres:17-alpine diff --git a/src/instrumentation.ts b/src/instrumentation.ts index a986157..9348ea5 100644 --- a/src/instrumentation.ts +++ b/src/instrumentation.ts @@ -1,17 +1,11 @@ // Runs on Next.js startup (Node.js runtime only) before any requests are handled. -// Uses compiled/bundled code — no tsx/ESM resolution issues. +// Migrations are handled by the `migrator` Docker service (docker-compose.prod.yml) +// which runs before this container starts, so no migration call is needed here. export async function register() { if (process.env.NEXT_RUNTIME === 'nodejs') { const { getPayload } = await import('payload'); const { default: config } = await import('@payload-config'); - - const payload = await getPayload({ config }); - - // In dev mode Payload auto-pushes schema changes — migrations only needed in production - if (process.env.NODE_ENV === 'production') { - console.log('[startup] Running database migrations...'); - await payload.db.migrate(); - console.log('[startup] Migrations complete.'); - } + // Initialize Payload (connects to DB, registers collections/globals) + await getPayload({ config }); } }