universal/data/database-migration/SKILL.md
Safe patterns for evolving database schemas in production with decision trees and troubleshooting guidance.
npx skillsauth add bobmatnyc/claude-mpm-skills database-migrationInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
Safe patterns for evolving database schemas in production.
-- Add new column (nullable initially)
ALTER TABLE users ADD COLUMN full_name VARCHAR(255) NULL;
-- Deploy new code that writes to both old and new
UPDATE users SET full_name = CONCAT(first_name, ' ', last_name);
-- Backfill existing data
UPDATE users
SET full_name = CONCAT(first_name, ' ', last_name)
WHERE full_name IS NULL;
-- Make column required
ALTER TABLE users ALTER COLUMN full_name SET NOT NULL;
-- Remove old columns
ALTER TABLE users DROP COLUMN first_name;
ALTER TABLE users DROP COLUMN last_name;
-- Create index concurrently (PostgreSQL)
CREATE INDEX CONCURRENTLY idx_users_email ON users(email);
-- Phase 1: Add new column
ALTER TABLE users ADD COLUMN email_address VARCHAR(255);
-- Phase 2: Copy data
UPDATE users SET email_address = email;
-- Phase 3: Drop old column (after deploy)
ALTER TABLE users DROP COLUMN email;
-- Phase 1: Add new column with new type
ALTER TABLE products ADD COLUMN price_cents INTEGER;
-- Phase 2: Migrate data
UPDATE products SET price_cents = CAST(price * 100 AS INTEGER);
-- Phase 3: Drop old column
ALTER TABLE products DROP COLUMN price;
ALTER TABLE products RENAME COLUMN price_cents TO price;
-- Add column first
ALTER TABLE orders ADD COLUMN user_id INTEGER NULL;
-- Populate data
UPDATE orders SET user_id = (
SELECT id FROM users WHERE users.email = orders.user_email
);
-- Add foreign key
ALTER TABLE orders
ADD CONSTRAINT fk_orders_users
FOREIGN KEY (user_id) REFERENCES users(id);
# Generate migration
alembic revision --autogenerate -m "add user full_name"
# Apply migration
alembic upgrade head
# Rollback
alembic downgrade -1
// Create migration
knex migrate:make add_full_name
// Apply migrations
knex migrate:latest
// Rollback
knex migrate:rollback
# Generate migration
rails generate migration AddFullNameToUsers full_name:string
# Run migrations
rails db:migrate
# Rollback
rails db:rollback
def test_migration_forward_backward():
# Apply migration
apply_migration("add_full_name")
# Verify schema
assert column_exists("users", "full_name")
# Rollback
rollback_migration()
# Verify rollback
assert not column_exists("users", "full_name")
-- Locks table for long time
ALTER TABLE users ADD COLUMN email VARCHAR(255) NOT NULL;
-- Can't rollback
DROP TABLE old_users;
-- Breaks existing code immediately
ALTER TABLE users DROP COLUMN email;
-- Add as nullable first
ALTER TABLE users ADD COLUMN email VARCHAR(255) NULL;
-- Rename instead of drop
ALTER TABLE old_users RENAME TO archived_users;
-- Keep old column until new code deployed
-- (multi-phase approach)
-- Every migration needs DOWN
-- UP
ALTER TABLE users ADD COLUMN full_name VARCHAR(255);
-- DOWN
ALTER TABLE users DROP COLUMN full_name;
Making a schema change?
Need zero downtime?
Planning rollback?
Choosing migration tool?
→ See references/decision-trees.md for comprehensive decision frameworks
Migration failed halfway → Check database state, fix forward with repair migration
Schema drift detected → Use autogenerate to create reconciliation migration
Cannot rollback (no downgrade) → Create reverse migration or fix forward
Foreign key violation → Clean data before adding constraint, or add as NOT VALID
Migration locks table too long → Use CONCURRENTLY, add columns in phases, batch updates
Circular dependency → Create merge migration or reorder dependencies
→ See references/troubleshooting.md for detailed solutions with examples
🌳 Decision Trees - Schema migration strategies, zero-downtime patterns, rollback strategies, migration tool selection, and data migration approaches. Load when planning migrations or choosing strategies.
🔧 Troubleshooting - Failed migration recovery, schema drift detection, migration conflicts, rollback failures, data integrity issues, and performance problems. Load when debugging migration issues.
development
Optimize web performance using Core Web Vitals, modern patterns (View Transitions, Speculation Rules), and framework-specific techniques
development
Best practices for documenting APIs and code interfaces, eliminating redundant documentation guidance per agent.
development
Comprehensive API design patterns covering REST, GraphQL, gRPC, versioning, authentication, and modern API best practices
development
Visual verification workflow for UI changes to accelerate code review and catch ...