agentleFS
DeadlymindDeadlymind/nanolama/skills/migrations/SKILL.md

migrations

Skill1 starsChanged 5 months agoMITView on GitHub

Writes safe schema and data migrations for Django 5.2 on PostgreSQL — data backfills via RunPython with apps.get_model, reversible operations, tenant-aware per-entreprise batching, expand-contract column renames, and concurrent index creation. Use when adding or reviewing a migration, backfilling a column, renaming or dropping a field without downtime, wiring makemigrations --check into CI, or asking how to migrate safely here. Not for the model/ViewSet change that triggers the migration (see multi-tenancy) or lock/transaction concurrency at request time (see db-concurrency).

3 other projects use a version of this.

Adopted by

Most recently taken up by

The file

We split this skill into the rules it gives an agent

4 rules. About 0 are common, 0 are distinctive, and 4 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈0

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 4

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    Data migrations are reversible and idempotent: Every RunPython gets a real reverse callable (or RunPython.noop), and re-running it is a …

    line 17
  2. Very specific:

    Never import models at module top level in a data migration: Use the historical model via apps.get_model, give a reverse, and make it …

    line 29
  3. Very specific:

    Run python manage.py makemigrations --check --dry-run in CI so a model change that forgot its migration fails the build (see ci-cd).

    line 107
  4. Very specific:

    Never edit a migration that is already applied or committed — add a new forward migration.

    line 112

≈ “Common” is an estimate: at least 1 in 5 of the 213,897 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Migrations (safe schema and data change on PostgreSQL)

When to use

Any time a change produces a new migration — a new column, a data backfill, a rename, a drop, or an index. On PostgreSQL a careless migration takes an ACCESS EXCLUSIVE lock or rewrites a large table, so treat "does this block writes in prod?" as a review question, not an afterthought.

Pattern

Two invariants hold everywhere:

  1. Data migrations are reversible and idempotent. Every RunPython gets a real reverse callable (or RunPython.noop), and re-running it is a no-op — never assume it runs exactly once on clean state.
  2. Schema changes that lock or rewrite are split (expand-contract). Add the new shape, backfill, switch reads/writes, then drop the old shape in a later deploy — so no single migration takes a long exclusive lock.

Tenant data lives under entreprise, so backfills iterate per tenant in batches rather than loading one giant queryset.

Steps / idioms

  1. Never import models at module top level in a data migration. Use the historical model via apps.get_model, give a reverse, and make it idempotent:

    from django.db import migrations
    
    def backfill_slug(apps, schema_editor):
        Invoice = apps.get_model("billing", "Invoice")  # historical model, not the import
        # tenant-aware: iterate per entreprise, batch to bound memory + lock time
        for ent_id in Invoice.objects.values_list("entreprise_id", flat=True).distinct():
            qs = Invoice.objects.filter(entreprise_id=ent_id, slug="")  # idempotent: only unset rows
            for inv in qs.iterator(chunk_size=500):
                inv.slug = f"inv-{inv.pk}"
                inv.save(update_fields=["slug"])
    
    def unset_slug(apps, schema_editor):
        Invoice = apps.get_model("billing", "Invoice")
        # reverse ONLY the rows this migration generated; leave pre-existing slugs alone.
        # An unconditional .update(slug="") would destroy slugs the forward step never wrote.
        for ent_id in Invoice.objects.values_list("entreprise_id", flat=True).distinct():
            qs = Invoice.objects.filter(entreprise_id=ent_id).exclude(slug="")
            for inv in qs.iterator(chunk_size=500):
                if inv.slug == f"inv-{inv.pk}":     # our value, not the user's
                    inv.slug = ""
                    inv.save(update_fields=["slug"])
    
    class Migration(migrations.Migration):
        dependencies = [("billing", "0007_invoice_slug")]
        operations = [migrations.RunPython(backfill_slug, unset_slug)]

    A reverse must undo only what the forward step did. If the generated value is not recognisable, do not guess — declare it: migrations.RunPython(backfill_slug, migrations.RunPython.noop) # intentionally irreversible: cannot distinguish generated slugs from pre-existing ones.

  2. Split schema from data. One migration adds the nullable/blank column; a separate RunPython backfills; a later migration adds NOT NULL/constraints. Mixing DDL and a long data loop in one migration holds the lock the whole time.
  3. Add indexes concurrently on hot tables so writes are not blocked. Use AddIndexConcurrently (from django.contrib.postgres.operations) and set atomic = False on the Migration — CREATE INDEX CONCURRENTLY cannot run inside a transaction. Pass the model name, not the app label.

Expand-contract (zero-downtime rename)

A single RenameField rewrites nothing but breaks any running old code mid-deploy. Split across releases instead:

  1. Expand — add the new column; dual-write to both old and new in app code.
  2. Backfill — RunPython copies old to new per entreprise, in batches.
  3. Switch — ship code that reads/writes only the new column.
  4. Contract — a later migration drops the old column, once no code references it.

Each step is independently deployable and reversible. Never collapse them.

Adapt to your repo

Rename entreprise/Invoice/billing and the app label to match your project. Confirm whether your table is large enough to need atomic = False + concurrent indexes (small tables are fine with a plain AddIndex). Pick chunk_size to fit your row width. If a backfill is huge, run it as an idempotent management command or celery-task and keep the migration to schema only.

Gotchas

  • Importing the app model directly (from billing.models import Invoice) breaks when the migration replays against old state — always apps.get_model.
  • A RunPython with no reverse blocks migrate from rolling back; pass RunPython.noop when the change genuinely cannot be undone.
  • A reverse that is broader than its forward is data loss, not a rollback. If the forward only touched unset rows, the reverse must only reset rows it generated — an unconditional Model.objects.update(field="") wipes values that predate the migration, across every tenant, and no error is raised. When you cannot tell your value from the user's, RunPython.noop plus an explicit "intentionally irreversible" comment is the honest reverse.
  • CREATE INDEX CONCURRENTLY fails inside a transaction — it needs atomic = False, and a failed concurrent index leaves an INVALID index you must drop by hand.
  • Adding a NOT NULL column with a default on a big table rewrites it under a lock; add nullable, backfill, then set NOT NULL in a follow-up.
  • Run python manage.py makemigrations --check --dry-run in CI so a model change that forgot its migration fails the build (see ci-cd).
  • On a multi-instance or rolling deploy, run migrate as a single leader-only step (one instance), never on every instance's startup — concurrent migrate runs race the migration ledger and can deadlock or double-apply.
  • Never edit a migration that is already applied or committed — add a new forward migration. Two branches that each add a migration to one app clash; resolve with makemigrations --merge and rebase. --fake marks a migration applied without running it — safe only when the DB already matches that state, otherwise it silently skips the real schema change.
  • Decide where a data change lives. A one-time backfill tied to this schema change belongs in RunPython in the migration; evolving reference/config data belongs in an idempotent, re-runnable seed management command (not a migration — migrations are frozen and replay everywhere forever); a one-off repair of bad data belongs in a run-once fix command.

See also

  • multi-tenancy
  • db-concurrency
  • ci-cd

Download now (ZIP)

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
What did you use it for? e.g. Reviewing pull requests in a Rails app

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.