Database Migrations With AI Coding Agents: Three Rules Checked
By Matt Cullerton, founder of Mavric and Apso
A database migration is a versioned, ordered file that changes your schema after the app already has users and data. A TikTok from @haydenschmitty (Hayden Smith) gives three rules for people who let Claude, Codex or other coding agents touch their database: always use migrations, treat drops and column-type changes as dangerous, and test against production-like data. All three hold up against the PostgreSQL, Rails and Django docs. The video leaves out two things that decide whether a bad migration can be undone: down migrations and transactional DDL.
What is a database migration?
Hayden defines it as "a controlled way to change the shape or structure of your database after the app already exists." The Rails guide says "Migrations are a feature of Active Record that allows you to evolve your database schema over time." Supabase calls migrations "SQL statements that create, update, or delete your existing database schemas."
His definition skips versioning and ordering, which is what separates a migration from typing ALTER TABLE into a prompt. Supabase applies migration files "in timestamp order." Alembic chains each script to the previous one through a down_revision identifier.
He compares a migration to renovating a house with people living in it. PostgreSQL's locking docs make that literal. The ACCESS EXCLUSIVE lock "guarantees that the holder is the only transaction accessing the table in any way," and many forms of ALTER TABLE take it. A regular CREATE INDEX lets other transactions read the table but blocks inserts, updates and deletes until the build finishes.
One part of the analogy runs the other way. Adding "a new bathroom" is now the cheap case. When ADD COLUMN uses a non-volatile DEFAULT, PostgreSQL stores the value in metadata, "making the ALTER TABLE very fast even on large tables."
Rule 1: Should you let an AI agent modify the database directly?
No. Hayden says to always instruct "Claude, Codex, or whatever you use to use proper migrations." Supabase's deployment guide says the same: route every schema change through migration files, validate locally with supabase db reset, then run supabase db push.
An instruction is a request, though. Claude Code's permissions docs describe a control that does not depend on the model remembering: "Deny rules prevent Claude Code from using the specified tool," evaluated "deny, then ask, then allow." The same page warns that a Bash rule "isn't a security boundary around the program." Its example shows Bash(rm *) catching rm -rf build/ and missing /bin/rm -rf build/. The firmer boundary is a database role for the app with no DDL rights, which neither the video nor the docs discuss.
Rule 2: Which migrations are dangerous?
Hayden names drops, deletions and column-type changes. Each fails differently.
- Changing a column type usually rewrites the whole table. PostgreSQL says it "will normally cause the entire table and its indexes to be rewritten," under an exclusive lock.
- Dropping a table takes
ACCESS EXCLUSIVEand the data is gone. - Dropping a column is fast. PostgreSQL's
DROP COLUMN"does not physically remove the column, but simply makes it invisible to SQL operations." It returns instantly, which makes it easy for an agent to run and a reviewer to approve. The risk is that it cannot be reversed.
The tools do not hold this line for you. Prisma ORM 8 classifies these operations as destructive and says "it is only a warning: nothing stops and waits for your answer."
Rule 3: How should you test migrations?
Hayden says to test "with production-like data before actually applying them." That asks for more than the default workflow. Supabase's supabase db reset validates against a clean local schema. A column-type change that rewrites the table runs fine on an empty database and behaves very differently on a large one, so only realistic data volume shows the lock time.
Can you roll back a database migration?
Often not, and the video never mentions it. Support varies by tool:
- Rails reverses most
changemigrations automatically and supportsupanddownmethods. When a migration destroys data, the guide tells you to raiseActiveRecord::IrreversibleMigration. - Alembic requires
upgrade(), whiledowngrade()"is only needed if down-revision capability is desired." - Prisma has no native down migration. You generate a
down.sqlby hand withmigrate diff, apply it withdb executeand record it withmigrate resolve. - Drizzle Kit's
generatewrites forward SQL and a schema snapshot, with no rollback file.
On Prisma or Drizzle, a dropped column has no undo command.
Does your database support transactional DDL?
This decides whether a migration that fails halfway can be unwound, and it depends on the database. Django's docs say "On databases that support DDL transactions (SQLite and PostgreSQL), all migration operations will run inside a single transaction by default," while MySQL and Oracle "run without a transaction." On MySQL, "you will have to manually unpick the changes in order to try again (it's impossible to roll back to an earlier point)."
MySQL's manual confirms that DDL statements cause an implicit commit, and that a ROLLBACK does not undo CREATE TABLE in InnoDB.
PostgreSQL has exceptions. CREATE INDEX CONCURRENTLY, the non-blocking build you want on a live table, cannot run inside a transaction block. TRUNCATE and the table-rewriting forms of ALTER TABLE "are not MVCC-safe."
Bottom line
All three rules are sound. Back rule one with a deny rule or a restricted database role rather than a prompt alone. Before an agent drops or retypes anything, find out whether your tool can reverse it and whether your database wraps DDL in a transaction.
Sources
- PostgreSQL ALTER TABLE - table rewrites, lock level, DROP COLUMN and fast ADD COLUMN.
- Modifying Tables, PostgreSQL docs - why schema changes on filled tables need care.
- Explicit Locking - which commands take ACCESS EXCLUSIVE.
- CREATE INDEX reference - write blocking and the CONCURRENTLY limit.
- MVCC caveats - DDL that is not MVCC-safe.
- Rails Active Record Migrations - change, up, down and IrreversibleMigration.
- Django migrations - which databases support DDL transactions.
- MySQL implicit commit - DDL ends the active transaction.
- Prisma down migrations - the manual reversal steps.
- Prisma Migrate getting started - destructive operations get only a warning.
- drizzle-kit generate - forward-only migrations.
- Supabase database migrations - timestamp order and the db reset, db push workflow.
- Claude Code permissions - deny rules and their limits.
- Alembic tutorial - optional downgrade and revision ordering.
Published August 11, 2026. Based on a TikTok by @haydenschmitty. Full video notes (markdown)