Skip to content

feat(frontend): gate the drizzle migrations on being backward compatible - #451

Merged
athvin merged 1 commit into
mainfrom
bm/bluegreen-p6-drizzle-gate
Nov 5, 2026
Merged

athvin merged 1 commit into
mainfrom
bm/bluegreen-p6-drizzle-gate

Conversation

@athvin

@athvin athvin commented Nov 3, 2026

Copy link
Copy Markdown
Owner

Why this is not a blue/green feature

templates/job-db-migrate.yaml runs the drizzle migrator as a helm.sh/hook: pre-install,pre-upgrade Job. Pre-upgrade means the old pod is still serving — Helm applies the hook, waits for it, and only then touches the Deployment. So every migration in this tree has always been applied underneath live code, and nothing checked that it could be.

The neighbouring Rust half makes the asymmetry exact. job-tenant-migrate.yaml is post-upgrade and says why in its own header:

"the objection that rules out a pre-upgrade hook for the control plane (the old pod still serves) does not apply here."

The control plane took that objection seriously and moved its migration into the pod's init container. It also cannot execute a breaking change at all — its reconciler emits CREATE TABLE for an absent relation and ADD COLUMN for an absent nullable column, and refuses the rest as ERR_SCHEMA_BREAKING. Drizzle emits whatever drizzle-kit generated. This is the missing half of that asymmetry, expressed in the only place it can be: the committed SQL.

Two severities

Collapsing them produced a false positive on a migration that had already shipped, which is how the distinction got found.

Hard breaks — the old pod's SQL fails:

pattern why
DROP COLUMN its SELECT names that column
DROP TABLE / DROP SCHEMA likewise
RENAME a drop and an add at once, from the reader's side
ALTER … TYPE the driver decodes the old type
SET NOT NULL its INSERT omits the column
ADD COLUMN … NOT NULL with no DEFAULT its INSERT does not name the column

That last line is the interesting one: with a default it is allowed, because the default is what keeps the old pod's existing INSERTs working — exactly where er_entity_diff's Severity puts the boundary between Safe and Breaking.

Invariant swaps — DROP INDEX and DROP CONSTRAINT don't break the old pod at all; they remove a guarantee its code assumes. The honest rule isn't "never" but "replace it", so a swap is a finding only when the same migration adds nothing back.

0007_thick_cyclops.sql is the worked example already in the tree: it drops three indexes and a primary key and adds the dataset-scoped replacements for all four, so its window is one transaction. Treating that as a hard break would have demanded a contract marker for something correct — and a marker demanded where it isn't warranted is how an escape hatch becomes reflexive.

The escape hatch has to justify itself

Removing a column is legitimate in two releases: N stops reading it, N+1 drops it. A migration opts out with -- er-contract: followed by prose naming what stopped reading the column and when:

-- er-contract: release N+1 of the org_credentials rename; no reader has named
-- `legacy_nonce` since <commit>, which shipped in <release>.
ALTER TABLE "erweb"."org_credentials" DROP COLUMN "legacy_nonce";

A bare -- er-contract: is refused. An escape hatch nobody has to justify is just an absent gate.

Observed failing before being trusted

25 unit cases plus a live end-to-end run against a planted migration:

state result
hard break, no marker exit 1, naming the file and the statement
same break, bare -- er-contract: still refused
same break, justified marker passes, and prints the exemption and its reason so a reviewer sees what was waved through
empty migration directory exit 2 — refuses to report success on having examined nothing

Two of those cases caught a real bug in my first implementation: the reason reader walked past the comment block into the SQL statement, so any ALTER TABLE … padded a one-word reason over the length bar. The justification has to be prose a person wrote, not the statement it's excusing.

Comments and string literals are stripped before matching, because a comment explaining a past DROP COLUMN would otherwise be read as one — the exact mistake bff-envelope-contract.test.ts records making in its first draft, where a comment quoting a historical bug matched as the bug.

Verification

  • pnpm check green — 794 tests, line coverage 57.2% against a 48% floor.
  • Wired into pnpm check beside the authz and upstream gates, and exposed as pnpm migrations:check.

🤖 Generated with Claude Code

`templates/job-db-migrate.yaml` runs the drizzle migrator as a
`helm.sh/hook: pre-install,pre-upgrade` Job. **Pre-upgrade means the old pod is
still serving** — Helm applies the hook, waits for it, and only then touches the
Deployment. So every migration in this tree has always been applied underneath
live code, and nothing checked that it could be.

The neighbouring Rust half makes the asymmetry exact. `job-tenant-migrate.yaml`
is `post-upgrade` and says why in its own header: *"the objection that rules out
a pre-upgrade hook for the control plane (the old pod still serves) does not
apply here."* The control plane took that objection seriously and moved its
migration into the pod's init container. It also cannot execute a breaking
change at all — its reconciler emits `CREATE TABLE` for an absent relation and
`ADD COLUMN` for an absent nullable column, and refuses the rest as
`ERR_SCHEMA_BREAKING`. Drizzle emits whatever `drizzle-kit` generated.

This is the missing half of that asymmetry, expressed in the only place it can
be: the committed SQL. Not a blue/green feature — the hole is open today.

**Two severities, because collapsing them produced a false positive on a
migration that had already shipped, which is how the distinction got found.**

*Hard breaks* — the old pod's SQL fails: `DROP COLUMN`, `DROP TABLE`,
`DROP SCHEMA`, `RENAME`, `ALTER … TYPE`, `SET NOT NULL`, and
`ADD COLUMN … NOT NULL` with no `DEFAULT`. The last one is the interesting
line: WITH a default it is allowed, because the default is what keeps the old
pod's existing `INSERT`s working — exactly where `er_entity_diff`'s `Severity`
puts the boundary between `Safe` and `Breaking`.

*Invariant swaps* — `DROP INDEX` and `DROP CONSTRAINT` do not break the old pod
at all; they remove a guarantee its code assumes. The honest rule is not "never"
but "replace it", so a swap is a finding only when the same migration adds
nothing back. `0007_thick_cyclops.sql` is the worked example already in the
tree: it drops three indexes and a primary key and adds the dataset-scoped
replacements for all four, so its window is one transaction. Treating that as a
hard break would have demanded a contract marker for something correct — and a
marker demanded where it is not warranted is how an escape hatch becomes
reflexive.

**The escape hatch has to justify itself.** Removing a column is legitimate in
TWO releases: N stops reading it, N+1 drops it. A migration opts out with
`-- er-contract:` followed by prose naming what stopped reading the column and
when. A bare marker is refused, because an escape hatch nobody has to justify
is just an absent gate.

Observed failing before being trusted, in every direction — 25 unit cases plus
a live end-to-end run against a planted migration:

- a hard break with no marker -> exit 1, naming the file and the statement
- the same break with a BARE `-- er-contract:` -> still refused
- the same break with a justified marker -> passes, and PRINTS the exemption
  and its reason so a reviewer sees what was waved through
- an empty migration directory -> exit **2**, refusing to report success on
  having examined nothing

Two of those cases caught a real bug in the first implementation: the reason
reader walked past the comment block into the SQL statement, so any
`ALTER TABLE …` padded a one-word reason over the length bar. The justification
has to be prose a person wrote, not the statement it is excusing.

Comments and string literals are stripped before matching, because a comment
explaining a past `DROP COLUMN` would otherwise be read as one — the exact
mistake `bff-envelope-contract.test.ts` records making in its first draft,
where a comment quoting a historical bug matched as the bug.

Wired into `pnpm check` beside the authz and upstream gates, and exposed as
`pnpm migrations:check`.

Verified: `pnpm check` green — 794 tests, line coverage 57.2% against a 48%
floor.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@athvin
athvin force-pushed the bm/bluegreen-p6-drizzle-gate branch from c298b57 to 6701819 Compare November 3, 2026 05:51
@athvin
athvin merged commit a5e6100 into main Nov 5, 2026
38 checks passed
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.

2 participants