Detect, diagnose, and auto-fix Django migration problems, circular dependencies, and merge conflicts.
Project description
django-migraid
Detect, diagnose, and auto-fix Django migration problems in Git workflows.
The Problem
Two developers branch from main at migration 0004. Both generate 0005_*.py. Main merges one. The second developer's rebase leaves them with a conflict that makemigrations --merge doesn't cleanly handle in rebase-based workflows. django-migraid fixes this — and ten other common migration pain points.
Installation
pip install django-migraid
Add to INSTALLED_APPS:
INSTALLED_APPS = [
...
"migraid",
]
Quick Start
# Diagnose all migration issues in your project
python manage.py migraid doctor
# Rebase your branch's migrations onto main
python manage.py migraid rebase --base main --dry-run
# Rebase AND keep django_migrations in sync for applied migrations (CI-friendly)
python manage.py migraid rebase --base main --update-db --noinput
# Fix conflicting leaf migrations
python manage.py migraid fix-conflicts --dry-run
# Fix out-of-order numbering
python manage.py migraid renumber myapp --dry-run
# Remove stale django_migrations rows
python manage.py migraid prune --yes
# Clean up untracked migration files after switching branches
python manage.py migraid sync-branch --dry-run
# Visualize the migration DAG
python manage.py migraid graph myapp --format mermaid
Problems This Solves
| Issue | Code | Django Error / Search Term | Command |
|---|---|---|---|
| Conflicting leaf migrations | E001 | multiple leaf nodes, migration merge conflict |
fix-conflicts |
| Circular migration dependencies | E002 | CircularDependencyError, circular dependency |
— (reports) |
| Out-of-order numbering | W006 | gap in numbering, renumber migrations |
renumber |
| Dependency on a deleted migration | E004 | NodeNotFoundError, missing dependency |
— (reports) |
| Renamed applied migration | E005 | InconsistentMigrationHistory, table desync |
rebase/renumber/fix-conflicts --update-db |
Stale django_migrations rows |
W001 | ghost migrations, remove from django_migrations |
prune |
RunPython without reverse_code |
W002 | unreversible data migration |
— (reports) |
| Squashed migration cleanup | W003 | remove old migrations after squash |
— (reports) |
| Merge migrations in rebase flow | W004 | delete django merge migrations |
rebase |
| Non-deterministic dependencies | W005 | random migration order |
— (reports) |
Common Scenarios & How-To
How to fix a Django migration merge conflict?
When two developers create 0005_*.py on different branches, run:
python manage.py migraid fix-conflicts
This linearizes the migrations into 0005_... and 0006_... automatically.
How to rebase migrations onto another branch?
To renumber your local migrations to follow the latest from main:
python manage.py migraid rebase --base main
How to fix InconsistentMigrationHistory?
If you've renamed or renumbered migrations that are already applied, use the --update-db flag:
python manage.py migraid renumber myapp --update-db
This synchronizes the django_migrations table with your new file names.
How to find circular dependencies?
Run the diagnostic tool to identify cycles in your migration graph:
python manage.py migraid doctor
Command Reference
Commands fall into three groups:
Diagnose (read-only): doctor, graph
Rewrite migration files: rebase, fix-conflicts, linearize, renumber — file plane only by default; pass --update-db to also rename django_migrations rows.
Repair DB / branch state: prune, sync-branch — these commands exist to fix the database or file state directly.
doctor
Read-only diagnostic. Reports every detected issue with severity.
python manage.py migraid doctor [--app LABEL] [--format text|json]
rebase
Renumber local branch migrations to follow the latest from a target branch.
python manage.py migraid rebase [--base BRANCH] [--app LABEL] [--dry-run] [--yes] [--force] [--allow-applied] [--update-db] [--noinput] [--database ALIAS]
fix-conflicts
Resolve multiple-leaf conflicts by linearizing the fork.
python manage.py migraid fix-conflicts [--app LABEL] [--dry-run] [--yes] [--force] [--allow-applied] [--update-db] [--noinput] [--database ALIAS]
linearize
Rewrite history into a gap-free 0001..N chain where each migration depends on exactly one predecessor — renumbering, collapsing redundant dependency lists, resolving forks, and deleting merge migrations in one pass. Cross-app dependencies are preserved by default (--strip-cross-app to drop them).
python manage.py migraid linearize [--app LABEL] [--strip-cross-app] [--dry-run] [--yes] [--force] [--allow-applied] [--update-db] [--noinput] [--database ALIAS]
renumber
Fix gap or duplicate numbering in a single app's migrations.
python manage.py migraid renumber <app> [--dry-run] [--yes] [--force] [--allow-applied] [--update-db] [--noinput] [--database ALIAS]
prune
Remove orphaned django_migrations rows for migrations no longer on disk.
python manage.py migraid prune [--dry-run] [--yes] [--noinput] [--database ALIAS] [--allow-remote-db]
sync-branch
Align local migration files (and optionally the database) to the current git branch state. Detects untracked migration files and optionally removes stale django_migrations rows or reverses applied schema changes.
python manage.py migraid sync-branch [--app LABEL] [--dry-run] [--yes] [--noinput] [--database ALIAS] [--update-db] [--schema]
graph
Print or export the migration DAG.
python manage.py migraid graph [app] [--format mermaid|dot|ascii] [--output FILE]
Safety Model
Commands act on the file plane by default. Any DB change requires an explicit flag.
--update-dbauthorizes renamingdjango_migrationsrows in step with file renames (rewrite commands) or deleting stale rows (sync-branch).--schemaauthorizes runningmigratebackwards to reverse schema changes (sync-branch only).pruneandsync-branchare inherently DB/file-repair commands — running them is the authorization, and they always preview + confirm before writing.
Every mutation command also:
- Checks for uncommitted git changes (bypass with
--forceon rewrite commands) - Guards against rewriting already-applied migrations (bypass with
--allow-applied, or use--update-dbwhich implies it) - Creates a
migraid-backup-<timestamp>git ref before any writes - Shows a diff-style preview before making changes
- Asks for confirmation (bypass with
--yes/--noinput) - Maintains an undo log — reverses all file ops automatically if anything fails
- Self-validates after apply: if the migration graph gets worse, auto-reverts
--dry-run on any mutation command prints the full preview without writing.
When renaming applied migrations, --update-db renames the matching
django_migrations rows in the same per-app transaction.atomic() block as the
file changes (preserving the applied timestamp), writes a replayable
inverse-SQL undo script, and rolls back both files and rows on any failure.
See the --update-db guide.
CI Integration
Add to your pre-push hook or CI pipeline:
python manage.py migraid doctor --format json | jq '.[] | select(.severity == "error")'
Or fail CI on any ERROR-level issue:
python manage.py migraid doctor
(exits non-zero if any E0xx issues are found)
Contributing
See CONTRIBUTING.md.
License
Apache 2.0 — see LICENSE.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file django_migraid-0.3.0b4.tar.gz.
File metadata
- Download URL: django_migraid-0.3.0b4.tar.gz
- Upload date:
- Size: 78.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ef545f09adaf041ec788fb90e9244bb61a6b7a16ac5c96790e7e700f2c49259c
|
|
| MD5 |
1d7ad38888989a7ffd36cca05e1c07ef
|
|
| BLAKE2b-256 |
232891c462876f74ea7aca96486b98e9da993bf39a51d3ee96089a2d654048c3
|
Provenance
The following attestation bundles were made for django_migraid-0.3.0b4.tar.gz:
Publisher:
release.yml on AhmedShehab/django-migraid
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
django_migraid-0.3.0b4.tar.gz -
Subject digest:
ef545f09adaf041ec788fb90e9244bb61a6b7a16ac5c96790e7e700f2c49259c - Sigstore transparency entry: 1644849592
- Sigstore integration time:
-
Permalink:
AhmedShehab/django-migraid@498f962184c7d4a04a283bc9b5b68fd03bd706ce -
Branch / Tag:
refs/tags/v0.3.0b4 - Owner: https://github.com/AhmedShehab
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@498f962184c7d4a04a283bc9b5b68fd03bd706ce -
Trigger Event:
push
-
Statement type:
File details
Details for the file django_migraid-0.3.0b4-py3-none-any.whl.
File metadata
- Download URL: django_migraid-0.3.0b4-py3-none-any.whl
- Upload date:
- Size: 46.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
85c5cccaafd61a129f99bfd54338e17e945312a552e655dcdde71b7ca07d13b0
|
|
| MD5 |
f04d2382d4e98a2fd444cc24d5121bc3
|
|
| BLAKE2b-256 |
1e0592a302849b234ed2908bd1ae74c87ae771d8aa15111ab290dba6dfa6cac4
|
Provenance
The following attestation bundles were made for django_migraid-0.3.0b4-py3-none-any.whl:
Publisher:
release.yml on AhmedShehab/django-migraid
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
django_migraid-0.3.0b4-py3-none-any.whl -
Subject digest:
85c5cccaafd61a129f99bfd54338e17e945312a552e655dcdde71b7ca07d13b0 - Sigstore transparency entry: 1644849709
- Sigstore integration time:
-
Permalink:
AhmedShehab/django-migraid@498f962184c7d4a04a283bc9b5b68fd03bd706ce -
Branch / Tag:
refs/tags/v0.3.0b4 - Owner: https://github.com/AhmedShehab
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@498f962184c7d4a04a283bc9b5b68fd03bd706ce -
Trigger Event:
push
-
Statement type: