SQL migrations with AI, without wrecking data

SQL migrations are high-blast-radius changes. Agents can draft expand/contract steps, locking notes, and rollback checklists.

Published 2026-11-09 · Translation: VI

SQL migrations are high-blast-radius changes. Agents can draft expand/contract steps, locking notes, and rollback checklists. Humans must still own apply windows, drops, and backup thinking. The trap is letting a model one-shot destructive DDL because the statement “looks simple.”

Agents draft. Humans own data risk.

State the data goal and traffic assumptions up front: table size, hot paths, whether the app or the migration ships first. Related: Spec-driven agent workflow when the migration is part of a larger API change.

Expand → dual path → backfill → switch → contract

Expand

Expand

  1. Ask for an expand/contract plan, not a single destructive migration.
  2. Generate SQL with locking, index, and duration-class notes.
  3. Dry-run on a realistic staging dataset. Record timing. Adjust for online strategies if needed.
  4. Backfill with observability. Switch reads when safe.
  5. Document rollback: which steps reverse cleanly, which need restore-from-backup thinking.

Never give the agent unsupervised production apply rights for DDL.

Three traps that show up first

Failure modes to catch early

Failure modes to catch early

  • Same-release drop. Stop writing and drop together. Fix: contract in a later release after dual-read retirement.
  • Blind big rewrite. Untested UPDATE on a huge table. Fix: staging timing class + batching strategy before prod.
  • Hot index surprise. Blocking CREATE INDEX. Fix: online/concurrent strategy named in the plan.

A one-week practice loop

Take one pending schema change. Force an expand/contract outline before any SQL lands. Dry-run on staging with timing notes. If the agent proposes a drop in the expand PR, reject and split releases.

Bottom line

When this fits vs when it does not

When this fits vs when it does not

Agents are useful SQL draft partners. They are unsafe apply bots. Expand first, prove compatibility, backfill with eyes on, contract later. For agent thrash while drafting plans, use stop rules for agents.

Share

X Facebook LinkedIn
Educational content only. Rankings reflect stated criteria at the update date—not investment advice or permanent performance guarantees.