with one click
migrate
Plan and execute database schema migrations safely
Install with Codex or Claude Copy this prompt, paste it into Codex, Claude, or another assistant, and let it review the skill page and install it for you.
Menu
Plan and execute database schema migrations safely
Install with Codex or Claude Copy this prompt, paste it into Codex, Claude, or another assistant, and let it review the skill page and install it for you.
Based on SOC occupation classification
| name | migrate |
| description | Plan and execute database schema migrations safely |
| allowed-tools | Read, Write, Bash, Glob, Grep |
Structured workflow for designing schema changes, generating migration files (up/down), validating backward compatibility, planning deployment order, testing rollback, and creating runbooks for failed migrations.
Before designing any migration:
**/migrations/**, **/migrate/**, **/db/** for existing migration files to understand current schema state.claude/rules/data-patterns.md for database conventionsOutput: Current schema summary for affected tables with columns, types, constraints, indexes, and relationships.
For each change, classify it and determine the migration strategy:
| Change Type | Strategy | Risk Level |
|---|---|---|
| Add column (nullable) | Single migration, no deploy coordination | Low |
| Add column (NOT NULL) | Add nullable -> backfill -> set NOT NULL | Medium |
| Drop column | Stop reading -> deploy -> drop column | Medium |
| Rename column | Add new -> copy data -> update code -> drop old | High |
| Add index | CREATE INDEX CONCURRENTLY (PostgreSQL) | Low |
| Drop index | Verify no queries depend on it | Low |
| Add table | Single migration | Low |
| Drop table | Verify no foreign keys, no code references | High |
| Change column type | Add new column -> copy -> swap -> drop old | High |
| Add foreign key | Verify referential integrity first | Medium |
| Add constraint | Verify all existing data satisfies constraint | Medium |
Design rules:
CONCURRENTLY for index operations, batch updates for backfillsIF NOT EXISTS, IF EXISTS)Output: Schema change design document with change type, strategy, and SQL for each change.
Generate numbered migration files following the project's migration tooling:
migrations/
YYYYMMDDHHMMSS_description.up.sql
YYYYMMDDHHMMSS_description.down.sql
UP Migration Template:
-- Migration: {description}
-- Created: {timestamp}
-- Dependencies: {previous migration number, if any}
BEGIN;
-- {Change description}
{SQL statements}
-- Verify
{Verification queries -- SELECT count, check constraints}
COMMIT;
DOWN Migration Template:
-- Rollback: {description}
-- Reverses: {up migration number}
BEGIN;
-- {Reverse change description}
{Reverse SQL statements}
-- Verify rollback
{Verification queries}
COMMIT;
Rules:
For each migration, verify:
Compatibility Matrix:
| Migration | Old Code + New Schema | New Code + Old Schema | Reversible |
|-----------|----------------------|----------------------|------------|
| Add users.avatar_url (nullable) | OK | OK (ignores new column) | Yes |
| Drop users.legacy_field | FAIL (code reads it) | OK | Yes |
If backward compatibility fails, use the expand-contract pattern:
Document the deployment sequence for each migration:
## Deployment Runbook: {Migration Description}
### Pre-Deployment Checks
- [ ] Migration tested in staging environment
- [ ] Rollback tested in staging environment
- [ ] Backup verified (point-in-time recovery available)
- [ ] Estimated migration time: {X minutes}
- [ ] Lock impact: {None / Brief table lock / Extended lock}
- [ ] Maintenance window required: {Yes/No}
### Deployment Steps
1. Take database backup / verify point-in-time recovery
2. Run migration: `{migration command}`
3. Verify migration: `{verification query}`
4. Deploy new application code
5. Verify application health
6. Monitor for 15 minutes
### Rollback Steps (if anything fails)
1. Stop deployment
2. Run rollback: `{rollback command}`
3. Verify rollback: `{verification query}`
4. Redeploy previous application version
5. Verify application health
### Post-Deployment
- [ ] Monitor error rates for 1 hour
- [ ] Verify data integrity
- [ ] Update schema documentation
Before deploying, verify the rollback works:
If rollback testing reveals issues, fix the DOWN migration before proceeding.
Document what to do if the migration fails at each step:
## Failure Runbook: {Migration Description}
### Failure Scenarios
#### Migration fails mid-execution
- **Symptom**: Migration command returns error
- **Action**: Transaction will auto-rollback. Check error message, fix migration, retry.
- **Data impact**: None (transaction rolled back)
#### Migration succeeds but application errors
- **Symptom**: Increased 500 errors after deploy
- **Action**: Rollback application deploy first, then assess if migration rollback is needed
- **Data impact**: Check if new data was written to new columns/tables
#### Migration succeeds but performance degradation
- **Symptom**: Increased latency, high CPU on database
- **Action**: Check for missing indexes, long-running queries. May need to add indexes concurrently.
- **Data impact**: None (schema is correct, performance is the issue)
#### Rollback fails
- **Symptom**: DOWN migration returns error
- **Action**: Restore from backup (point-in-time recovery to pre-migration timestamp)
- **Data impact**: Data written after migration will be lost
- **Escalation**: Page database team
For multi-tenant systems:
$ARGUMENTS
Create Architecture Decision Record
Design API contracts with OpenAPI specifications
Assess architecture decisions tradeoffs and edge cases
Design database schemas with migrations and repository interfaces
Break requests into parallel tasks for team execution
Audit dependencies for vulnerabilities and plan upgrades