Database Schema Deployment
What you'll learn: How to deploy database schema changes without breaking your running application or causing downtime.
Why Schema Changes Are Risky
Unlike application code that can be swapped out instantly, databases hold state—real data that customers depend on. Dropping a column or renaming a table can instantly break your running application, even if you deploy carefully.
The Expand-Contract Pattern
Think of this like renovating a bridge while traffic keeps flowing. You don't destroy the old lane before building the new one.
Expand: Add new database structures alongside the old ones. Your schema temporarily supports both old and new versions.
Contract: Once all application code uses the new structures, remove the old ones.
Example Flow
- Expand: Add a new
email_addresscolumn while keeping the oldemailcolumn - Deploy: Update application code to write to both columns, read from the new one
- Migrate: Backfill data from
emailtoemail_address - Deploy: Update code to ignore the old
emailcolumn entirely - Contract: Drop the old
emailcolumn
Backward-Compatible Migrations
Each migration must work with both the current and previous application version:
- Add columns with default values or as nullable
- Never drop columns immediately—deprecate first
- Create new tables instead of modifying existing ones
- Use views to present data in both old and new formats
This ensures zero-downtime deployments where database changes roll out before, during, or after application code updates—without causing errors.
Key Takeaway: Deploy schema changes safely using the expand-contract pattern: add new structures first, migrate gradually, then remove old structures only after all code has migrated—ensuring backward compatibility at every step.