🗄️

Database Migration Script Writer

Generate safe, reversible database migration scripts

Intermediate🗄️ DatabaseAll Models
prompt template
Write a database migration for this change:

**Current schema:**
```sql
[PASTE_CURRENT_TABLE_DEFINITIONS]
```

**Desired change:** [DESCRIBE — e.g., "add a 'status' column to orders, add an index on user_id + created_at, rename 'name' to 'full_name'"]

Generate:
1. **Up migration** — SQL to apply the change
2. **Down migration** — SQL to reverse it completely
3. **Data migration** (if needed) — backfill existing rows
4. **Zero-downtime check** — will this lock the table? If so, how to avoid it

Safety checklist:
- [ ] Uses IF NOT EXISTS / IF EXISTS to be idempotent
- [ ] Handles NULL for new columns on existing rows
- [ ] Doesn't break existing queries (backward compatible)
- [ ] Includes a default value for new NOT NULL columns
- [ ] Estimates lock time for large tables

If the table has more than 1M rows, provide the migration in batches to avoid long locks.

Also generate a test query to verify the migration worked correctly.

How to Use This Prompt

  1. 1Copy the prompt template above
  2. 2Paste into Claude, ChatGPT, or Cursor
  3. 3Replace [bracketed placeholders] with your specific project details
  4. 4Iterate on the AI output to refine and customize the results