$npx -y skills add girijashankarj/cursor-handbook --skill migrationSafe workflow for creating and applying database schema changes. Use when the user needs to modify the database schema.
| 1 | # Skill: Create Database Migration |
| 2 | |
| 3 | ## Trigger |
| 4 | When the user needs to modify the database schema. |
| 5 | |
| 6 | ## Steps |
| 7 | |
| 8 | ### Step 1: Plan the Change |
| 9 | - [ ] Define what's changing and why |
| 10 | - [ ] Check for backward compatibility |
| 11 | - [ ] Identify affected queries and code |
| 12 | - [ ] Determine if data migration is needed |
| 13 | |
| 14 | ### Step 2: Create Migration File |
| 15 | - [ ] Name: `YYYYMMDDHHMMSS_descriptive_name.sql` |
| 16 | - [ ] Write UP migration (apply change) |
| 17 | - [ ] Write DOWN migration (rollback) |
| 18 | - [ ] Include required columns: `{{CONFIG.database.timestampFields.created}}`, `{{CONFIG.database.timestampFields.updated}}`, `{{CONFIG.database.softDeleteField}}` |
| 19 | |
| 20 | ### Step 3: Safety Checks |
| 21 | - [ ] No `DELETE FROM` statements (soft delete only) |
| 22 | - [ ] New columns have defaults or are nullable |
| 23 | - [ ] Indexes created with `CONCURRENTLY` for large tables |
| 24 | - [ ] No column renames (add new → migrate → drop old) |
| 25 | - [ ] Foreign keys have appropriate cascade rules |
| 26 | |
| 27 | ### Step 4: Data Migration |
| 28 | If needed: |
| 29 | - [ ] Write data migration script |
| 30 | - [ ] Use batch processing (1000-5000 rows per batch) |
| 31 | - [ ] Make it idempotent (safe to re-run) |
| 32 | - [ ] Include progress logging |
| 33 | |
| 34 | ### Step 5: Test Migration |
| 35 | - [ ] Apply to development database |
| 36 | - [ ] Verify data integrity |
| 37 | - [ ] Test rollback |
| 38 | - [ ] Test with production-like data volume |
| 39 | - [ ] Verify affected queries still work |
| 40 | |
| 41 | ### Step 6: Update Code |
| 42 | - [ ] Update models/types to match new schema |
| 43 | - [ ] Update affected queries |
| 44 | - [ ] Run type check: `{{CONFIG.testing.typeCheckCommand}}` |
| 45 | - [ ] Run affected tests |
| 46 | |
| 47 | ## If a step fails |
| 48 | |
| 49 | | Step | Failure | Recovery | |
| 50 | |------|---------|----------| |
| 51 | | Step 2 | Syntax error in migration | Fix SQL; re-run | |
| 52 | | Step 4 | Data migration fails mid-run | Script should be idempotent; fix and re-run; if partial data, document manual cleanup | |
| 53 | | Step 5 | Test migration fails | Run DOWN migration to revert; fix UP migration; retest | |
| 54 | | Step 5 | Rollback test fails | Fix DOWN migration; ensure DOWN fully reverts UP before retrying | |
| 55 | |
| 56 | Never apply migrations to production without testing UP and DOWN in a non-prod environment first. |
| 57 | |
| 58 | ## Completion |
| 59 | Migration created, tested, and code updated. Ready for review. |