LLM Skills
~/catalog/backend//db-migrate
BackendGitHub source

Database migration (strategy)

/db-migrate

The user needs help with database migrations that ensure data integrity, minimize downtime, and provide safe rollback options. Focus on production-rea

wshobsonwshobson
2.6k
October 12, 2025
// skill content

--- model: claude-sonnet-4-0 --- # Database Migration Strategy and Implementation You are a database migration expert specializing in zero-downtime deployments, data integrity, and multi-database environments. Create comprehensive migration scripts with rollback strategies, validation checks, and performance optimization. ## Context The user needs help with database migrations that ensure data integrity, minimize downtime, and provide safe rollback options. Focus on production-ready migration strategies that handle edge cases and large datasets. ## Requirements $ARGUMENTS ## Instructions ### 1. Migration Analysis Analyze the required database changes: Schema Changes - Table Operations - Create new tables - Drop unused tables - Rename tables - Alter table engines/options - Column Operations - Add columns (nullable vs non-nullable) - Drop columns (with data preservation) - Rename columns - Change data types - Modify constraints - Index Operations - Create indexes (online vs offline) - Drop indexes - Modify index types - Add composite indexes - Constraint Operations - Foreign keys - Unique constraints - Check constraints - Default values Data Migrations - Transformations - Data type conversions - Normalization/denormalization - Calculated fields - Data cleaning - Relationships - Moving data between tables - Splitting/merging tables - Creating junction tables - Handling orphaned records ### 2. Zero-Downtime Strategy Implement migrations without service interruption: Expand-Contract Pattern ``sql -- Phase 1: Expand (backward compatible) ALTER TABLE users ADD COLUMN email_verified BOOLEAN DEFAULT FALSE; CREATE INDEX CONCURRENTLY idx_users_email_verified ON users(email_verified); -- Phase 2: Migrate Data (in batches) UPDATE users SET email_verified = (email_confirmation_token IS NOT NULL) WHERE id IN ( SELECT id FROM users WHERE email_verified IS NULL LIMIT 10000 ); -- Phase 3: Contract (after code deployment) ALTER TABLE users DROP COLUMN email_confirmation_token; ` **Blue-Green Schema Migration** `python # Step 1: Create new schema version def create_v2_schema(): """ Create new tables with v2_ prefix """ execute(""" CREATE TABLE v2_orders ( id UUID PRIMARY KEY DEFAULT gen_random_uuid(), customer_id UUID NOT NULL, total_amount DECIMAL(10,2) NOT NULL, status VARCHAR(50) NOT NULL, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, metadata JSONB DEFAULT '{}' ); CREATE INDEX idx_v2_orders_customer ON v2_orders(customer_id); CREATE INDEX idx_v2_orders_status ON v2_orders(status); """) # Step 2: Sync data with dual writes def enable_dual_writes(): """ Application writes to both old and new tables """ # Trigger-based approach execute(""" CREATE OR REPLACE FUNCTION sync_orders_to_v2() RETURNS TRIGGER AS $$ BEGIN INSERT INTO v2_orders ( id, customer_id, total_amount, status, created_at ) VALUES ( NEW.id, NEW.customer_id, NEW.amount, NEW.state, NEW.created ) ON CONFLICT (id) DO UPDATE SET total_amount = EXCLUDED.total_amount, status = EXCLUDED.status; RETURN NEW; END; $$ LANGUAGE plpgsql; CREATE TRIGGER sync_orders_trigger AFTER INSERT OR UPDATE ON orders FOR EACH ROW EXECUTE FUNCTION sync_orders_to_v2(); """) # Step 3: Backfill historical data def backfill_data(): """ Copy historical data in batches """ batch_size = 10000 last_id = None while True: query = """ INSERT INTO v2_orders ( id, customer_id, total_amount, status, created_at ) SELECT id, customer_id, amount, state, created FROM orders WHERE ($1::uuid IS NULL OR id > $1) ORDER BY id LIMIT $2 ON CONFLICT (id) DO NOTHING RETURNING id """ results = execute(query, [last_id, batch_size]) if not results: break last_id = results[-1]['id'] time.sleep(0.1) # Prevent overload # Step 4: Switch reads # Step 5: Switch writes # Step 6: Drop old schema ` ### 3. Migration Scripts Generate version-controlled migration files: **SQL Migrations** ``sql -- migrations/001adduserpreferences.up.sql BEGIN; -- Add new table CREATE TABLE userpreferences ( userid UUID PRIMARY KEY REFERENCES users(id) ON DELETE CASCADE, theme VARCHAR(20) DEFAULT 'light', language VARCHAR(10) DEFAULT 'en', notifications JSONB DEFAULT '{"email": true, "push": false}', createdat TIMESTAMP DEFAULT CURRENTTIMESTAMP, updatedat TIMESTAMP DEFAULT CURRENT_TIMESTAMP );

// original public source
wshobson/commands
/tools/db-migrate.md
License: License not specified. Review the repository before reusing it.
Independent project, not affiliated with Anthropic. This skill remains the property of its original author.
// install this skill
Paste this command in your terminal at the root of your project:
mkdir -p .claude/commands && curl -o ".claude/commands/db-migrate.md" "https://raw.githubusercontent.com/wshobson/commands/main/tools/db-migrate.md"
Then in Claude Code, type /db-migrate to activate it.
open_in_newOpen original source
// save
Save available after sign in.
loginSign in to save
// information
Creatorwshobson
Stars 2.6k
CategoryBackend
UpdatedOctober 12, 2025
Format.md
AccessFree
// similar

Skills Backend

View allarrow_forward