Django Linear Migrations
django-linear-migrations is a Django app that enforces a linear migration history in your project, preventing common issues caused by concurrent migration development and merge migrations. It achieves this by introducing `max_migration.txt` files for each app, which will conflict in version control (e.g., Git) if multiple branches add migrations concurrently. The library, currently at version 2.19.0, is actively maintained with frequent releases.
Warnings
- breaking In version 2.0.0, the management commands were renamed from using hyphens to underscores to make them importable and extensible. `create-max-migration-files` became `create_max_migration_files` and `rebase-migration` became `rebase_migration`.
- breaking As of version 2.19.0, support for Python 3.9 has been dropped. The library now requires Python 3.10 or newer.
- gotcha Automatic detection of 'first-party' (project) apps can sometimes fail, especially with editable package installations (`pip install -e`). This can lead to `max_migration.txt` files being created for third-party apps.
- gotcha If you override Django's built-in `makemigrations` or `squashmigrations` commands in your project, your custom command must subclass the version provided by `django-linear-migrations` to ensure its functionality. Additionally, your app containing the custom command must be listed *above* `django_linear_migrations` in `INSTALLED_APPS`.
- gotcha The `rebase_migration` command does not currently support rebasing multiple migrations within the same app. Attempting this may lead to unexpected behavior or incomplete rebasing.
- gotcha The `rebase_migration` command does not guarantee that its edits to migration files will match your project's code style. It will attempt to format with Black if installed, but manual reformatting may still be needed.
Install
-
pip install django-linear-migrations
Imports
- django_linear_migrations
INSTALLED_APPS = [ # ... 'django_linear_migrations', # ... ] - Command
from django_linear_migrations.management.commands.makemigrations import Command as BaseCommand class Command(BaseCommand): # ... your custom logic pass
Quickstart
# 1. Install the library
pip install django-linear-migrations
# 2. Add to INSTALLED_APPS in settings.py
# settings.py
INSTALLED_APPS = [
# ... your first-party apps first
'your_app_name',
'django_linear_migrations',
# ... other third-party apps
]
# Optional: Explicitly define first-party apps if auto-detection fails (e.g., with editable installs)
# settings.py
# FIRST_PARTY_APPS = ['your_app_name']
# INSTALLED_APPS = FIRST_PARTY_APPS + ['django_linear_migrations', ...]
# 3. Check automatic detection of first-party apps (dry run)
python manage.py create_max_migration_files --dry-run
# 4. Create initial max_migration.txt files
python manage.py create_max_migration_files
# Now, makemigrations will automatically update max_migration.txt,
# and `git rebase` or `git merge` will expose migration conflicts.