← Back to Blog

How I Manage 39 Database Migrations With an AI Coding Agent

March 2026 · 5 min read

Database schema changes are the one area where people get genuinely nervous about AI-generated code. A bad migration can corrupt data, break foreign keys, or take down a production database. There's no "undo" for a migration that drops a column with live user data in it.

I've had an AI coding agent generate 39 migrations for a production application with zero data loss incidents. Not because the AI is perfect — it's not — but because the rules system makes certain categories of mistakes structurally impossible.

Why Databases Are Different

Most AI coding mistakes are annoying but fixable. Wrong naming convention? Find and replace. Bad import path? Quick correction. But database mistakes can be catastrophic and irreversible.

Without explicit rules, AI agents will do things that seem helpful but are dangerous in production: using ORM shortcuts that bypass your migration chain, generating migrations without downgrade functions, creating tables with naming patterns that conflict with your existing schema, or modifying JSONB columns without the specific ORM calls required to persist changes.

Every one of those has happened to me. Each one taught me a new rule to add to my database rules file.

What I Learned After 39 Migrations

Rules eliminate entire categories of mistakes. Before the database rules file, about one in three AI-generated migrations needed corrections. After, it's closer to one in ten — and those remaining issues are edge cases rather than pattern violations.

The review step is non-negotiable. Even with comprehensive rules, I never let the AI run a migration without human review. You let the AI do the tedious work of writing migration boilerplate, but you keep a human in the loop for the judgment call of "is this safe to run."

Decision records matter 10x more for databases. An AI agent can see your schema. It can't see the reasoning behind it. Without documented decisions, the agent might "helpfully" refactor your carefully-chosen schema design because it thinks a different pattern is better — not knowing you already evaluated and rejected that approach for specific reasons.

Naming conventions prevent 2am production bugs. Table names, column names, index names, foreign key patterns — the things that seem pedantic until you're debugging a failed migration on a live server and need predictable naming to find what went wrong.

The System Behind the Stats

39 migrations, zero data loss. That's not luck — it's a system. A specific set of rules, a review workflow, naming conventions, and documented decisions that work together to make the dangerous parts of AI-assisted development safe.

The same system works for any migration framework — Alembic, Django, Prisma, Knex, ActiveRecord. The principles are universal even if the specific rules vary by stack.

The Agent Playbook Pro guide includes the complete database rules template, the migration review workflow, naming convention system, and decision record format — plus the full case study. Get the guide.