Skip to content
Toolkit

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.yml re-ran every file on every deploy, without ON_ERROR_STOP. 25 of 44 files errored on each run, the errors were ignored, and each run of 000001 inserted another Free plan (launch audit P0-2, reproduced 1 → 2 → 3 rows). Data migrations (000031's grant, 000044's H1 rewrite) re-executed every time.
  • make db-migrate used ON_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.

  • up applies 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 N reverts the N newest applied migrations, newest first, each in a transaction with the removal of its row.
  • status lists 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, up stops with ErrUnledgeredSchema. baseline V records 1..V as applied without running them (marked baseline = 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 up before restarting the binary. The workflow and Makefile changes belong to infra and are handed to it (launch hand-off api-launch.md, Requests). Until they land, deploy-production.yml still 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 needs baseline too, until the hook is replaced by migrate up.
  • psql still works for one-off inspection, and a .down.sql is 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

Library