Review Migration
Use this skill when schema changes must be safe in real environments.
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.
Core Process
- 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
- Deploy code that tolerates both old and new schemas during transitions.
- Add indexes concurrently when supported.
- Backfill in batches outside a long transaction when volume is high.
- Use multi-step rollouts for renames, type changes, and unique constraints.
- For every step, state the expected lock or table-rewrite risk explicitly; if negligible, say why.
If the project uses strong_migrations, follow it. If it does not, apply the same safety rules manually.
Type change rollout pattern:
1. Add the new typed column as nullable.
2. Dual-write old and new columns from application code.
3. Backfill in batches outside the migration transaction.
4. Read from the new column after parity checks pass.
5. Stop writing the old column, then drop it in a later deploy.
Code Examples
Concurrent index (Rails / PostgreSQL):
# Migration file
class AddIndexOnUsersEmail < ActiveRecord::Migration[7.1]
disable_ddl_transaction!
def change
add_index :users, :email, algorithm: :concurrently
end
end
disable_ddl_transaction! is required — concurrent index creation cannot run inside a transaction.
Nullable-first column with deferred NOT NULL (Rails):
# Step 1 — Deploy: add nullable column
class AddConfirmedAtToUsers < ActiveRecord::Migration[7.1]
def change
add_column :users, :confirmed_at, :datetime
end
end
# Step 2 — Backfill outside migration (background job or script)
User.in_batches(of: 1_000) do |batch|
batch.update_all(confirmed_at: Time.current)
end
# Step 3 — Deploy: enforce NOT NULL only after all rows are filled
class ChangeConfirmedAtNotNull < ActiveRecord::Migration[7.1]
def change
change_column_null :users, :confirmed_at, false
end
end
Batch backfill snippet (safe for large tables):
# Run this as a Rake task or background job, never inside a migration transaction
BATCH_SIZE = 1_000
User.where(confirmed_at: nil).in_batches(of: BATCH_SIZE) do |batch|
batch.update_all(confirmed_at: Time.current)
sleep(0.05) # throttle to reduce replication lag
end
Common Mistakes
| Mistake |
Why It Fails |
Fix |
add_column :t, :col, :string, null: false, default: "x" on large table |
Full table rewrite + lock (PG < 11) |
Add nullable, backfill, then add NOT NULL |
add_index :users, :email without algorithm: :concurrently |
Acquires share lock; blocks writes |
Add algorithm: :concurrently + disable_ddl_transaction! |
Backfill inside migration with User.update_all(...) |
Holds transaction lock for full duration |
Move backfill to a separate job |
| Rename column directly |
Breaks running app during deploy |
Add new column, dual-write, migrate callers, drop old |
| Drop column while code still reads it |
Runtime unknown attribute errors |
Remove code references first, deploy, then drop |
Output Style
- List risks first.
- For each risk include: Migration step, likely failure mode, explicit lock/table-rewrite risk, safer rollout, rollback or forward-fix note.
- Ensure backwards compatibility steps are included.
- Always include explicit phased patterns for column renames, type changes, and unique constraints. If one does not apply, mark it
Not applicable and explain why.
- Language — Must be in English unless explicitly requested otherwise.
Integration
| Skill |
When to chain |
| code-review |
When reviewing PRs that include migrations |
| implement-background-job |
For backfill jobs that run after schema change |
| security-check |
When migrations expose or move sensitive data |
Additional Resources
- PATTERNS.md — Advanced migration patterns for complex schema operations
1---2name: review-migration3description: Use when planning or reviewing production database migrations, adding columns, indexes, constraints, backfills, renames, table rewrites, or concurrent operations. Covers phased rollouts, lock behavior, rollback strategy, strong_migrations compliance, and deployment ordering for schema changes.4license: MIT5---6
7# Review Migration
8
9Use this skill when schema changes must be safe in real environments.
10
11## Quick Reference
12
13| Operation | Safe Pattern |
14|-----------|-------------|
15| Add column | Nullable first, backfill later, enforce NOT NULL last |
16| Add index (large table) | `algorithm: :concurrent` (PG) / `:inplace` (MySQL) |
17| Backfill data | Batch job, not inside migration transaction |
18| Rename column | Add new, copy data, migrate callers, drop old |
19| Add NOT NULL | After backfill confirms all rows have values |
20| Add foreign key | After cleaning orphaned records |
21| Remove column | Remove code references first, then drop column |
22
23## HARD-GATE
24
25```text
26DO NOT combine schema change and data backfill in one migration.
27DO NOT add NOT NULL on a column that hasn't been fully backfilled.
28DO NOT drop columns before all code references are removed.
29```
30
31## Core Process
32
331. Identify the database and table-size risk.
342. Separate schema changes from data backfills.
353. Check lock behavior for indexes, constraints, defaults, and rewrites.
364. Plan deployment order between app code and migration code.
375. Plan rollback or forward-fix strategy.
38
39## Safe Patterns
40
41- Deploy code that tolerates both old and new schemas during transitions.
42- Add indexes concurrently when supported.
43- Backfill in batches outside a long transaction when volume is high.
44- Use multi-step rollouts for renames, type changes, and unique constraints.
45- For every step, state the expected lock or table-rewrite risk explicitly; if negligible, say why.
46
47If the project uses `strong_migrations`, follow it. If it does not, apply the same safety rules manually.
48
49**Type change rollout pattern:**
50
51```text
521. Add the new typed column as nullable.
532. Dual-write old and new columns from application code.
543. Backfill in batches outside the migration transaction.
554. Read from the new column after parity checks pass.
565. Stop writing the old column, then drop it in a later deploy.
57```
58
59## Code Examples
60
61**Concurrent index (Rails / PostgreSQL):**
62
63```ruby
64# Migration file
65class AddIndexOnUsersEmail < ActiveRecord::Migration[7.1]
66 disable_ddl_transaction!
67
68 def change
69 add_index :users, :email, algorithm: :concurrently
70 end
71end
72```
73
74> `disable_ddl_transaction!` is required — concurrent index creation cannot run inside a transaction.
75
76**Nullable-first column with deferred NOT NULL (Rails):**
77
78```ruby
79# Step 1 — Deploy: add nullable column
80class AddConfirmedAtToUsers < ActiveRecord::Migration[7.1]
81 def change
82 add_column :users, :confirmed_at, :datetime
83 end
84end
85
86# Step 2 — Backfill outside migration (background job or script)
87User.in_batches(of: 1_000) do |batch|
88 batch.update_all(confirmed_at: Time.current)
89end
90
91# Step 3 — Deploy: enforce NOT NULL only after all rows are filled
92class ChangeConfirmedAtNotNull < ActiveRecord::Migration[7.1]
93 def change
94 change_column_null :users, :confirmed_at, false
95 end
96end
97```
98
99**Batch backfill snippet (safe for large tables):**
100
101```ruby
102# Run this as a Rake task or background job, never inside a migration transaction
103BATCH_SIZE = 1_000
104
105User.where(confirmed_at: nil).in_batches(of: BATCH_SIZE) do |batch|
106 batch.update_all(confirmed_at: Time.current)
107 sleep(0.05) # throttle to reduce replication lag
108end
109```
110
111## Common Mistakes
112
113| Mistake | Why It Fails | Fix |
114|---------|-------------|-----|
115| `add_column :t, :col, :string, null: false, default: "x"` on large table | Full table rewrite + lock (PG < 11) | Add nullable, backfill, then add NOT NULL |
116| `add_index :users, :email` without `algorithm: :concurrently` | Acquires share lock; blocks writes | Add `algorithm: :concurrently` + `disable_ddl_transaction!` |
117| Backfill inside migration with `User.update_all(...)` | Holds transaction lock for full duration | Move backfill to a separate job |
118| Rename column directly | Breaks running app during deploy | Add new column, dual-write, migrate callers, drop old |
119| Drop column while code still reads it | Runtime `unknown attribute` errors | Remove code references first, deploy, then drop |
120
121## Output Style
122
1231. List risks first.
1242. For each risk include: Migration step, likely failure mode, explicit lock/table-rewrite risk, safer rollout, rollback or forward-fix note.
1253. Ensure backwards compatibility steps are included.
1264. Always include explicit phased patterns for column renames, type changes, and unique constraints. If one does not apply, mark it `Not applicable` and explain why.
1275. Language — Must be in English unless explicitly requested otherwise.
128
129## Integration
130
131| Skill | When to chain |
132|-------|---------------|
133| **code-review** | When reviewing PRs that include migrations |
134| **implement-background-job** | For backfill jobs that run after schema change |
135| **security-check** | When migrations expose or move sensitive data |
136
137## Additional Resources
138
139- [PATTERNS.md](PATTERNS.md) — Advanced migration patterns for complex schema operations