Investigation & Containment
Investigation
Four questions, in this order: which schema the database has, what the release changed, whether the previous release can run on it, and what users have written since. Every kubectl names the cluster with --context (Chapter 01).
Which schema does the database have? The migration tool keeps one row: the version of the last migration applied, and whether it failed half-way (
dirty):kubectl --context kind-sre-control-plane -n develop exec app-postgres-1 -c postgres -- \ psql -d app -tAc "SELECT version, dirty FROM schema_migrations"2|f- version 2, clean. At(dirty) means a migration started and did not record that it finished - not that the schema is broken. Each migration file runs as one transaction, so a failed one usually changed nothing; only a comparison of the schema with the file tells. Nothing is deployed until a person has looked (see Workflow).What did the release change - and who applied it? On this platform the migrations are SQL files built into the backend’s image, and an initContainer applies them before the app starts. Its log says what it did:
kubectl --context kind-sre-control-plane -n develop logs deploy/backend -c migratemigrate: schema 1 -> 2when this pod’s start applied a migration;schema already at version 2 (this build needs 2)when an earlier pod did. The files themselves are in the backend repository,pkg/migrations/sql/, one per version - the diff of the release shows exactly which DDL it brought.Can the previous release run on this schema? This is the question that decides whether an image rollback is safe. Read the migration: does it remove, rename or tighten anything the previous code reads or writes? A new nullable column, a new table, a new index - the previous code never notices them. A dropped or renamed column, a new
NOT NULLwithout a default - the previous code breaks, as in the story. On this platform the question is answered before the merge: CI runs the previous release’s binary on the new schema and logs in with it (see Workflow). After a rollback, the previous release’smigrateprints schema 2 is newer than this build knows (1) - nothing to apply - it only means nothing was applied, not that the code works on that schema. In the story it would have printed the same line, and every login still failed.What have users written since? Before anyone proposes a restore, count what it would take away:
kubectl --context kind-sre-control-plane -n develop exec app-postgres-1 -c postgres -- \ psql -d app -tAc "SELECT count(*) FROM app_users WHERE created_at > '<the release time, UTC>'"Every one of these rows disappears with a restore to before the release. In the story it was 214 accounts; nobody counted before the restore.
The root cause of the story is not the rollback and not the restore. It is a release that left no schema the previous code could run on: the rename and the code that needed it shipped together, so the way back was closed the moment the migration ran.
Containment
- Switch the new behaviour off first. If the release’s new code path is behind a feature flag, the flag goes off - no image change, no schema change.
- Roll back the image only when the previous release runs on the current schema (question 3). Then the rollback is the one line of Chapter 10, and every row written since stays.
- Otherwise roll forward. Fix the bug in a new release on top of the current one - the schema stays as it is. If the previous release has to run after all, the next migration gives it back what it needs without taking it from the current release - here,
usernameagain next tologin, kept in step with it - before the image goes back. In that pull request the N-1 job runs the previous release’smigrateand app on the new schema; the current release’s own tests run on the schema its migrations produce, and its binary is what applies the migration when it is deployed. Renamingloginback would only move the breakage to the current release. Every such fix goes through a pull request and the pipeline, like every release. Never ad hoc DDL typed into a productionpsqlto make the schema fit the code: it is untested, and it is not in Git, so the next environment never gets it. The one exception is finishing or undoing a migration left dirty - reviewed, in a transaction wherever the statements allow one (runbook). - Restore only for lost or corrupted data - and never over the live database. A point-in-time restore goes into a separate cluster (Chapter 12); the rows that were lost are copied back from there. It never replaces the database users are writing to, to undo a release.
A release that changes the schema must leave a schema the previous release runs on - then a rollback is only an image, and the users’ data stays.
Pause and Predict: A migration adds a column display_name that may be empty, and the new code writes it on every registration. The release is rolled back an hour later. What happens to the display names written in that hour - and what does the previous release do with the column?