Migration Rules
Why This Matters
Database migrations are high-risk changes. A bad migration can lock tables, corrupt data, or make rollback impossible. LLMs will generate migrations if asked β and without rules, theyβll generate naive ones that work in development but fail in production.
Migration rules constrain how schema changes are introduced, tested, and deployed.
What to Include
- Migration tooling β which tool generates and runs migrations
- What requires a migration β vs what can be changed without one
- Safety rules β whatβs allowed in a single migration, size limits, locking concerns
- Rollback expectations β must migrations be reversible?
- Review process β who approves schema changes
Example
## Migration Rules
Tool: Prisma Migrate (schema in prisma/schema.prisma)
Rules:
- Never modify or delete an existing migration file
- One logical change per migration (don't combine table creation with data backfill)
- Always test migrations against a copy of production data before deploying
- Adding columns: must be nullable or have a default (no locks on large tables)
- Removing columns: deprecate first (stop reading), then remove in a later migration
- Renaming: create new column, backfill, update code, drop old column (3 migrations)
LLM-specific:
- The model must NEVER generate or modify migration files directly
- The model can modify prisma/schema.prisma
- Human runs `prisma migrate dev` to generate the migration
- Human reviews the generated SQL before applying
Common Mistakes
Letting the model generate migrations directly. Migration files should be generated by the migration tool, not hand-written by an LLM. The model should modify the schema definition; the tool generates the SQL.
No rule about combining changes. A migration that creates a table, adds indexes, and backfills data is nearly impossible to debug if it fails. One change per migration.
Forgetting about production data volume. Adding a NOT NULL column without a default locks the table for the duration of the backfill. This works in dev (100 rows) but kills production (10M rows).
Tool-Specific Notes
- Claude Code: Include in CLAUDE.md LLM Guardrails β migration files should be on the protected list.
- All tools: This guideline works alongside guardrails. The guardrails say βdonβt touch migrations.β This guideline explains the correct process.