Bỏ qua, tới nội dung chính

Kiến trúc · ADR 0053

The API container applies pending migrations at boot

Trạng thái
accepted
Ngày
2026-11-22
Người quyết định
Tín

Context

ADR-0031 runs prisma migrate deploy in CI before it triggers the Render deploy, and the container never migrated on its own. On 2026-11-21 CircleCI ran out of credits (refill on 2026-12-07) and GitHub Actions was already out of minutes, so nothing could deploy: six merged features with five additive migrations sat undeployed. A one-off Render job could have run the migration, but Render's free plan refuses jobs. The session connection string lives only in CI's secret store.

Decision

  • The API image ships the Prisma CLI (pinned to the repo's version, engines downloaded at build time) plus schema.prisma and the migrations folder, under /db/prisma.
  • infra/runtime/supervise.mjs runs prisma migrate deploy before it starts the API and the worker when MIGRATE_ON_BOOT is on (the image default; Compose turns it off).
  • The migration connection is DIRECT_URL when set, otherwise DATABASE_URL with the Supabase transaction-pooler port 6543 switched to the session-pooler port 5432 and the pooling parameters removed. Only the port and query parameters change; the script never logs the URL.
  • If migration fails or times out (MIGRATE_TIMEOUT_MS, default 180 s), nothing starts and the container exits non-zero. The health check fails and Render keeps the previous deploy live, so new code never serves an old schema.
  • CI keeps running migrate deploy first. Both paths are idempotent and migrate deploy takes Prisma's advisory lock, so they cannot apply the same migration twice.

Consequences

  • A deploy can be triggered from the Render deploy hook or API alone, without CI.
  • Boot is a few seconds slower when nothing is pending; the health-check start period is now 90 s to cover a migration.
  • Migrations still have to be additive and safe to run while the previous version serves traffic, as ADR-0031 already requires.