Skip to content

feat(migrate): apply each migration once, recorded in a ledger, and refuse a changed or contract file on request - #201

Merged
aRustyDev merged 2 commits into
mainfrom
feat/migration-ledger
Nov 16, 2026
Merged

aRustyDev merged 2 commits into
mainfrom
feat/migration-ledger

Conversation

@aRustyDev

Copy link
Copy Markdown
Owner

Stage PR B of #195: one hostname serving production and several other targets, some of them on production's data (a
release candidate, an older release kept beside the new one). Until now migrate ran every file on every start. That is
safe for one version over its own database, and not for two: an older build running its files again would put an older
definition back over a newer one. So a database now records what it has.

What changes

  • Ledger. thrust_schema_migrations (file, sha256, applied_at). Each file is applied once, in one transaction with
    its row.
    • A database without the ledger (every database before this one) gets the baseline: every file once, as before
      (they are idempotent), all recorded, in one transaction.
    • A session advisory lock makes concurrent runs take turns.
  • ledger::plan (pure, crates/thrust-adapters-postgres/src/ledger.rs) compares the build's files with the ledger:
    • Apply the pending files, when the build has everything that is recorded;
    • Older: apply nothing and succeed, when the ledger has everything the build has and more. This is how an older
      target keeps running on shared data;
    • refused, with nothing applied: a recorded file whose checksum changed; files missing on both sides (another
      branch); or a pending file that sorts before the newest applied one (two branches took the same number).
  • Contract migrations are named NNNN_contract_<what>.sql.
    • migrate --no-contract refuses a run that has one to apply.
    • migrate --plan prints the plan as one line of JSON and writes nothing, not even the ledger table.
    • scripts/migrate-ci.sh passes either flag on and refuses anything else before the tunnel opens. Its own lines now go
      to stderr, so --plan's JSON is all of stdout.
  • reset leaves the ledger alone. Emptying it would make the next run apply every file again.
  • Lint. scripts/check-migration-lint.sh runs in the deps job, with its test in ci-scripts. A file numbered
    above 0017 that is not a contract migration may not DROP, RENAME, change a column's TYPE or SET NOT NULL. Comments
    and strings are ignored. DROP NOT NULL is refused too, which its test pins.

Tests (local)

Command Result
cargo nextest run -p thrust-adapters-postgres --features native 269 passed (161 in the new ledger binary)
cargo nextest run --profile ci --workspace 4120 passed, 4 skipped
cargo test -p thrust-adapters-selfhosted --test pg_adapter (Docker) 82 passed, 12 of them new
test-migrate-ci.sh, test-check-migration-lint.sh under dash 35/35, 214/214
every ci-scripts test under dash, the deps guards, cargo fmt --check, clippy (workspace and wasm32 Worker) clean

The hermetic tests check the plan against a reference model in two ways: exhaustively, over every possible state of a
four-file set (6,561 cases), and over generated larger sets.

Mutation testing

  • cargo mutants on src/ledger.rs: 17 caught, 1 unviable, 1 missed. The miss (< as <= in the out-of-order check)
    is equivalent: a pending file is never in the ledger, so the names compared are never equal.
  • Planted bugs, all killed:
    • 10 on the database side, for example no lock, no per-file transaction, a baseline not recorded, --plan writing
      the table;
    • 5 on migrate-ci.sh;
    • 18 on the lint.
  • The per-file transaction mutant first survived, because Postgres already rolls a failing multi-statement file back
    by itself. a_file_whose_row_cannot_be_written_leaves_nothing now kills it.
  • The binary's main/run/env I/O is reached only by the contract cells and the deploy. That gap is recorded as G-39
    in docs/src/testing/status.md.

Upgrade-path tests: about 13 expected a second migrate to re-run every file. They now go through
rerun_every_file, which drops the ledger and migrates, so they still prove every file can run again. No assertion was
weakened.

Not verified

  • The first baseline on a real staging or production database.
  • --plan and --no-contract through the real binary against a real database. The library calls are tested against
    Docker; the flags only through a fake.

Left for a later stage of #195

reset empties tables in the public schema only. Isolated targets with their own schema will need it to follow
search_path (the dev-* stage).

Refs #195

🤖 Generated with Claude Code

…efuse a changed or contract file on request

#195 puts more than one version of the code on the same data (a release candidate, an older release kept beside the new one, next to
production). Until now `migrate` ran every file on every start, which is safe for one version over its own database and not for two:
an older build running its files again would put an older definition back over a newer one. So a database now records what it has.

- `thrust_schema_migrations (file, sha256, applied_at)`: each file is applied once, in its own transaction together with its row. A
  database without the ledger (every database before this) gets the baseline: every file once, as before (they are idempotent), all
  recorded, in one transaction. A session advisory lock makes two runs at once take turns.
- `ledger::plan` (pure, src/ledger.rs) compares the build's files with the ledger: Apply(pending) when the build has everything
  recorded; Older (apply nothing, succeed) when the ledger has everything the build has and more; refused when a recorded file's
  checksum differs (Changed), when each side lacks a file of the other (Diverged), or when a pending file sorts before the newest applied
  one (OutOfOrder: two branches that took the same number). Contract migrations are `NNNN_contract_<what>.sql`.
- `migrate --no-contract` refuses a run with a contract file to apply and applies nothing; `migrate --plan` prints the plan as one line of
  JSON and writes nothing, not even the ledger table. `scripts/migrate-ci.sh` passes either flag on, refuses anything else before the
  tunnel opens, and now writes its own lines to stderr so that `--plan`'s JSON is the whole of stdout.
- `reset` leaves the ledger alone: it is not data, and emptied it would make the next run apply every file again.
- `scripts/check-migration-lint.sh` (deps job; its test in ci-scripts): a file above 0017 that is not a contract migration may not DROP,
  RENAME, change a column's TYPE or SET NOT NULL (comments and strings ignored).

Tests. Hermetic: the plan against a reference over every world of a four-file universe (6561) and over generated larger ones, what
applying a plan leaves, the `--plan` JSON, contract names, the list against the migrations directory, the bin's command line. Docker (12
in pg_adapter's `ledger` module): baseline, second run, a file once, a failing file, a file whose row cannot be written, a failed baseline,
edited file and edited checksum, older, diverged, out of order, contract refused and allowed, reset, two runs at once.

The pg_adapter upgrade-path tests that called `migrate` twice expected every file to run again; with the ledger the second call applies
nothing, so they now forget the ledger first (`rerun_every_file`) and keep proving what they proved: every file can run again over a
migrated schema, which the baseline does once to each database from before the ledger. The reset tests' row counts leave the ledger out
for the same reason `reset` does. No assertion was weakened.

Controls: `cargo mutants -f src/ledger.rs` 17 caught, 1 unviable, 1 missed that is equivalent (`<` as `<=` comparing a pending name with
the newest recorded one: they are never equal); 10 hand mutants of the database side, 5 of migrate-ci.sh's flags and 18 of the lint, each
failing its tests. Recorded as G-39 in docs/src/testing/status.md, with what is not covered (the binary's flags against a real database,
the first baseline on the real staging and production databases).

Refs: #195
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Conflict in docs/src/testing/status.md: main added G-39 (the Cloudflare email adapter) while this branch added G-39 (the
migration ledger). Main's rows are kept as they are; the ledger's row is renumbered G-40. Nothing else refers to it.

Refs: #195
Co-Authored-By: Claude Opus 5.5 <[email protected]>
aRustyDev added a commit that referenced this pull request Nov 16, 2026
…er Worker Previews and decided by Flagship (#207)

ADR-0111 records the owner's decisions of 2026-11-15 for #195, which are:
- one hostname, thrust.sow.foo;
- any number of live targets (prod, vX.Y.Z, the alias staging, dev-*);
- explicit opt-in, with identity from Access on the opt-in path only;
- every Flagship flag made by CI/CD;
- a reviewer before a shared-data target migrates.

It also records the design those decisions led to:
- production stays the one thrust Worker, behind a routing-only edge Worker;
- every other target is a Worker Preview of thrust, reached only through the edge;
- data profiles shared and isolated;
- the migration rules for shared data.

research/0016 holds the live spike of 2026-11-16 that settled what the documentation did not:
- a Preview's custom-domain URL refuses subrequests, so the edge uses workers.dev;
- Access can cover one Worker's Previews only;
- a script_name room binding from a Preview reaches production's rooms and code;
- Flagship works from workers-rs only through extern method imports;
- flag changes arrive in about 6 s;
- the edge must drop Access's CF_Authorization Set-Cookie and handle redirects itself;
- a Preview's version tag is empty.
Everything the spike made is deleted.

Dated updates, following this repository's amend-not-supersede convention:
- ADR-0110: where each kind of deploy lands;
- ADR-0092: the environment set; Q-0092.2's answer superseded;
- ADR-0090: an edge in front of the one Worker;
- ADR-0008: Flagship only at the edge; product flags stay a table;
- ADR-0042: the ledger of #201 and contract migrations by name;
- ADR-0061: shared targets use production's rooms.

New open questions:
- Q-0111.1: do Preview URLs open production's Version URLs?
- Q-0111.2: does thrust work behind the edge? /api/instances/ builds its origin from Host.

Refs: #195

Co-authored-by: Claude Opus 5.5 <[email protected]>
@aRustyDev
aRustyDev merged commit 11d4087 into main Nov 16, 2026
43 checks passed
@aRustyDev
aRustyDev deleted the feat/migration-ledger branch November 16, 2026 04:55
This was referenced Nov 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant