Course contentsShow
← Software Engineering
Lesson 698 of 1,93322. Continuous Delivery and DeploymentFree lesson

Database Changes in CD

How to handle schema migrations and data changes in a continuous delivery workflow using backward-compatible changes.

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:

  1. Expand: Add new structures (columns, tables) without removing old ones
  2. Deploy: Roll out application code that writes to both old and new structures
  3. Migrate: Backfill or sync data as needed
  4. Switch: Deploy new code that reads from the new structure
  5. 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_address column (expand)
  • Deploy app code that writes to both columns
  • Backfill existing email data into email_address
  • Deploy app code that reads from email_address only
  • Drop email column (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.