Skip to content

feat(deploy): the migration becomes a pre-upgrade hook that may only add - #468

Merged
athvin merged 1 commit into
mainfrom
bm/bluegreen-p10-migrate-hook
Nov 5, 2026
Merged

athvin merged 1 commit into
mainfrom
bm/bluegreen-p10-migrate-hook

Conversation

@athvin

@athvin athvin commented Nov 4, 2026

Copy link
Copy Markdown
Owner

Stacked on #461 (colour dimension) and carries #456's commit (migrate --plan), because the chart cannot call a flag that doesn't exist. Merge order is #456 → #461 → this; both parent commits drop out on rebase. Retarget this to main before merging #461 — a stacked PR whose base branch is deleted on merge gets marked MERGED by GitHub with a real merge commit while its code never lands (it cost #438 earlier in this stack).

Why it had to move

The init container on web-api was only safe because Recreate guaranteed the old pod was already gone before it ran. Blue/green gives that guarantee up on purpose — the point is that the old colour keeps serving — so the migration had to become one the old code survives.

This is the swap entity-framework-design.md rule 4 already named as its own condition:

"If replicas ever exceed one, the rule becomes expand-and-contract and the init container is replaced by a hook that may only add."

The colour dimension is that condition.

The objection is answered, not dismissed

job-db-migrate.yaml's header records why a pre-upgrade hook was rejected: it runs while the old pod still serves. That's true. The reason this one is safe is that er-server migrate --expect-additive plans first and refuses, writing nothing, if the plan holds anything the reconciler would not auto-apply.

So what it can execute is only CREATE TABLE for an absent relation and ADD COLUMN for an absent nullable one — both of which the live colour tolerates, because every statement names its columns (E011) and an older binary reads an ahead store correctly.

That refusal is also what the bare command cannot give: ensure_schema is six transactions, so by the time its phase-3 reconciler refuses, the baseline replay and the BeforeReconcile fixups have committed.

Weight −5, and the rendered order

After the ExternalSecrets at −10 that project the DSN it needs; before the drizzle hook at 0 — ordering the one that can refuse first, so a breaking control plane stops the release before anything touches erweb.

weight -10  pre-install,pre-upgrade    ExternalSecret/er-erserver
weight -10  pre-install,pre-upgrade    ExternalSecret/er-erweb
weight  -5  pre-install,pre-upgrade    Job/er-schema-migrate
weight   0  pre-install,pre-upgrade    Job/er-web-db-migrate
weight   0  post-install,post-upgrade  Job/er-tenant-migrate

Two choices worth reviewing

backoffLimit: 0, unlike the drizzle hook's 3. The expected failure here is a refusal — a deliberate state someone created by writing a breaking entity change — and retrying it three times triples the time to a clear error while changing nothing. A transient blip failing the release is the better trade, since --atomic rolls it back and re-running is one command.

Which image it runs: images.api, not a colour's pin. The colour blocks say what each Deployment runs; images.api means "the release this upgrade is about". Deriving it from a colour would be wrong in one of the two cutover phases — during the deploy phase color.active is still the old colour, and during the flip phase the inactive one is.

A probe found something worth fixing

I ran the hook's Job spec against the live kind cluster. It proved the wiring: image pulled, env and secret resolved, er-api service account, command dispatched, exit 0 on a current store.

But that cluster's image predates --expect-additive, and the old binary — which took no arguments at all — accepted --does-not-exist and exited 0 having done a normal migrate:

er-server: schema reconciled + DDL-ledger event triggers ensured
succeeded=1

A misspelled flag in a chart would silently turn this gate back into a plain apply, with the deploy looking identical. So the argument parsing moved into migrate_plan::parse_migrate_args where the refusal is testable — four cases, including that order must not matter, since a scan stopping at the first recognised flag would accept --plan --oops.

Being straight about what the probe did and didn't cover: it validated the Job wiring, not the new flag's behaviour. That seam is covered directly against a real Postgres by #456's own pg_migrate_plan.rs, which was observed failing with its key assertion inverted.

Docs revised in the same commit

Both claims became false. Rule 4 now describes the hook. And "What this does not do" no longer excludes blue-green across schema versions — what rules 5–7 still stand in for is the lake half, which is per-tenant and reconciled at each job's start rather than at the deploy boundary. Canary remains excluded: traffic is all on one colour or all on the other, never split by weight.

Verification

  • make check green — 16 migrate_plan unit tests.
  • helm lint clean; helm template + kubeconform -strict on the dev and kind profiles, both with zero init containers and the hook present.
  • Hook Job wiring probed on a real cluster, with the limitation stated above.

Note on CI: GitHub is still refusing runners for this account (steps_sum=0, repo-wide — see the ci-jobs-fail-to-start-quota memory). Every gate above was run locally.

🤖 Generated with Claude Code

@athvin
athvin force-pushed the bm/bluegreen-p9-colour branch from 40dc8de to 09b8d0a Compare November 4, 2026 23:52
@athvin
athvin force-pushed the bm/bluegreen-p10-migrate-hook branch from 93e8ec1 to cf7d3df Compare November 4, 2026 23:52
@athvin
athvin changed the base branch from bm/bluegreen-p9-colour to main November 5, 2026 00:15
@athvin
athvin force-pushed the bm/bluegreen-p10-migrate-hook branch from cf7d3df to f47798d Compare November 5, 2026 00:19
The init container on `web-api` was only safe because `Recreate` guaranteed the
old pod was already gone before it ran. Blue/green gives that guarantee up on
purpose — the point is that the old colour keeps serving — so the migration had
to become one the old code survives.

This is the swap `entity-framework-design.md` rule 4 already named as its own
condition: *"If replicas ever exceed one, the rule becomes expand-and-contract
and the init container is replaced by a hook that may only add."* The colour
dimension is that condition.

**The objection against a pre-upgrade hook is answered, not dismissed.**
`job-db-migrate.yaml`'s header records it: a pre-upgrade hook runs while the old
pod still serves. True, and the reason this one is safe is that
`er-server migrate --expect-additive` **plans first and refuses, writing
nothing**, if the plan holds anything the reconciler would not auto-apply. What
it can therefore execute is only `CREATE TABLE` for an absent relation and
`ADD COLUMN` for an absent nullable one — both of which the live colour
tolerates, because every statement names its columns (E011) and an older binary
reads an ahead store correctly.

That refusal is also what the bare command cannot give. `ensure_schema` is six
transactions: by the time its phase-3 reconciler refuses, the baseline replay
and the BeforeReconcile fixups have COMMITTED. Right for a boot, wrong for a
gate.

**Weight -5**: after the ExternalSecrets at -10 that project the DSN it needs,
before the drizzle hook at 0 — ordering the one that can REFUSE first, so a
breaking control plane stops the release before anything touches erweb.
Rendered order verified:

    weight -10  pre-install,pre-upgrade    ExternalSecret/er-erserver
    weight -10  pre-install,pre-upgrade    ExternalSecret/er-erweb
    weight  -5  pre-install,pre-upgrade    Job/er-schema-migrate
    weight   0  pre-install,pre-upgrade    Job/er-web-db-migrate
    weight   0  post-install,post-upgrade  Job/er-tenant-migrate

**`backoffLimit: 0`**, unlike the drizzle hook's 3. The expected failure here is
a refusal — a deliberate state someone created — and retrying it three times
triples the time to a clear error while changing nothing. A transient blip
failing the release is the better trade, because `--atomic` rolls it back and
re-running is one command.

**Which image it runs: `images.api`, not a colour's pin.** The colour blocks say
what each DEPLOYMENT runs; `images.api` means "the release this upgrade is
about". Deriving it from a colour would be wrong in one of the two cutover
phases — during the deploy phase `color.active` is still the OLD colour, and
during the flip phase the inactive one is.

**A probe found something worth fixing.** Running the hook's Job spec against
the live kind cluster proved the wiring — image pulled, env and secret resolved,
`er-api` service account, command dispatched, exit 0 on a current store. But its
image predates `--expect-additive`, and the OLD binary, which took no arguments
at all, **accepted `--does-not-exist` and exited 0 having done a normal
migrate**. Silent acceptance of a misspelled flag in a chart would turn this
gate back into a plain apply with the deploy looking identical. So the argument
parsing moved into `migrate_plan::parse_migrate_args` where the refusal is
testable, with four cases including that order must not matter — a scan
stopping at the first RECOGNISED flag would accept `--plan --oops`.

Rule 4 and the "What this does not do" paragraph are revised in the same commit,
because both became false: blue/green across schema versions is no longer
excluded, and what rules 5 to 7 still stand in for is the LAKE half, which is
per-tenant and reconciled at each job's start rather than at the deploy
boundary. Canary remains excluded — traffic is all on one colour or all on the
other, never split by weight.

Includes #456's commit, because the chart cannot call a flag that does not
exist; it drops out when that merges first.

Verified: `make check` green (16 `migrate_plan` unit tests); `helm lint` clean;
`helm template` + `kubeconform -strict` on the dev and kind profiles, both with
zero init containers and the hook present. The hook's Job wiring probed on a
real cluster as above — and the one seam that probe could NOT cover, the new
flag's behaviour, is covered directly against a real Postgres by #456's own
pg test.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@athvin
athvin force-pushed the bm/bluegreen-p10-migrate-hook branch from f47798d to 611a100 Compare November 5, 2026 11:47
@athvin
athvin merged commit 7eaa713 into main Nov 5, 2026
39 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