Level 10 / Project 08 - Zero Downtime Migration Lab¶
Home: README
Learn Your Way¶
| Read | Build | Watch | Test | Review | Visualize | Try |
|---|---|---|---|---|---|---|
| Concept | This project | — | Quiz | Flashcards | — | — |
Focus¶
- Expand-migrate-contract pattern for zero-downtime schema changes
- State machine for migration phase tracking
- In-memory table simulation with column operations
- Safety validation and rollback mechanisms
Why this project exists¶
Traditional "stop the world" migrations cause downtime. The expand-contract pattern adds new columns first, backfills data, then removes old columns. At every phase, both old and new code paths work — enabling zero-downtime rollout. This project simulates the entire lifecycle in Python.
Run (copy/paste)¶
Expected terminal output¶
Migration: Add display_name
Phase: COMPLETE
Progress: 100%
History (3 entries):
[EXPANDING] Add column 'display_name' ...
[MIGRATING] Backfill 'display_name' ...
[COMPLETE] Migration finished successfully
Alter it (required)¶
- Add a
build_split_table_migrationthat creates a new table, copies data, then drops the old one. - Add a
dry_runmode toMigrationExecutorthat logs steps without executing them. - Add a step-level progress callback so callers can monitor migration progress.
Break it (required)¶
- Try adding a duplicate column name — observe the
ValueError. - Create a contracting step without an expanding step and check the safety warning.
- Drop a non-existent column and see the error.
Fix it (required)¶
- Make
add_columnidempotent — skip silently if the column already exists. - Add validation that migration steps are in the correct phase order (EXPANDING before MIGRATING before CONTRACTING).
- Test the phase order validation.
Explain it (teach-back)¶
- Why must the EXPANDING phase come before MIGRATING? What breaks if you skip it?
- How does the contract phase differ from just dropping a column directly?
- Why is rollback essential in production migrations?
- How does this pattern apply to real databases using tools like Alembic or Django migrations?
Mastery check¶
You can move on when you can: - build a migration plan for renaming a column and trace each phase, - explain why both old and new code must work during the MIGRATING phase, - describe what happens if a migration fails mid-way and gets rolled back, - compare expand-contract to "big bang" migration and explain the tradeoffs.
Related Concepts¶
| ← Prev | Home | Next → |
|---|---|---|