AI wrote your migration: trusting database changes you didn't write

Agents write migrations fast, and database mistakes are the ones you can't roll back from coffee. What to verify before an AI-generated migration touches production data.

Published

Updated

Read time

5 min

Cover for 'AI wrote your migration': the headline AI wrote your migration, with chips for the five-point checklist, expand and contract, and rollback first

Of everything you can let an agent generate, database migrations deserve the most suspicion. Application code can be redeployed; bad data is forever. And agents write migrations with the same confidence they write CSS.

Why migrations are the danger zone

  • Irreversible by default. Most migration frameworks make down migrations optional. Agents skip them.
  • Lock behavior is invisible. ALTER TABLE on a million-row table locks writes. The agent doesn't know your table has a million rows.
  • Data migrations hide in schema migrations. Backfilling a new column in the same transaction as adding it is how you learn about lock timeouts at 2 AM.
  • No staging parity. The migration ran fine on the agent's empty local database. Production has data, and data has opinions.

The pre-merge checklist

1. Is it reversible?

Every migration needs a down path that's actually been thought through, not an auto-generated stub. Ask the agent explicitly: "write the rollback first." If the rollback is "restore from backup," that's an answer, but it should be a conscious one.

2. What's the lock impact?

For Postgres (and most databases), know these before approving:

  • Adding a column with a default → rewrites the table on older versions; use the multi-step pattern (add nullable → backfill → set default → set not null)
  • Creating an index → CONCURRENTLY, or it locks writes
  • Changing a column type → full rewrite; needs the expand/contract pattern

If the agent's migration does any of these in one step on a large table, send it back.

3. Data migrations are separate

Schema change and data backfill in one migration is the classic footgun. The rule: schema migrations change structure; data migrations change rows; never both in one deploy. Agents love combining them because it's "efficient." It's efficient the way skipping the pre-flight check is efficient.

4. Run it against realistic data

The agent tested on an empty database. You need:

  • A staging database with production-like volume (anonymized dump, or generated fixtures at scale)
  • EXPLAIN on any migration-time queries
  • Timing: if it takes 10 seconds on 10k rows, do the math for 10M

Two traps hide in that gap. If the agent tested against a local SQLite database and production is Postgres, it tested the syntax, not the behaviour: locking, type coercion, and defaults differ enough to hide exactly the failures this checklist exists to catch. And when you do get an anonymized production dump, query it before you migrate it. DuckDB runs analytical SQL directly over dump files, so "how many rows actually have a NULL here" becomes a ten-second question instead of a staging deploy.

The checklist catches a migration's shape. What catches its behaviour is rehearsing the migration: run it up, down, and up again against realistic data, and measure the locks before production volunteers to go first.

5. Check the framework's footguns

Every migration tool has them. For the common ones:

  • Django: RunPython without atomic = False on large tables; missing elidable operations
  • Prisma: a single prisma migrate deploy doing more than you reviewed: always prisma migrate diff first
  • Raw SQL: no transaction wrapping, no statement timeout

The pre-merge checklist for AI-generated migrations: is it reversible, what is the lock impact, data stays separate, test on realistic data, and know the framework footguns

Expand and contract, concretely

The pattern behind half the checklist is expand/contract (also called parallel change), and it's worth seeing once in full, because agents almost never produce it unprompted. Say you need to rename users.name to users.full_name on a large, busy table. The one-step migration (rename the column) breaks every running instance of the old code the moment it lands. The expand/contract version takes four deploys, each one safe:

  1. Expand. Add full_name as a nullable column. Deploy code that writes to both columns and still reads from name. Nothing depends on the new column yet, so nothing can break.
  2. Backfill. Copy name into full_name in batches, as a data migration, separate from any schema change, with progress you can watch and a rate that doesn't disturb production traffic.
  3. Switch reads. Deploy code that reads from full_name and still writes both. Verify in production that the values agree.
  4. Contract. Stop writing name, then drop it in a later deploy, once you're confident no rollback will need it.

Every step is reversible, every step deploys independently, and at no point does a running server meet a schema it doesn't understand. Adding a required column follows the same shape: add nullable, backfill, then add the constraint.

When you ask an agent for a migration on anything bigger than a toy table, ask for it in this shape explicitly: "give me the expand/contract sequence, one deploy per step, rollback for each." Agents know the pattern; they just default to the one-step version because it's shorter. Shorter is not the metric. Reversible is.

Two cautions about the pattern itself, so it doesn't become a new way to fail. Dual writes are application code, and they can drift: add a periodic consistency check (counts and checksums between the old and new columns) while both are live, and alert on disagreement instead of discovering it at contract time. And give the sequence an owner and a deadline. Expand/contract migrations that stall between steps leave you maintaining two columns forever, which is its own kind of incident. The pattern works because each step is small and finished, not because the steps exist.

The workflow that works

  1. Agent drafts the migration from your description
  2. You review against this checklist (5 minutes)
  3. CI runs it against a seeded staging-scale database
  4. Deploy schema and data changes separately
  5. Verify rollback on staging before production

Agents are genuinely good at the tedious parts of migrations, the boilerplate, the syntax, the framework idioms. Let them do that. Keep the judgment, reversibility, locks, data volume, for yourself. That's the part that protects the data.

Share this article

Newsletter

The NotVibesCoded Weekly

One email a week: the newest articles, project teardowns and AI-coding notes. No slop, no spam, unsubscribe anytime.

We will send a confirmation email. Click the link in it to finish subscribing.

Written by

NotVibesCoded Editorial Team

NotVibesCoded is written and reviewed by its editorial team under the standards in the editorial policy. A named author page, with full credentials, replaces this placeholder shortly.

More about the author

Related

Projects mentioned

AI Engineering

Claude Code logoClaude Code

Anthropic's agentic coding tool that works in your terminal.

Claude Code brought the agent loop to the command line: it reads your codebase, plans changes, runs commands, and iterates, with you directing, not typing. The reference implementation of terminal-native AI engineering.

TypeScript
Developer Tools

Aider logoAider

AI pair programming in your terminal, open source.

Aider is the open-source terminal chatbot for code: pair it with any frontier model, point it at your git repo, and it edits files with proper diffs and commit messages. The hacker's choice for agentic coding.

Python
Developer Tools

Zed logoZed

A next-generation code editor built for high-performance collaboration.

From the team behind Atom, Zed is a Rust editor that treats latency as a bug: GPU-rendered UI, multiplayer editing via CRDTs, and deep AI integration that doesn't get in the way.

Rust

Related

Related articles