ADR-0167 — A migration is applied once, and the database says which
Status: Accepted · Date: 2026-10-11 · Track: launch wave 1, api
workstream · Supersedes: the "apply them manually with psql" instruction in
docs/DEPLOYMENT.md · Keeps: the rule that the server binary never runs a
migration (platform/AGENTS.md)
Context
Nothing recorded which migrations a database had. The supported path was a psql
loop over every *.up.sql, which only works on an empty database. Each place
that needed "apply what is new" improvised:
deploy-production.ymlre-ran every file on every deploy, withoutON_ERROR_STOP. 25 of 44 files errored on each run, the errors were ignored, and each run of 000001 inserted anotherFreeplan (launch audit P0-2, reproduced 1 → 2 → 3 rows). Data migrations (000031's grant, 000044's H1 rewrite) re-executed every time.make db-migrateusedON_ERROR_STOP=1, so on any migrated database it stopped at 000001 and could never apply a new file (P1-9).- CI's loop had no
ON_ERROR_STOP, so a broken migration did not fail CI.
Decision
A separate command, apps/api/cmd/migrate, applies migrations and records
each one in schema_migrations. The logic is internal/migrate; the server
does not import it.
upapplies pending files in version order. Each runs in its own transaction with the insert of its ledger row, so a migration is applied and recorded, or neither. It stops at the first error and exits non-zero.- A file PostgreSQL refuses inside a transaction (
CONCURRENTLY, detected in the code with comments and literals ignored) runs statement by statement outside one, and is recorded after its last statement. 000018 is the only such file, and its statements are idempotent. down Nreverts the N newest applied migrations, newest first, each in a transaction with the removal of its row.statuslists every file as applied, baseline or pending, and marks an applied file whose SHA-256 changed since.- An unrecorded database is refused, not replayed. If the ledger is empty
and the schema already has tables,
upstops withErrUnledgeredSchema.baseline Vrecords 1..V as applied without running them (markedbaseline = true), and is refused once the ledger has rows. The runner does not guess V: the operator knows what was applied, and a wrong guess either replays a data migration or skips a schema change. - A pending file numbered below an applied one is refused (a branch merged out of order), and a PostgreSQL advisory lock stops two runners overlapping.
- The ledger lives in the connection's current schema, so the integration tests run each case in a schema of their own. One of them takes the repository's own 45 migrations up, down and up again through the runner.
Consequences
- Deploys run
migrate upbefore restarting the binary. The workflow and Makefile changes belong toinfraand are handed to it (launch hand-offapi-launch.md, Requests). Until they land,deploy-production.ymlstill runs the old loop. - Every existing database needs one
migrate baseline <version>. The Docker init hook (scripts/db-init.sh) still applies files with psql on first boot, so a fresh compose database needsbaselinetoo, until the hook is replaced bymigrate up. - psql still works for one-off inspection, and a
.down.sqlis still required for every migration. The runner uses both directions.
Source: docs/decisions/0167-a-migration-is-applied-once-and-the-database-says-which.md