AI-Assisted Database Migration Script Cost: Postgres and MySQL Schema Changes
By Eric Bush · July 4, 2026 · 9 min read
Not All Migrations Cost the Same
A one-line ALTER TABLE users ADD COLUMN age INT NULL; is cheap for an LLM to generate. A migration that adds a NOT NULL column to a 40M-row table without blocking writes is a completely different exercise: it needs a backfill strategy, a coordinated double-write with the application code, a plan for rollback, and a plan for what to do when the migration is 60% done and a query timeout fires.
This post breaks down realistic LLM cost by migration shape, on Postgres and MySQL, and highlights where the model reliably fails.
Change-Shape Taxonomy
- Trivial: add nullable column, add non-unique index on small table, add table.
- Medium: add index on large table (needs CONCURRENTLY or online DDL), rename column with backward compatibility, add check constraint.
- Hard: add NOT NULL column to large table with default, change column type on hot path, denormalize a shape-heavy join.
- Very hard: split table, merge tables, change primary key, move column across tables with FK preservation.
Cost Per Migration by Shape
Cost assumes Sonnet 5 as the generator, one initial pass, one review pass, and one "reversibility check" pass. Numbers include context loading of the current schema (typically 2-4K tokens for a mid-sized app).
| Shape | Postgres cost | MySQL cost | Typical output |
|---|---|---|---|
| Trivial | $0.05-$0.10 | $0.05-$0.10 | 10-30 lines SQL |
| Medium | $0.15-$0.25 | $0.20-$0.35 | 40-80 lines + rollback |
| Hard | $0.40-$0.70 | $0.60-$1.00 | 100-200 lines + phased plan |
| Very hard | $0.80-$1.50 | $1.20-$2.20 | 200-400 lines + app coordination doc |
MySQL is consistently more expensive because online-DDL rules differ across storage engines and versions; the model has to generate more caveats and version-specific branches.
Why Hard Migrations Multiply Tokens
A hard migration is not one script — it is a sequence:
- Add new column, still nullable.
- Deploy app code that writes to both old and new location.
- Backfill old rows into the new column, in batches.
- Verify backfill completeness (count query, sample check).
- Add NOT NULL constraint (Postgres: NOT VALID + VALIDATE; MySQL: online ALTER).
- Deploy app code that reads only from the new location.
- Drop old location.
Each step needs its own SQL, plus coordination notes for the deploy pipeline. The LLM cost scales with the number of coordinated steps, not with SQL line count.
Where LLMs Silently Fail
Three recurring failure modes across Postgres and MySQL migrations:
- Locking assumptions. LLMs frequently forget that
ALTER TABLEin Postgres takes an ACCESS EXCLUSIVE lock unless you use CONCURRENTLY or version-16 non-blocking DDL. On a busy table this brings the app down. - Foreign-key gotchas. Adding a FK does not scan existing data in some Postgres versions unless you validate. Data integrity can be silently wrong.
- Backfill batching. Models default to
UPDATE ... WHERE id BETWEEN a AND bstyle batching, which is fine for sequential IDs but explodes on tables with UUID primary keys.
For every hard migration, run EXPLAIN on the generated queries before running them. It is the only way to catch the model's plan-vs-reality gaps.
Bottom Line
LLM cost for migration generation ranges from $0.05 for a trivial add-column to $2 for a table-split. That is negligible compared to the cost of a bad migration (production incident, data loss, hours of on-call time). Treat LLMs as fast draft writers for migrations, and always run the review passes plus EXPLAIN before executing.
Want to calculate exact costs for your project?
Frequently Asked Questions
How much does an AI-generated database migration cost?
Trivial migrations (add-column, small-table index) cost $0.05-$0.10 with Sonnet 5. Medium (large-table index, online rename) $0.15-$0.35. Hard (add NOT NULL to hot table, denormalization) $0.40-$1.00. Very hard (table split, PK change) $0.80-$2.20 including review passes.
Why does MySQL cost more than Postgres for AI-generated migrations?
MySQL online-DDL rules vary by storage engine and version, so the model has to generate more caveats and version-specific branches. Postgres has a cleaner CONCURRENTLY story, which shortens the output for common patterns.
What migration types silently fail with AI generation?
Locking assumptions (forgetting Postgres ACCESS EXCLUSIVE without CONCURRENTLY), foreign-key validation (adding a FK without a validate pass in some Postgres versions), and backfill batching that assumes sequential IDs but breaks on UUID primary keys.
Can I skip the review passes on database migrations?
No. Migration bugs are among the most expensive incident classes — data loss and downtime cost far more than the extra LLM tokens. Always run at least one review pass, and always run EXPLAIN on the generated queries before executing on production.
How does a hard migration scale in cost?
Hard migrations are not one script — they are a coordinated sequence: add nullable column, dual-write, backfill in batches, verify, add NOT NULL, switch reads, drop old column. LLM cost scales with the number of coordinated steps, not raw SQL line count. Expect 100-200 lines of output plus a coordination doc.
Related Articles
AI-Assisted Developer Onboarding Cost: First-Month API Spend for a New Hire
The AI coding bill for a new hire's first month is often 2-3x what a seasoned engineer costs. Codebase familiarization queries, code review learning curves, and rework loops explain most of the spread. Here is the real number.
AI-Assisted Git Merge Conflict Resolution Cost: 3-Way Merges with LLMs
Simple text conflicts cost pennies to resolve with an LLM. Structural conflicts across a big diff can cost $2-$5 each and still need human review. Here is the token math and where to draw the automation line.
AI-Assisted i18n Translation Cost: Token Math per 1,000 UI Strings (Claude, GPT, Gemini 2026)
Translating 1,000 UI strings with an LLM sounds cheap. Then variable placeholders, brand terms, and re-review overhead multiply the token count 4-6x. Here is what it actually costs across Claude, GPT, and Gemini.