Change a live schema safely
Separate schema expansion, backfill, and contract cleanup.
Design for overlapping versions
During a deployment, old and new application instances may run together. A migration that renames or drops a column immediately can break the old instances. Add the new nullable field first, deploy code that understands both representations, and backfill in bounded batches before tightening constraints.
Make backfills resumable
Choose a stable cursor and record progress. Update only rows still needing work, and make a repeated batch safe. Large transactions hold locks and can amplify replication lag, so set a batch size and pause conditions based on observed load. Validate counts and sampled values before switching readers.
while ($batch = User::whereNull('normalized_name')->orderBy('id')->limit(500)->get()) {
if ($batch->isEmpty()) break;
// Update idempotently and record the last processed ID.
}
Contract after evidence
Once every writer uses the new field and old code is gone, add the required constraint and remove the old field in a later release. Keep a rollback plan for the application version and a forward-fix plan for data; restoring a database backup is rarely a practical first rollback for a live service.
Check your understanding
Why should a column drop usually be a later release?
Show explanation
Old and new app versions can overlap during rollout. Keeping the old column until readers migrate prevents a compatibility break.
You can read for free. Create an account to save your progress.
← Previous lesson: Evolve an API without breaking clients Next lesson: Rehearse failure and recovery →Try it at senior level
Plan an expand-backfill-contract migration for a new non-null column on a large table. Include batch size, dual-write period, and rollback.
Check your work
Old and new code can coexist, the backfill is restartable, and constraints are tightened only after validation.
Self-guided practice. This does not record assessed mastery.
Put this lesson into practice
A related activity in this module at mid level.
Try: Diagnose and remove an N+1 queryFurther reading
Primary references for this module. Check the current version when behavior or framework guidance may have changed.