garage44.eu/jeroen/cartopolis

logclone: git clone https://garage44.eu/jeroen/cartopolis.git

.. /docs/notes/migration-numbers-are-claimed-by-a-deploy.md

raw


topic: migration-numbers-are-claimed-by-a-deploy triggers: [refinery, migrations, "V1", "add a migration", deploy-sim, "refinery migrate", "divergent", "MissingVersion", world.db, schema, crates/simulator/migrations, deploy-sim.yml, check-numbers.ts, crates/simulator/src/db.rs] updated: 2026-10-17

A migration number is claimed by a deploy, not by a merge

The next free Vnn__ is not the one after the highest file in crates/simulator/migrations. It is the one after the highest version in the production database, and those two numbers can differ — because deploy-sim.yml is a workflow_dispatch and can be dispatched from any ref, so a branch that is nowhere near main can migrate the live schema.

This took the public simulator down for about ten minutes on 2026-08-29, and the sequence is worth reading once because git gives no warning at any step:

  1. A branch adding V11__prim_particles and V12__prim_origin was deployed to production while unmerged. refinery applied both to world.db.
  2. A different branch, cut earlier and carrying its own V11__account_email and V12__house_claims, merged to main and deployed.
  3. refinery compared applied version 11 against the file on disk, found two different migrations wearing one number, and refused to start. Restart=always crash-looped the service and every client got a 502 from Caddy.

Both branches merged clean in git, because the filenames differ and neither touched the other's file. It is the same shape as the PROTOCOL_HISTORY collision (wire-protocol-versions.md), with one extra twist: the wire version collides at merge, and this one collides at deploy.

Renumbering afterwards does not fix it

The obvious repair — move the losing migration to V13/V14 — does not work, and the reason is worth knowing before trying it under pressure. refinery's Runner defaults to both abort_divergent: true and abort_missing: true (refinery-core/src/runner.rs), and verify_migrations walks the applied list first: an applied version with no file at that number is Kind::MissingVersion and a hard error. So renumbering only trades "applied migration V11 is different than filesystem one V11" for "V11 is missing from the filesystem". The service stays down.

What that leaves, once two migrations already share a number in a live database:

So, before adding a migration

Read the live history, not the directory listing:

ssh host 'sudo sqlite3 /data/cartopolis/sim/data/world.db \
  "select version, name from refinery_schema_history order by version desc limit 5;"'

A gap in the on-disk numbering is fine — refinery applies whatever it finds with a version above the current one, so a fresh database is happy with 1..10, 13, 14. Two files sharing a number never is.

What now catches it (2026-10-07)

Two guards, one at each end, because the clash can be made at either:

Neither stops somebody deploying an unmerged branch; they make the cost of having done so a refused landing or a refused deploy, not an outage.