Process use when you need to work with database migrations. This skill provides schema migration management with comprehensive guidance and automation. Trigger with phrases like "create migration", "run migrations", or "manage schema versions".
85
83%
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Passed
No known issues
Create, validate, and execute database schema migrations with full rollback support across PostgreSQL, MySQL, and MongoDB.
psql or mysql CLI for executing and verifying migrationspg_dump --schema-onlyCapture the current schema state before making changes. Run pg_dump --schema-only -f schema_before.sql (PostgreSQL) or mysqldump --no-data > schema_before.sql (MySQL) to create a reference point.
Define the desired schema change clearly: specify table name, column additions/removals/modifications, constraint changes, and index updates. Document whether the change is additive (safe) or destructive (requires data migration).
Generate a versioned migration file following the framework's naming convention:
V20240115_001__add_status_column_to_orders.sqlalembic revision --autogenerate -m "add status column to orders"schema.prisma then npx prisma migrate dev --name add_status_to_ordersnpx knex migrate:make add_status_to_ordersWrite the UP migration (forward change) with these safety practices:
IF NOT EXISTS for CREATE operations to make migrations idempotentBEGIN; ... COMMIT;Write the DOWN migration (rollback) that exactly reverses the UP migration. For column additions, the DOWN drops the column. For table renames, the DOWN renames back. For data transformations, the DOWN must restore original data (store it in a backup column or table if needed).
Validate the migration on staging by running the full migration sequence:
For zero-downtime migrations on production, follow the expand-contract pattern:
Handle large table migrations (>10M rows) with online DDL tools: pg_repack for PostgreSQL, pt-online-schema-change for MySQL, or gh-ost for MySQL. These tools create a shadow table, replicate changes, then swap atomically.
Update the migration history table and verify the migration version matches expectations. Run flyway info or alembic current to confirm.
Document the migration in a changelog with: migration version, description, tables affected, estimated execution time, rollback procedure, and any required application deployments.
| Error | Cause | Solution |
|---|---|---|
| Migration fails with lock timeout | Long-running queries blocking DDL locks on the target table | Set lock_timeout = '5s' to fail fast; retry during low-traffic period; use pg_repack for lock-free operations |
| Column cannot be dropped because of dependent views | Views or materialized views reference the column being removed | Drop or recreate dependent views first; use CASCADE only after verifying all dependents are acceptable to drop |
| NOT NULL constraint violation during migration | Existing rows have NULL values in a column being made NOT NULL | Add a backfill step: UPDATE table SET column = default_value WHERE column IS NULL before adding NOT NULL |
| Migration version conflict | Two developers created migrations with the same version number | Use timestamp-based versioning; resolve by renaming one migration and re-running |
| Rollback fails because data was inserted after UP migration | New data in added columns has no place to go in the old schema | Design rollback to handle new data (backup column, log discarded data); accept that some rollbacks are data-lossy |
Adding a status enum column to an orders table: Generate a migration that: (1) creates the enum type CREATE TYPE order_status AS ENUM ('pending', 'shipped', 'delivered'), (2) adds column ALTER TABLE orders ADD COLUMN status order_status, (3) backfills UPDATE orders SET status = 'delivered' WHERE shipped_at IS NOT NULL, (4) sets NOT NULL ALTER TABLE orders ALTER COLUMN status SET NOT NULL. Rollback drops the column and enum type.
Splitting a monolith users table into users and profiles: Expand phase adds profiles table and triggers to sync data. Migrate phase copies existing profile data in batches of 10,000 rows. Contract phase drops profile columns from users table after application code is updated.
Renaming a column without downtime: Create a migration that adds the new column, adds a trigger to sync writes between old and new columns, backfills existing data, deploys application code using the new column name, then drops the old column and trigger in a follow-up migration.
3e83543
If you maintain this skill, you can claim it as your own. Once claimed, you can manage eval scenarios, bundle related skills, attach documentation or rules, and ensure cross-agent compatibility.