Part 07 / Topic 6 of 6
Migrations
The schema will change after people depend on it — the question is whether it changes safely. The rules:
- Migrations are scripts, versioned in git, applied in order — never hand-edits to a live database. (Tools: Alembic for SQLAlchemy projects, or plain numbered SQL files.)
- Additive changes are safe: adding a nullable column or a new table does not break running readers. Ship these freely.
- Destructive changes happen in phases (expand–migrate–contract): add the new column, backfill it, switch readers and writers over, and only then — a release later — drop the old column. At no point does deployed code reference a column that does not exist.
- Test the migration against a copy of the customer's data first. Their data has the weird rows; yours does not.
Done here? The practice set for Part 7 — 4 exercises with solutions — is on the part page.