How to Run an Expand-and-Contract Migration Safely
Expand-and-contract changes a database gradually, keeping old and new structures usable during a transition. First add the new structure, move application behavior and data, then remove the old structure last. It helps when application versions overlap or rollback must remain possible, but careless dual writes or backfills can create inconsistent data. AWS guidance recommends decoupling schema and code changes so old and new versions can coexist, and notes that reader/writer deployment order can differ between roll-forward and rollback. [AWS: Schema change practices](https://docs.aws.amazon.com/whitepapers/latest/blue-green-deployments/best-practices-for-managing-data-synchronization-and-schema-changes.html) [AWS DevOps Guidance](https://docs.aws.amazon.com/wellarchitected/latest/devops-guidance/dl.ads.5-ensure-backwards-compatibility-for-data-store-and-schema-changes.html)
How to Run an Expand-and-Contract Migration Safely
Expand-and-contract changes a database gradually, keeping old and new structures usable during a transition. First add the new structure, move application behavior and data, then remove the old structure last. It helps when application versions overlap or rollback must remain possible, but careless dual writes or backfills can create inconsistent data. AWS guidance recommends decoupling schema and code changes so old and new versions can coexist, and notes that reader/writer deployment order can differ between roll-forward and rollback. AWS: Schema change practices AWS DevOps Guidance
Why not rename the field in one step?
If the running application reads and writes users.full_name, renaming the column to display_name during deployment can break older instances on their next request. Old and new application versions may run at the same time, so both need a structure they can understand.
The expand phase adds a new column or table while retaining the old one. The middle phases update the application and data to use the new structure. Contract removes the old structure only after active code and recovery procedures no longer need it. Separating the phases preserves more options if a problem appears before cleanup.
Example: split full_name into two fields
This is a teaching example. Name-splitting rules vary by language and product; automatic splitting can be wrong. Assume the team defines an acceptable conversion rule for its own data.
| Phase | Schema and application state | What to check |
|---|---|---|
| Expand | Add nullable given_name and family_name; old app still uses full_name | New structure does not break old code |
| Compatible deploy | New app reads both forms and writes both according to an agreed rule | Source of truth and conflict handling across mixed versions |
| Backfill and verify | Convert existing rows | Idempotent reruns, unconverted rows, value differences |
| Cut over | New app reads the new fields; keep old values synchronized during rollback window | Latest values survive a rollback |
| Contract | Remove full_name in a separate release | No app, job, report, or external consumer still depends on it |
Choose nullability and defaults based on the data meaning. A generic rule such as splitting every name on a space may lose information even if the backfill runs successfully. If conversion is ambiguous, preserve the original and use review or an explicit mapping.
Plan a safe transition
1. Add the new structure first
Add the fields or table and confirm older code tolerates columns it does not know. Immediately enforcing a required value, uniqueness, or foreign key can reject old rows or writes from older application versions. Plan separately when to enforce constraints and how to populate existing data. Locking and transactional behavior for DDL vary by DBMS.
2. Deploy code that can read and write compatibly
If the new application must read both formats, one policy is to prefer the new value when present and fall back to the old value. This only works if the application can distinguish missing data from a legitimate empty value. Decide which version owns the source of truth for writes. During dual writes, one side can succeed while the other fails; define transaction, retry, reconciliation, and monitoring behavior.
AWS recommends planning for old and new versions to coexist and validating in a non-production environment. For serialized data formats, its guidance says deploy readers before writers when moving forward, then writers before readers when rolling back. That order is scoped to the described reader/writer compatibility scenario; it is not a universal rule for every database change. AWS DevOps Guidance
3. Backfill and validate existing rows
Backfill in chunks when appropriate, and make the operation safe to rerun without duplication or damage. While writes continue, an old-row copy can race with a fresh update. Decide which value wins, whether changing rows are re-read, and whether the write path keeps both representations synchronized. Afterward, check more than total row count: inspect missing new values, whether sample conversions preserve meaning, consistency between old and new fields, and constraints.
4. Switch reads, then stop old writes and remove the old structure
Once the new fields are complete and correct, make them the primary read source. Keep the old representation synchronized during the rollback window if older code needs it. Stop old-field writes only after old app instances, workers, scheduled jobs, reports, and external integrations are gone. Remove the old structure in a separate migration after an observation period. After removal, a simple rollback to old application code may no longer work.
What can roll back safely?
Before contract, keeping the old structure synchronized leaves more options to restore an earlier app version. But if information written only to the new structure cannot be translated back without loss, rollback can still lose recent data. After deleting the old column, reverting application code alone cannot restore it. Treat that cleanup as a separate change with explicit backup, restore, and acceptable-loss planning.
Before starting, write down stop conditions, mismatch detection, how to resume the backfill, the recovery owner, and the rollback window for each phase. AWS emphasizes pre-production validation, staged coexistence/rollout/rollback, and monitoring. AWS: Schema change practices This pattern does not guarantee zero downtime or a rollback. Safety depends on mixed-version behavior, backfill correctness, DBMS DDL behavior, release controls, and recovery procedures.