Repository navigation
feat(migrate): apply each migration once, recorded in a ledger, and refuse a changed or contract file on request - #201
Merged
Merged
Conversation
…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]>
aRustyDev
enabled auto-merge (squash)
November 16, 2026 04:25
aRustyDev
disabled auto-merge
November 16, 2026 04:27
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]>
This was referenced Nov 16, 2026
Closed
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
migrateran every file on every start. That issafe 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
thrust_schema_migrations (file, sha256, applied_at). Each file is applied once, in one transaction withits row.
(they are idempotent), all recorded, in one transaction.
ledger::plan(pure,crates/thrust-adapters-postgres/src/ledger.rs) compares the build's files with the ledger:target keeps running on shared data;
branch); or a pending file that sorts before the newest applied one (two branches took the same number).
NNNN_contract_<what>.sql.migrate --no-contractrefuses a run that has one to apply.migrate --planprints the plan as one line of JSON and writes nothing, not even the ledger table.scripts/migrate-ci.shpasses either flag on and refuses anything else before the tunnel opens. Its own lines now goto stderr, so
--plan's JSON is all of stdout.resetleaves the ledger alone. Emptying it would make the next run apply every file again.scripts/check-migration-lint.shruns in thedepsjob, with its test inci-scripts. A file numberedabove 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 NULLis refused too, which its test pins.Tests (local)
cargo nextest run -p thrust-adapters-postgres --features nativecargo nextest run --profile ci --workspacecargo test -p thrust-adapters-selfhosted --test pg_adapter(Docker)test-migrate-ci.sh,test-check-migration-lint.shunder dashdepsguards,cargo fmt --check, clippy (workspace and wasm32 Worker)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 mutantsonsrc/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.
--planwritingthe table;
migrate-ci.sh;by itself.
a_file_whose_row_cannot_be_written_leaves_nothingnow kills it.main/run/envI/O is reached only by the contract cells and the deploy. That gap is recorded as G-39in
docs/src/testing/status.md.Upgrade-path tests: about 13 expected a second
migrateto re-run every file. They now go throughrerun_every_file, which drops the ledger and migrates, so they still prove every file can run again. No assertion wasweakened.
Not verified
--planand--no-contractthrough the real binary against a real database. The library calls are tested againstDocker; the flags only through a fake.
Left for a later stage of #195
resetempties tables in thepublicschema only. Isolated targets with their own schema will need it to followsearch_path(thedev-*stage).Refs #195
🤖 Generated with Claude Code