Skip to the page

Data access and security · mid

Django and Alembic migrations that are safe to deploy

A migration changes a live database while old and new code run together. Autogenerate cannot see renames, an index can lock a table, and conflicting migration files need a merge. Use expand and contract and one migration runner.

The notes

What you must know

  • Django builds a graph from migration files, compares it with the django_migrations table and runs what is missing. Two branches that each add a child of 0041 give two leaf nodes: run makemigrations --merge after checking they do not touch the same field.
  • Data migrations: use apps.get_model() (historical models), give a reverse_code, make them safe to run again and run big ones in batches. --fake only writes rows to django_migrations and runs no SQL.
  • Useful commands: sqlmigrate (shows SQL), migrate --plan, squashmigrations. In CI: makemigrations --check --dry-run and a one-leaf-per-app check.
  • AddIndex runs a plain CREATE INDEX, which blocks INSERT, UPDATE and DELETE until it ends. Use AddIndexConcurrently (django.contrib.postgres.operations) in a migration with atomic = False. A failed concurrent build leaves an INVALID index: drop it and retry.
  • Set lock_timeout (for example 5 s) and statement_timeout for the migration session so it fails fast instead of making everyone queue. Find a stuck lock in pg_stat_activity and pg_locks and use pg_cancel_backend.
  • A column with a constant default is a quick change on PostgreSQL 11 and later; a volatile default, NOT NULL with a backfill or a type change can rewrite or scan the table.
  • Alembic autogenerate compares models with the database and cannot see renames: a renamed column looks like drop and add, so the data is lost. It also misses table renames and some constraints and types. Always review and fix the generated file; review the SQL with alembic upgrade head --sql.
  • Safe rename is expand and contract: add the new column, write to both, backfill in batches, switch reads, deploy, and drop the old column in a later release. Run migrations in one pipeline step or Kubernetes Job before the rollout, not in every pod at startup. A CI check flags drop_column and drop_table for sign-off, and restores must be tested.

The red pen

Common traps: mistake → fix

  • Mistake: op.alter_column fixes a rename in one deploy

    Fix: old pods still use the old name during a rolling deploy.

  • Mistake: Autogenerate output can be applied unchanged

    Fix: it is a draft that needs review.

  • Mistake: A migration blocks only the migrating process

    Fix: a lock can stop every writer.

  • Mistake: --fake is a way to skip a failing migration

    Fix: it only fits when the schema already matches.

Easy to hard

Questions interviewers ask, easy to hard

  1. The deploy fails with Conflicting migrations detected. What happened and how do you fix it?
  2. A column rename deleted every phone number. Why?
  3. A migration that added an index blocked checkout for 9 minutes. What happened?
  4. How do you rename a column safely?
  5. Who should run migrations in Kubernetes?

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview