Rails Migration Safety
Use this skill when schema changes must be safe in real environments.
Core principle: Prefer phased rollouts over one-shot migrations on large or busy tables.
Quick Reference
| Operation |
Safe Pattern |
| Add column |
Nullable first, backfill later, enforce NOT NULL last |
| Add index (large table) |
algorithm: :concurrent (PG) / :inplace (MySQL) |
| Backfill data |
Batch job, not inside migration transaction |
| Rename column |
Add new, copy data, migrate callers, drop old |
| Add NOT NULL |
After backfill confirms all rows have values |
| Add foreign key |
After cleaning orphaned records |
| Remove column |
Remove code references first, then drop column |
HARD-GATE
DO NOT combine schema change and data backfill in one migration.
DO NOT add NOT NULL on a column that hasn't been fully backfilled.
DO NOT drop columns before all code references are removed.
Review Order
- Identify the database and table-size risk.
- Separate schema changes from data backfills.
- Check lock behavior for indexes, constraints, defaults, and rewrites.
- Plan deployment order between app code and migration code.
- Plan rollback or forward-fix strategy.
Safe Patterns
- Add nullable column first, backfill later, enforce
NOT NULL last.
- Add indexes concurrently when supported.
- Backfill in batches outside a long transaction when volume is high.
- Deploy code that tolerates both old and new schemas during transitions.
- Use multi-step rollouts for renames, type changes, and unique constraints.
If the project uses strong_migrations, follow it. If it does not, apply the same safety rules manually.
Examples
Risky (avoid):
add_column :orders, :status, :string, default: 'pending', null: false
Order.update_all("status = 'pending'")
- Risk: Long lock; table rewrite if default is applied on large table.
Safe pattern:
# Step 1: add nullable column
add_column :orders, :status, :string
# Step 2 (separate deploy): backfill in batches outside migration
# Step 3 (after backfill): add constraint
change_column_null :orders, :status, false
change_column_default :orders, :status, from: nil, to: 'pending'
Index on large tables:
# PostgreSQL: concurrent index (no write lock)
disable_ddl_transaction!
add_index :orders, :processed_at, algorithm: :concurrent
# MySQL: online DDL
add_index :orders, :processed_at, algorithm: :inplace
Common Mistakes
| Mistake |
Reality |
| "Table is small, no need for phased migration" |
Tables grow. Build the habit for all migrations. |
| Schema change + backfill in one migration |
Long transaction, long lock. Always separate them. |
| Column rename with immediate app cutover |
App will crash during deploy. Use add-copy-migrate-drop. |
add_index without algorithm: :concurrent |
Exclusive lock on large PostgreSQL tables blocks writes. |
| Adding NOT NULL before backfill completes |
Migration fails or locks table waiting for backfill. |
| Removing column before removing code references |
App crashes when accessing the missing column. |
Red Flags
- Schema change and data backfill combined in one long migration
- Column rename with app code assuming immediate cutover
- Large-table default, rewrite, or NOT NULL change without a phased plan
- Foreign key or unique constraint added before cleaning existing data
- Destructive remove or drop in the same deploy as the replacement path
- No rollback or forward-fix strategy documented
Output Style
List risks first.
For each risk include:
- Migration step
- Likely failure mode or lock risk
- Safer rollout
- Rollback or forward-fix note
For complex type changes, multi-step unique constraints, foreign key validation deferral, safe column removal, and multi-database migrations see PATTERNS.md.
Integration
| Skill |
When to chain |
| rails-code-review |
When reviewing PRs that include migrations |
| rails-background-jobs |
For backfill jobs that run after schema change |
| rails-security-review |
When migrations expose or move sensitive data |
1---
2name: rails-migration-safety
3license: MIT
4description: >
5 Use when planning or reviewing production database migrations, adding columns, indexes,
6 constraints, backfills, renames, table rewrites, or concurrent operations. Covers phased
7 rollouts, lock behavior, rollback strategy, strong_migrations compliance, and deployment
8 ordering for schema changes.
9metadata:
10 source: https://skillmd.com/skills/aibot88/rails-migration-safety
11---
12
13# Rails Migration Safety
14
15Use this skill when schema changes must be safe in real environments.
16
17**Core principle:** Prefer phased rollouts over one-shot migrations on large or busy tables.
18
19## Quick Reference
20
21| Operation | Safe Pattern |
22|-----------|-------------|
23| Add column | Nullable first, backfill later, enforce NOT NULL last |
24| Add index (large table) | `algorithm: :concurrent` (PG) / `:inplace` (MySQL) |
25| Backfill data | Batch job, not inside migration transaction |
26| Rename column | Add new, copy data, migrate callers, drop old |
27| Add NOT NULL | After backfill confirms all rows have values |
28| Add foreign key | After cleaning orphaned records |
29| Remove column | Remove code references first, then drop column |
30
31## HARD-GATE
32
33```
34DO NOT combine schema change and data backfill in one migration.
35DO NOT add NOT NULL on a column that hasn't been fully backfilled.
36DO NOT drop columns before all code references are removed.
37```
38
39## Review Order
40
411. Identify the database and table-size risk.
422. Separate schema changes from data backfills.
433. Check lock behavior for indexes, constraints, defaults, and rewrites.
444. Plan deployment order between app code and migration code.
455. Plan rollback or forward-fix strategy.
46
47## Safe Patterns
48
49- Add nullable column first, backfill later, enforce `NOT NULL` last.
50- Add indexes concurrently when supported.
51- Backfill in batches outside a long transaction when volume is high.
52- Deploy code that tolerates both old and new schemas during transitions.
53- Use multi-step rollouts for renames, type changes, and unique constraints.
54
55If the project uses `strong_migrations`, follow it. If it does not, apply the same safety rules manually.
56
57## Examples
58
59**Risky (avoid):**
60
61```ruby
62add_column :orders, :status, :string, default: 'pending', null: false
63Order.update_all("status = 'pending'")
64```
65
66- **Risk:** Long lock; table rewrite if default is applied on large table.
67
68**Safe pattern:**
69
70```ruby
71# Step 1: add nullable column
72add_column :orders, :status, :string
73
74# Step 2 (separate deploy): backfill in batches outside migration
75
76# Step 3 (after backfill): add constraint
77change_column_null :orders, :status, false
78change_column_default :orders, :status, from: nil, to: 'pending'
79```
80
81**Index on large tables:**
82
83```ruby
84# PostgreSQL: concurrent index (no write lock)
85disable_ddl_transaction!
86add_index :orders, :processed_at, algorithm: :concurrent
87
88# MySQL: online DDL
89add_index :orders, :processed_at, algorithm: :inplace
90```
91
92## Common Mistakes
93
94| Mistake | Reality |
95|---------|---------|
96| "Table is small, no need for phased migration" | Tables grow. Build the habit for all migrations. |
97| Schema change + backfill in one migration | Long transaction, long lock. Always separate them. |
98| Column rename with immediate app cutover | App will crash during deploy. Use add-copy-migrate-drop. |
99| `add_index` without `algorithm: :concurrent` | Exclusive lock on large PostgreSQL tables blocks writes. |
100| Adding NOT NULL before backfill completes | Migration fails or locks table waiting for backfill. |
101| Removing column before removing code references | App crashes when accessing the missing column. |
102
103## Red Flags
104
105- Schema change and data backfill combined in one long migration
106- Column rename with app code assuming immediate cutover
107- Large-table default, rewrite, or NOT NULL change without a phased plan
108- Foreign key or unique constraint added before cleaning existing data
109- Destructive remove or drop in the same deploy as the replacement path
110- No rollback or forward-fix strategy documented
111
112## Output Style
113
114List risks first.
115
116For each risk include:
117- Migration step
118- Likely failure mode or lock risk
119- Safer rollout
120- Rollback or forward-fix note
121
122For complex type changes, multi-step unique constraints, foreign key validation deferral, safe column removal, and multi-database migrations see [PATTERNS.md](./PATTERNS.md).
123
124## Integration
125
126| Skill | When to chain |
127|-------|---------------|
128| **rails-code-review** | When reviewing PRs that include migrations |
129| **rails-background-jobs** | For backfill jobs that run after schema change |
130| **rails-security-review** | When migrations expose or move sensitive data |