db-migrations
Skill3 starsChanged 8 days agoMITView on GitHub
Use when writing a schema or data migration, adding a column or index to a large table, planning a rollback, or preparing a zero-downtime deploy. Postgres locks and lock_timeout, CREATE INDEX CONCURRENTLY, expand-contract renames, batched backfills, Django and golang-migrate workflows.
9 other projects use a version of this.
Adopted by
Most recently taken up by
- d4rken/clankermuxtheir own version, first seen Nov 28, 2026
- lobehub/lobehubtheir own version, first seen Nov 24, 2026
- superset-sh/supersettheir own version, first seen Nov 23, 2026
- pankaj-mahaur/Agent-Daemontheir own version, first seen Nov 16, 2026
- rafapeixer/go-skillstheir own version, first seen Nov 15, 2026
- gabrielbauman/skillstheir own version, first seen Nov 1, 2026
Used with
Projects with it also use testing.
Other files people use alongside this one.
Files found in the same public projects as a version of this one, more often than they turn up anywhere else. Each project counts once, large collections count for less, and this file's own repository is left out.
NVIDIASkilltesting
Run Python and Rust tests for physicsnemo-curator using uv + pytest with coverage reporting via pytest-cov, and cargo-nextest for Rust tests. Includes benchmark workflows with pytest-benchmark and criterion.
Also in 4 projects that use this
Other versions9 projects use a version of this.
Word for word · 0
None found.
Edited · 0
None found.
Wrote their own version · 9
- lobehub/lobe-chat★ 86ksame name
- lobehub/lobehub★ 86ksame name
- ParadevAI/Paradev★ 17ksame name
- superset-sh/superset★ 17ksame name
- d4rken/clankermux★ 11same name
- gabrielbauman/skills★ 1same name
- rafapeixer/go-skills★ 1same name
- rafapeixer/java-skills★ 1same name
- pankaj-mahaur/Agent-Daemon★ 0same name
How matches are found. Word for word: the exact same text. Edited: shares passages with this file (“99% same” means 99% of its five-word phrases also appear here; 10% or more counts). Their own version: similar meaning, or, weakest, the same name. We can’t tell which project had it first. Projects are counted among the 200 most-starred candidates, and mentions are links to the repository we found, up to 50 per site, so both counts are a floor.
In this repository12 other files it gives its agents.
- adversarial-reviewSkill
- agent-workdirSkill
- airgapSkill
- delegateSkill
- docker-packagingSkill
- handoffSkill
- impeccableSkill
- run-validationSkill
- security-huntSkill
- ship-auditSkill
- skill-critiqueSkill
- web-perimeter-auditSkill
The file
We split this skill into the rules it gives an agent
6 rules. 0 are common, 6 are distinctive, and 0 are very specific to this project.
0
Common
Most of the 9 edited versions we read keep these.
6
Distinctive
This author's own choices. Worth a look.
0
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Distinctive: line 20
Every change is a migration: Never alter production databases by hand.
- Distinctive: line 22
Schema and data migrations are separate: Never mix DDL and DML in one migration.
- Distinctive: line 24
Migrations are immutable once deployed: Never edit a migration that has run in production.
- Distinctive: line 28
Before applying any migration:
- Distinctive: line 57
Run the self-checking reproduction SQL on PostgreSQL 11+:
- Distinctive: line 222
Remove a column from the Django model without dropping it from the database yet:
“Common” is measured: kept by at least 6 in 10 of the 9 edited versions of this file we could read. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Database migration patterns
Safe, reversible schema changes for production systems.
When to use
- Creating or altering tables
- Adding or removing columns or indexes
- Running data migrations (backfill, transform)
- Planning zero-downtime schema changes
- Setting up migration tooling for a new project
Core principles
- Every change is a migration. Never alter production databases by hand.
- Migrations are forward-only in production. Rollbacks are new forward migrations.
- Schema and data migrations are separate. Never mix DDL and DML in one migration.
- Test against production-sized data. A migration that works on 100 rows may lock on 10M.
- Migrations are immutable once deployed. Never edit a migration that has run in production.
Safety checklist
Before applying any migration:
- Production rollback/recovery is a new forward migration; DOWN files, if present, are for development only
- Lock modes and expected hold times reviewed; concurrent operations used where supported
- On populated tables, new columns are nullable or have defaults compatible with their constraints
- Indexes created concurrently (not inline with CREATE TABLE for existing tables)
- Data backfill is a separate migration from the schema change
- Tested against a copy of production data
- Rollback plan documented
PostgreSQL patterns
Adding a column safely
-- Nullable column: no table rewrite, but takes ACCESS EXCLUSIVE lock
ALTER TABLE users ADD COLUMN avatar_url TEXT;
-- PostgreSQL 11+: a non-volatile default avoids a table rewrite
-- Still takes ACCESS EXCLUSIVE lock and may wait for other transactions
ALTER TABLE users ADD COLUMN is_active BOOLEAN NOT NULL DEFAULT true;
-- BAD on a populated table: existing rows would have NULL in the new column
ALTER TABLE users ADD COLUMN role TEXT NOT NULL;
-- Fails with not_null_violation (SQLSTATE 23502); does not backfill existing rowsAvoiding a table rewrite does not mean avoiding locks. These ADD COLUMN operations take an ACCESS EXCLUSIVE lock, held until the transaction ends. Keep the transaction short and set a lock_timeout to bound lock acquisition waits. A volatile default such as clock_timestamp() requires a table rewrite; the PostgreSQL 11+ fast path applies only to non-volatile defaults. Adding a NOT NULL column without a default can succeed on an empty table.
Run the self-checking reproduction SQL on PostgreSQL 11+:
psql -X -v ON_ERROR_STOP=1 -f examples/postgres-add-column.sqlIt uses a temporary table, rolls back the ALTERs and drops the fixture, checking lock acquisition, rewrites and the expected constraint violation.
Reference: PostgreSQL ALTER TABLE.
Adding an index without downtime
-- BAD: blocks writes on large tables
CREATE INDEX idx_users_email ON users (email);
-- GOOD: non-blocking, allows concurrent writes
CREATE INDEX CONCURRENTLY idx_users_email ON users (email);
-- Note: CONCURRENTLY cannot run inside a transaction block
-- Most migration tools need special handling for thisRenaming a column (zero-downtime)
For a pure rename (both columns hold the same value), use expand-contract. Keep username authoritative until the read cutover:
-- Step 1: Expand (migration 001)
ALTER TABLE users ADD COLUMN display_name TEXT;
-- Step 2: Deploy app v2: read username, write BOTH columns atomically.
-- Cover every INSERT/UPDATE path, including jobs, imports, and admin tools.
-- Drain old-only writers and their in-flight transactions before backfilling.
-- Step 3: Backfill in bounded primary-key ranges (separate data migration).
-- Example range; advance bounds to cover every existing id.
BEGIN;
SET TRANSACTION ISOLATION LEVEL READ COMMITTED;
SET LOCAL lock_timeout = '2s';
UPDATE users SET display_name = username
WHERE id > 0 AND id <= 10000
AND display_name IS DISTINCT FROM username;
COMMIT;
-- Step 4: Validate AFTER all ranges complete, while dual writes continue.
-- Require zero mismatches before switching reads.
SELECT count(*) AS mismatched_rows FROM users
WHERE display_name IS DISTINCT FROM username;
-- Step 5: Deploy app v3: read display_name, still write BOTH columns.
-- Keep dual writes throughout the rollback window.
-- Step 6: After the rollback window, stop backfill/reconciliation workers.
-- Transfer required constraints/indexes/dependencies to the new column.
-- Make old-column constraints compatible with new-only INSERTs/UPDATEs.
-- Drain non-app readers of username and their in-flight transactions first.
-- Then deploy app v4 using ONLY display_name; drain remaining old-column users.
-- Step 7: Contract in a separate migration after checking dependencies.
ALTER TABLE users DROP COLUMN username;Commit each range independently; checkpoint only after commit and retry failed batch transactions. Choose bounds for the actual key space (including zero or negative IDs if present). IS DISTINCT FROM repairs stale non-NULL values and handles NULL pairs without repeatedly updating them. Do not fetch username into application memory and later write that stale snapshot: copy the row's current value in the same UPDATE. At Read Committed, concurrent updates serialize on the row and PostgreSQL rechecks the predicate after waiting.
Deploying dual writes does not repair earlier drift by itself. Finish draining old-only writers before backfill, and validate parity before read cutover. Once new-only writes start, the old column may be stale: do not restart this backfill or roll back to old readers without a reconciliation plan. The timeline below shows deployment gates, not guaranteed elapsed time or lock-free DDL.
The self-checking SQL example reproduces the stale-value failure and verifies reconciliation, retries and NULL handling:
psql -X -v ON_ERROR_STOP=1 -f examples/postgres-column-rename.sqlReference: PostgreSQL Read Committed behavior.
Removing a column safely
-- Step 1: Remove all application references to the column
-- Step 2: Deploy application without the column reference
-- Step 3: Drop column in next migration
ALTER TABLE orders DROP COLUMN legacy_status;
-- For Django: use SeparateDatabaseAndState to remove from model
-- without generating DROP COLUMN (then drop in next migration)Large data migrations
-- BAD: updates all rows in one transaction (locks table)
UPDATE users SET normalized_email = LOWER(email);
-- GOOD: batch update with progress in a top-level, non-atomic migration
-- No surrounding BEGIN; do not run with psql --single-transaction.
DO $$
DECLARE
batch_size INT := 10000;
rows_updated INT;
BEGIN
LOOP
UPDATE users
SET normalized_email = LOWER(email)
WHERE id IN (
SELECT id FROM users
WHERE normalized_email IS NULL
LIMIT batch_size
FOR UPDATE SKIP LOCKED
);
GET DIAGNOSTICS rows_updated = ROW_COUNT;
RAISE NOTICE 'Updated % rows', rows_updated;
EXIT WHEN rows_updated = 0;
COMMIT;
END LOOP;
END $$;PostgreSQL 11+ permits COMMIT in a top-level DO command. A surrounding transaction or an atomic migration runner rejects it with invalid transaction termination; use a non-atomic migration for this pattern. Run the self-checking SQL example with psql -X -v ON_ERROR_STOP=1 -f examples/postgres-batch-backfill.sql; it verifies multiple committed batches and the final empty batch.
Django (Python)
Workflow
# Generate migration from model changes
python manage.py makemigrations
# Apply migrations
python manage.py migrate
# Show migration status
python manage.py showmigrations
# Generate empty migration for custom SQL
python manage.py makemigrations --empty app_name -n descriptionData migration
from django.db import migrations
def backfill_display_names(apps, schema_editor):
User = apps.get_model("accounts", "User")
batch_size = 5000
users = User.objects.filter(display_name="")
while users.exists():
batch = list(users[:batch_size])
for user in batch:
user.display_name = user.username
User.objects.bulk_update(batch, ["display_name"], batch_size=batch_size)
def reverse_backfill(apps, schema_editor):
pass # Data migration, no reverse needed
class Migration(migrations.Migration):
dependencies = [("accounts", "0015_add_display_name")]
operations = [
migrations.RunPython(backfill_display_names, reverse_backfill),
]SeparateDatabaseAndState
Remove a column from the Django model without dropping it from the database yet:
class Migration(migrations.Migration):
operations = [
migrations.SeparateDatabaseAndState(
state_operations=[
migrations.RemoveField(model_name="user", name="legacy_field"),
],
database_operations=[], # Don't touch the DB yet
),
]golang-migrate (Go)
Workflow
# Create migration pair
migrate create -ext sql -dir migrations -seq add_user_avatar
# Apply all pending migrations
migrate -path migrations -database "$DATABASE_URL" up
# Roll back last migration
migrate -path migrations -database "$DATABASE_URL" down 1
# Force version (fix dirty state)
migrate -path migrations -database "$DATABASE_URL" force VERSIONMigration files
-- migrations/000003_add_user_avatar.up.sql
ALTER TABLE users ADD COLUMN avatar_url TEXT;
CREATE INDEX CONCURRENTLY idx_users_avatar ON users (avatar_url) WHERE avatar_url IS NOT NULL;
-- migrations/000003_add_user_avatar.down.sql
DROP INDEX IF EXISTS idx_users_avatar;
ALTER TABLE users DROP COLUMN IF EXISTS avatar_url;Zero-downtime strategy
For critical production changes, follow expand-contract:
Phase 1: EXPAND
- Add new column/table (nullable or with default)
- Deploy: app reads OLD, writes BOTH atomically
- Drain old-only writers and in-flight transactions
- Backfill in committed batches, then verify parity
Phase 2: MIGRATE
- Deploy: app reads from NEW, writes to BOTH
- Monitor parity and retain dual writes throughout the rollback window
Phase 3: CONTRACT
- End rollback window; stop backfill and migrate constraints/dependencies
- Drain non-app old-column readers and their in-flight transactions
- Deploy: app only uses NEW; drain remaining old-column users
- Drop old column/table in separate migrationTimeline example
Day 1: Migration adds new_status column (nullable)
Day 1: Deploy app v2: reads status, writes both columns atomically
Day 2: Drain old-only writers; backfill in batches; require zero mismatches
Day 3: Deploy app v3: reads new_status, still writes both columns
Day 7: End rollback window; stop backfill; migrate constraints/dependencies
Day 7: Drain non-app old-column readers and their in-flight transactions
Day 7: Deploy app v4: only uses new_status; drain remaining old-column users
Later: Separate migration drops status after all contract gates passAnti-patterns
| Anti-pattern | Why it fails | Better approach |
| Manual SQL in production | No audit trail, unrepeatable | Always use migration files |
| Editing deployed migrations | Causes drift between environments | Create a new migration instead |
| Adding NOT NULL column without default to populated table | Fails because existing rows would contain NULL | Add nullable, backfill, then set NOT NULL |
| Inline index on large table | Blocks writes during build | CREATE INDEX CONCURRENTLY |
| Schema + data in one migration | Hard to roll back, long transactions | Separate migrations |
| Dropping column before removing code | Application errors on missing column | Remove code first, drop column next deploy |
Adapted from affaan-m/everything-claude-code (MIT) @3f14a10: database-migrations.
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

