Skip to main content

Cloudsoft Zone Build, deploy and scale with practical guides to PHP, Node.js, React, DevOps and AI automation. Explore the guides

Cloudsoft Zone
DevOpsDevelopmentTutorial

Expand and contract database migrations: safe deployment with live writers

Use expand and contract migrations to keep old and new application versions compatible while backfilling retained data and verifying every writer before schema contraction.

Nilesh Kushvaha

5 min read

Sourced MySQL text artwork with an expand-and-contract migration sequence for live application writers.
Image: Cloudsoft Zone layout; Oracle Corporation; vectorized by VulcanSphere / Public domain (text logo); trademark retained
In this article

A release renames a column while old queue workers still read it. The new web application works, but background jobs fail immediately. Rolling back code does not automatically restore the removed schema or data.

Expand and contract migrations keep old and new versions compatible while the application changes its data contract. The difficult part is coordinating live writers and long-lived consumers, not just adding an extra migration file.

Our Laravel production checklist covers release gates. This guide uses a hypothetical MySQL-backed field transition and distinguishes Prisma ORM 7 guidance from newer version-specific tooling.

Schema expansion, consumer transition and final contraction form separate deployment phases.
An old queued payload can outlive the old web release.

Step 1: Inventory every schema consumer before expand and contract

List web instances, queue workers, scheduled tasks, reports, exports and operational scripts. Include queued payloads created before the rollout and any prior release that remains eligible for rollback.

For illustration, suppose an orders table moves from delivery_note to fulfillment_note with unchanged business meaning. If the meaning also changes, define the transformation explicitly rather than assuming a field copy is correct.

Consumer Compatibility evidence
Old web release Continues using the retained old field
Transitional release Coordinates both representations
Background workers Understand retained payload and schema versions
Reports and exports Use the intended field and format
Rollback artifact Remains compatible with the expanded schema

One successful new request does not prove all consumers are compatible.

Step 2: Expand without removing the old contract

Add a compatible new field through the existing reviewed migration owner. For an existing Laravel migration, an expansion fragment might be:

PHP
Schema::table('orders', function (Blueprint $table): void {
    $table->string('fulfillment_note', 255)->nullable();
});

The fragment assumes the existing Schema and Blueprint imports and a reviewed orders schema. It is not an instruction to run DDL on retained data without rehearsal.

Review the exact generated database operation. MySQL online DDL support depends on the operation and table conditions; online does not mean there are no metadata locks or resource costs.

Do not promise an instant operation merely because a framework migration looks short. Rehearse its lock behavior, disk headroom and failure path using a representative isolated copy.

Step 3: Establish the live-writer contract

Before backfilling, decide how every writer keeps old and new representations consistent. Transitional application code can write both atomically where that fits the data model, but old code still writing only the old field needs a deliberate compatibility plan.

Options include completing the compatible-writer rollout first, a reviewed database mechanism or a maintenance window for the relevant writes. Each has operating costs; choose explicitly rather than pretending a backfill compensates for unknown writers.

Preserve authorization and validation in the existing write owner. A migration helper should not become an unrestricted second application API.

A coordinated write contract supports bounded backfill batches and mismatch verification.
Do not overwrite a newer application value with a stale backfill read.

Step 4: Backfill in resumable bounded batches

Use a stable cursor, a defined initial high-water boundary and durable checkpoints. Choose a measured batch size and pause condition based on query latency, replication behavior and operating headroom.

Protect against concurrent changes. A read followed by an unconditional update can overwrite a newer application value. Use the reviewed transaction, row version or conditional-write contract appropriate to the schema.

Define how legitimate null values differ from unprocessed rows. A nullable field alone may not be a sufficient migration-progress marker.

Text
load migration checkpoint
select the next bounded primary-key range
apply the reviewed concurrency-safe transformation
commit data changes and checkpoint appropriately
observe impact before the next range
resume safely after interruption

This is a protocol outline. The application must implement its atomicity and conflict behavior; an informal loop does not prove safe backfill semantics.

Step 5: Verify data and switch reads deliberately

Check mismatches and business invariants before preferring the new field. For the hypothetical same-meaning fields, an authorized MySQL read can inspect a bounded ID range:

SQL
SELECT id
FROM orders
WHERE id > 1000 AND id <= 2000
  AND NOT (delivery_note <=> fulfillment_note)
ORDER BY id
LIMIT 20;

The null-safe comparison fits this specific copy contract. The IDs are examples, and the output limit is not a complete proof that every row is correct. Use reviewed ranges and a complete verification plan with known cost.

Switch reads only when the acceptance contract is met. Monitor outcomes and retain the old compatible representation through the intended reversal window.

Step 6: Retire old consumers before contraction

Confirm old web and worker processes are gone, retained queued payloads remain compatible and reports no longer depend on the old field. Include external consumers if the schema change affects an API or exported format.

Contraction gate Evidence required
Data correctness Backfill and invariant verification complete
Reader retirement No supported reader needs the old shape
Writer retirement No live writer updates only the old shape
Rollback boundary Intended prior release no longer requires it
Retention decision Removed data and recovery consequences reviewed

Do not remove the old column in the same casual step that introduces the new one. Contraction changes the recovery contract and needs its own reviewed migration.

Rollback options change between compatible expansion, read switching and destructive contraction.
Contraction requires a separately reviewed retained-data decision.

Step 7: Rehearse failure and phase-specific reversal

Exercise interrupted batches, writer conflicts and old queued payloads in isolated staging. Confirm the checkpoint resumes safely and the application does not present partially transformed data as complete.

Before contraction, code reversal may remain possible because both shapes survive. After destructive contraction, restoration or a forward repair can be required; a down migration cannot recreate lost business values automatically.

Our audit-log design guide explains accountable mutations. For safe schema transitions fitted to your application, discuss the project with Cloudsoft Zone.

Featured image: Cloudsoft Zone technical layout with mysql artwork by Oracle Corporation; vectorized by VulcanSphere (Public domain (text logo); trademark retained). Product mark resized proportionally for identification; no endorsement implied. Layout: CC BY-SA 4.0.

Frequently asked questions

  • Does adding a column guarantee zero downtime?

    No. The exact DDL operation, database version, table state and metadata locks affect behavior. Rehearse the operation and its waiting or interruption cost.

  • Can a backfill safely copy values while old code keeps writing?

    Only with a deliberate live-writer consistency contract and concurrency safeguards. Otherwise a backfill can miss later writes or overwrite newer values.

  • When is it safe to remove the old column?

    After all readers and writers, queued payloads, operational tools and the intended rollback path have retired it, with verification and a reviewed data-retention decision.

Sources

  1. Prisma7 expand-and-contract pattern — Prisma
  2. MySQL8.4 online DDL operations — MySQL
  3. Online DDL limitations — MySQL
  4. Metadata locking — MySQL
  5. Laravel reviewed migrations — Laravel

New tools, once a week

One short email listing what joined the directory.

The weekly email about new tools and occasional newsletters. Unsubscribe at any time.

Comments

No comments yet. Be the first to join the conversation.

Leave a comment

Every comment is read by a moderator before it appears. Your email address is never published; it is used only to contact you about this comment.

Never published. Used only to contact you about this comment.

Loading the security check…

Comments are moderated before publication.