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.

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
downmigrations optional. Agents skip them. - Lock behavior is invisible.
ALTER TABLEon 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)
EXPLAINon 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:
RunPythonwithoutatomic = Falseon large tables; missingelidableoperations - Prisma: a single
prisma migrate deploydoing more than you reviewed: alwaysprisma migrate difffirst - Raw SQL: no transaction wrapping, no statement timeout

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:
- Expand. Add
full_nameas a nullable column. Deploy code that writes to both columns and still reads fromname. Nothing depends on the new column yet, so nothing can break. - Backfill. Copy
nameintofull_namein batches, as a data migration, separate from any schema change, with progress you can watch and a rate that doesn't disturb production traffic. - Switch reads. Deploy code that reads from
full_nameand still writes both. Verify in production that the values agree. - 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
- Agent drafts the migration from your description
- You review against this checklist (5 minutes)
- CI runs it against a seeded staging-scale database
- Deploy schema and data changes separately
- 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 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.
Related
Projects mentioned
Claude 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.
Related
Related articles
Amazon Pushed AI Coding Hard, Then Made Senior Humans Sign Off On It
Amazon mandated senior sign-off on AI-assisted production changes after outages linked to its own AI coding tools. What happened, what the mandate covers, and what it means for every team using AI to write code.
From API wrapper to observable agent: how companies actually build their own AI
Most company AI is ChatGPT behind an API call. The full ladder from wrapper to observable agent, explained rung by rung, and when to stop climbing.
Your agent invented 14 shades of blue: auditing AI-generated design tokens
AI coding agents love inventing tokens: bgPrimaryBlue, color-blue-1, primary_hover_2. Here's a 15-minute audit that catches naming chaos, missing semantics, and accessibility failures before they fossilize in your codebase.

