Sub topic 12
Database changes across promotions
This chapter assumes you know the pipeline from the earlier chapters: one immutable image is built, deployed to staging, then promoted to production. You should also know basic SQL and what a schema migration is.
Why the database breaks the promise
A container image is replaceable. If a release misbehaves, you deploy the previous image. A database is not replaceable. It holds live data, and a schema change applied by version 42 is still there when you roll back to version 41.
This creates two constraints on every promotion:
- During a rolling update, old and new versions of the app run at the same time against one schema.
- After a rollback, the old version runs against a schema that the newer version already changed.
So the schema must work with both the version being deployed and the version before it. Migrations that satisfy this are called backward-compatible.
Expand and contract
The standard technique is expand and contract, also called parallel change. You split one breaking change into several releases, each safe on its own.
Suppose you want to rename the column users.fullname to users.display_name. A direct RENAME COLUMN breaks version 41 the moment it runs. Instead:
- Expand. Add
display_nameas a nullable column. Version 41 ignores it. - Dual write. Release a version that writes to both columns and reads from the old one.
- Backfill. Copy existing values into the new column in batches.
- Switch reads. Release a version that reads
display_name. It still writes both columns, so a rollback to the previous version stays safe. - Contract. In a later release, once no running version uses
fullname, drop it.
Each step is an ordinary promotion through staging and production. The destructive step comes last, after you have confirmed that you no longer need to roll back past it.
-- Release 1: expand (safe for the old version)
ALTER TABLE users ADD COLUMN display_name text;
-- Release 3: backfill in batches, not one giant update
UPDATE users
SET display_name = fullname
WHERE display_name IS NULL
AND id BETWEEN 1 AND 10000;
-- Release 5: contract (only after nothing reads fullname)
ALTER TABLE users DROP COLUMN fullname;Where migrations run in the pipeline
The migration must be part of what you promote, not something rewritten per environment. Keep migration files in the same commit as the code, and package them in the same image. The deploy job then runs the same migration files in staging and in production.
#!/usr/bin/env bash
# migrate.sh <environment> <image>
set -euo pipefail
env="$1"
image="$2"
kubectl --namespace "$env" run "migrate-${RANDOM}" \
--image="$image" --restart=Never --rm -i \
--command -- ./run-migrationsRun this before deploy.sh, so the schema is expanded before the new code starts. Because the image is identical in both environments, staging proves that these exact migration files apply cleanly.
Common errors
- Dropping or renaming in one step. This breaks the old version during the rollout and makes rollback impossible.
- Adding a
NOT NULLcolumn with no default. Old code does not write it, so its inserts fail. - Long locks. Some
ALTER TABLEstatements block reads and writes on a large table. Staging often has little data, so it hides this problem. Test against production-sized data, or check your database's documentation for online schema change options. - Assuming a down migration is a rollback. Reversing a migration can delete data written since the release. Prefer rolling the app back and leaving the expanded schema in place.
- Editing an applied migration. Treat applied migration files as immutable, like the image. Fix mistakes with a new migration.
Rollback and roll-forward
With expand and contract, kubectl rollout undo from the earlier chapter stays safe. The previous image still finds the columns it expects. If a problem is in the schema itself, such as a bad index, roll forward with a new migration instead.
Exercise
Your team wants to change orders.total from an integer number of cents to a decimal column named total_amount. Outline the releases you would promote, and say which one you cannot roll back past.
Show answer
Release 1 adds the nullable total_amount column. Release 2 writes both columns and reads total. Then backfill existing rows in batches. Release 3 reads total_amount and still writes both. A later release drops total once no running version uses it. The drop is the one you cannot roll back past, because the data in total is deleted.
Exercise
Why does testing a migration only in staging not guarantee it is safe in production, even though both environments run the same image?
Show answer
The migration files are identical, but the data is not. Staging usually has far fewer rows, so a statement that takes a long lock or runs for minutes on a large production table can finish instantly in staging. Production-sized test data or a review of the statement's locking behavior is needed.