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

Kiến trúc · ADR 0031

Migrations over directUrl, before the deploy rolls; demo seed fails closed

Trạng thái
accepted
Ngày
2026-09-29
Người quyết định
SmartLearn team (architecture review 2026-09, items #3, #5, #6, #17, #26)

Context

Runtime uses the Supabase transaction pooler (6543, PgBouncer), which cannot run prisma migrate; migrating meant a human swapping DATABASE_URL. Render's autoDeploy shipped code before anyone ran migrations, and the seed minted shared-password demo accounts whenever NODE_ENV !== 'production' — including an unset NODE_ENV against staging.

Decision

  • schema.prisma gets directUrl = env("DIRECT_URL") (session pooler 5432); the app stays on DATABASE_URL. db:* scripts run the CLI through scripts/prisma-cli.ts: unset DIRECT_URL falls back to DATABASE_URL unless that URL is pooled, which is refused; when both are set they must name the same database (Supabase project ref or host, plus db name; port and query ignored), so a stale localhost DIRECT_URL cannot silently absorb a staging migration. It prints the target host, never credentials. Compose sets it for migrate. This supersedes ADR-0009's bare prisma migrate deploy command.
  • Render autoDeploy: false (free plan: no preDeployCommand). .github/workflows/deploy.yml runs after CI passes on main: migrate deploy with secrets DATABASE_URL/DIRECT_URL, then POSTs RENDER_DEPLOY_HOOK_URL&ref=<sha>, pinned to the migrated commit. The roll is skipped only if a newer main commit already passed CI (its run owns the roll); otherwise this commit rolls, so a red newer commit cannot strand applied migrations. workflow_dispatch must find green CI for its SHA before migrating. Serialized, never cancelled mid-run.
  • Ordering contract: schema first, code second. Old code runs on the new schema until the roll finishes, so migrations are expand-then-contract.
  • Demo accounts only when NODE_ENV === 'development' exactly and no DB URL points at supabase.co/supabase.com (prisma/seed-guard.ts, tested). Admin seeding is unchanged. The seed imports the package's src/index barrel.

Alternatives considered

  • Migrate on container boot — every instance races the lock and a bad migration crash-loops the service.
  • Render preDeployCommand — paid plans only; revisit if the plan changes.

Consequences

Three GitHub secrets must exist before main deploys; a missing one fails the Deploy run loudly. A Render dashboard "Manual Deploy" bypasses the contract.