Agent skill
db-migration
Database schema changes and migration workflow — safe, reversible, tested
Install this agent skill to your Project
npx add-skill https://github.com/majiayu000/claude-skill-registry/tree/main/skills/other/other/data-andreibesleaga-gabbe-2
SKILL.md
DB Migration Skill
Goal
Implement database schema changes safely using migration-first development. All migrations must be reversible and tested before applying to production.
Steps
-
Inspect current schema
- Read current migration files to understand existing schema
- Check: any foreign key constraints that would be affected?
- Check: any indexes on columns being modified?
- Check: any application code relying on the current schema shape?
-
Design the schema change
- Minimal change to achieve the goal
- Consider backward compatibility:
- Adding nullable column: backward compatible ✓
- Adding NOT NULL column without default: breaking ✗
- Renaming column: breaking ✗ (use add → populate → rename → drop strategy)
- Changing column type: potentially breaking — check data conversion
-
Write the migration (forward + rollback)
Prisma (Node.js):
bashnpx prisma migrate dev --name add_email_verified_to_users # Edit: prisma/migrations/[timestamp]_add_email_verified_to_users/migration.sqlLaravel:
bashphp artisan make:migration add_email_verified_to_users_table # Edit: database/migrations/[timestamp]_add_email_verified_to_users_table.phpAlembic (Python):
bashuv run alembic revision --autogenerate -m "Add email_verified_to_users" # Edit: migrations/versions/[hash]_add_email_verified_to_users.pyRaw SQL migration file:
sql-- UP migration ALTER TABLE users ADD COLUMN email_verified_at TIMESTAMP NULL; CREATE INDEX idx_users_email_verified ON users(email_verified_at); -- DOWN migration (rollback) DROP INDEX IF EXISTS idx_users_email_verified; ALTER TABLE users DROP COLUMN IF EXISTS email_verified_at; -
Safe rename/drop strategy (never rename or drop directly)
Phase 1: Add new column (backward compatible) Phase 2: Populate new column in application code (dual-write) Phase 3: Deploy application using new column Phase 4: Remove old column (now safe — no code uses it) This allows zero-downtime deployments. -
Test the migration
bash# Apply migration to test database [migration command] --environment test # Verify schema is correct # Check that all application tests pass with new schema [test command] # Test rollback [rollback migration command] # Re-apply migration [migration command] --environment test -
Check for performance implications
- Is the new column indexed if it will be used in WHERE clauses?
- Does the migration lock the table? (ALTER TABLE on large tables can block)
- For large tables: consider pt-online-schema-change or gh-ost for zero-lock migrations
-
Update application code
- Update ORM models/entities to reflect new schema
- Update TypeScript types / PHP DTOs
- Update any seed data or fixture files
- Update API response schemas if schema change is exposed via API
-
Document the migration
- Update
docs/database-schema.md(if it exists) - If the migration has unusual behavior: add comment in migration file explaining why
- Update
Safety Checklist
Before applying to staging/production:
[ ] Migration tested on copy of production data (or realistic dataset)
[ ] Rollback tested and verified working
[ ] All application tests passing with new schema
[ ] Lock duration estimated for large tables
[ ] Backup confirmed before migration
[ ] Monitoring in place to detect errors after migration
Constraints
- NEVER drop a column or table without first verifying no application code uses it
- NEVER make a NOT NULL column without a DEFAULT or populating existing rows first
- ALWAYS write a rollback (DOWN) migration
- Production migrations require human approval
Output Format
Migration file + updated model/entity + test confirmation. Report: "Migration [name] created. Forward + rollback tested. All [N] tests passing."
Recommended Agent Skills
Expand your agent's capabilities with these related and highly-rated skills.
agent-ops-spec
Manage specification documents in .agent/specs/. Use when user provides requirements, acceptance criteria, or feature descriptions that need to be tracked and validated against implementation.
agent-ops-state
Maintain .agent state files. Use at session start, after meaningful steps, and before concluding: read/update constitution/memory/focus/issues/baseline consistently.
agent-ops-spec
Manage specification documents in .agent/specs/. Use when user provides requirements, acceptance criteria, or feature descriptions that need to be tracked and validated against implementation.
agent-ops-testing
Test strategy, execution, and coverage analysis. Use when designing tests, running test suites, or analyzing test results beyond baseline checks.
agent-ops-testing
Test strategy, execution, and coverage analysis. Use when designing tests, running test suites, or analyzing test results beyond baseline checks.
agent-ops-state
Maintain .agent state files. Use at session start, after meaningful steps, and before concluding: read/update constitution/memory/focus/issues/baseline consistently.
Didn't find tool you were looking for?