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-migration
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/review-migration
11 version: 1.0.0
12 user-invocable: "true"
13---
14
15# Review Migration
16
17Use this skill when schema changes must be safe in real environments.
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```text
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## Core Process
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- Deploy code that tolerates both old and new schemas during transitions.
50- Add indexes concurrently when supported.
51- Backfill in batches outside a long transaction when volume is high.
52- Use multi-step rollouts for renames, type changes, and unique constraints.
53- For every step, state the expected lock or table-rewrite risk explicitly; if negligible, say why.
54
55If the project uses `strong_migrations`, follow it. If it does not, apply the same safety rules manually.
56
57**Type change rollout pattern:**
58
59```text
601. Add the new typed column as nullable.
612. Dual-write old and new columns from application code.
623. Backfill in batches outside the migration transaction.
634. Read from the new column after parity checks pass.
645. Stop writing the old column, then drop it in a later deploy.
65```
66
67## Code Examples
68
69**Concurrent index (Rails / PostgreSQL):**
70
71```ruby
72# Migration file
73class AddIndexOnUsersEmail < ActiveRecord::Migration[7.1]
74 disable_ddl_transaction!
75
76 def change
77 add_index :users, :email, algorithm: :concurrently
78 end
79end
80```
81
82> `disable_ddl_transaction!` is required — concurrent index creation cannot run inside a transaction.
83
84**Nullable-first column with deferred NOT NULL (Rails):**
85
86```ruby
87# Step 1 — Deploy: add nullable column
88class AddConfirmedAtToUsers < ActiveRecord::Migration[7.1]
89 def change
90 add_column :users, :confirmed_at, :datetime
91 end
92end
93
94# Step 2 — Backfill outside migration (background job or script)
95User.in_batches(of: 1_000) do |batch|
96 batch.update_all(confirmed_at: Time.current)
97end
98
99# Step 3 — Deploy: enforce NOT NULL only after all rows are filled
100class ChangeConfirmedAtNotNull < ActiveRecord::Migration[7.1]
101 def change
102 change_column_null :users, :confirmed_at, false
103 end
104end
105```
106
107**Batch backfill snippet (safe for large tables):**
108
109```ruby
110# Run this as a Rake task or background job, never inside a migration transaction
111BATCH_SIZE = 1_000
112
113User.where(confirmed_at: nil).in_batches(of: BATCH_SIZE) do |batch|
114 batch.update_all(confirmed_at: Time.current)
115 sleep(0.05) # throttle to reduce replication lag
116end
117```
118
119## Common Mistakes
120
121| Mistake | Why It Fails | Fix |
122|---------|-------------|-----|
123| `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 |
124| `add_index :users, :email` without `algorithm: :concurrently` | Acquires share lock; blocks writes | Add `algorithm: :concurrently` + `disable_ddl_transaction!` |
125| Backfill inside migration with `User.update_all(...)` | Holds transaction lock for full duration | Move backfill to a separate job |
126| Rename column directly | Breaks running app during deploy | Add new column, dual-write, migrate callers, drop old |
127| Drop column while code still reads it | Runtime `unknown attribute` errors | Remove code references first, deploy, then drop |
128
129## Output Style
130
1311. List risks first.
1322. For each risk include: Migration step, likely failure mode, explicit lock/table-rewrite risk, safer rollout, rollback or forward-fix note.
1333. Ensure backwards compatibility steps are included.
1344. 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.
1355. Language — Must be in English unless explicitly requested otherwise.
136
137## Integration
138
139| Skill | When to chain |
140|-------|---------------|
141| **code-review** | When reviewing PRs that include migrations |
142| **implement-background-job** | For backfill jobs that run after schema change |
143| **security-check** | When migrations expose or move sensitive data |
144
145## Additional Resources
146
147- [PATTERNS.md](PATTERNS.md) — Advanced migration patterns for complex schema operations