Database Changes in CD
You'll learn: How to deploy database schema and data changes safely in a continuous delivery workflow without downtime or breaking production.
The Challenge
In continuous delivery, code changes flow frequently to production—but databases can't just be swapped out like application binaries. The database is stateful, shared, and often running while users are actively hitting your system. Schema migrations that drop columns or rename tables can instantly break running application code.
Backward-Compatible Changes: The Golden Rule
The solution is to make all database changes backward-compatible. This means both the old and new versions of your application can work with the database during the transition.
The Expand-Contract Pattern
Think of it like renovating a bridge while traffic flows across it:
- Expand: Add new structures (columns, tables) without removing old ones
- Deploy: Roll out application code that writes to both old and new structures
- Migrate: Backfill or sync data as needed
- Switch: Deploy new code that reads from the new structure
- Contract: Only after full rollout, remove the old structure
Example: Renaming a column
Instead of renaming email to email_address in one step:
- Add
email_addresscolumn (expand) - Deploy app code that writes to both columns
- Backfill existing
emaildata intoemail_address - Deploy app code that reads from
email_addressonly - Drop
emailcolumn (contract)
Each step can be deployed independently. If you need to rollback, the old code still works.
Why This Matters
Backward-compatible changes let you deploy database migrations before application code, safely and incrementally. No downtime. No big-bang releases. No "maintenance windows."
Key Takeaway: In CD, all database changes must be backward-compatible—use the expand-contract pattern to add first, migrate, then remove, ensuring old and new code coexist gracefully during deployment.