Creates safe database migrations with proper indexes and rollback strategies. Use when creating tables, adding columns, creating indexes, handling zero-downtime migrations, or when user mentions migrations, schema changes, or database structure.
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.
A direct command skips the review prompt. Inspect the source before running it.
Creates safe database migrations with proper indexes and rollback strategies. Use when creating tables, adding columns, creating indexes, handling zero-downtime migrations, or when user mentions migrations, schema changes, or database structure.
allowed-tools
Read, Write, Edit, Bash, Glob, Grep
Database Migration Patterns for Rails 8
Overview
Safe database migrations are critical for production stability:
classBackfillEventStatus < ActiveRecord::Migration[8.0]
disable_ddl_transaction!
defupEvent.unscoped.in_batches(of:1000) do |batch|
batch.where(status:nil).update_all(status:0)
sleep(0.1) # Reduce database loadendenddefdown# No rollback for data migrationendend
Index Strategies
Composite Indexes
# For queries: WHERE account_id = ? AND status = ?
add_index :events, [:account_id, :status]
# Order matters! Left-to-right prefix matching:# Helps: WHERE account_id = ? AND status = ?# Helps: WHERE account_id = ?# Does NOT help: WHERE status = ?
Partial Indexes
# Index only active records
add_index :events, :event_date, where:"status = 0", name:"index_events_on_date_active"# Index only non-null values
add_index :users, :reset_token, where:"reset_token IS NOT NULL"
# DON'T - Locks entire table
add_index :large_table, :column# DO - Non-blocking
disable_ddl_transaction!
add_index :large_table, :column, algorithm::concurrently# DON'T - Updates all at onceEvent.update_all(status:0)
# DO - Updates in batchesEvent.in_batches(of:1000) do |batch|
batch.update_all(status:0)
end