Django Migration Linter
django-migration-linter is a tool for Django projects designed to detect backward incompatible database migrations. It analyzes new migrations against a baseline (e.g., a Git branch) to prevent accidental breaking changes to your database schema. The current version is 6.0.0, and it maintains an active release cadence, frequently adding support for new Python and Django versions.
Warnings
- breaking As of v6.0.0, `django-migration-linter` now correctly handles custom Django app labels when determining migrations from a Git reference. Previously, it might have relied on folder names. If your project uses custom app labels, the linter will now reference apps by their Django label, which might change detection behavior if you were implicitly relying on folder names.
- breaking Beginning with v5.0.0, the linter no longer silently ignores failures that occur during its internal calls to Django's `sqlmigrate` command. Instead, it will now crash and raise the `sqlmigrate` error. This change prevents problematic migrations from passing the linter unnoticed, especially if SQL generation requires specific database conditions.
- breaking In v3.0.0, the command-line interface for `lintmigrations` underwent a breaking change. The `GIT_COMMIT_ID` positional argument became an optional named argument (`--git-commit-id [GIT_COMMIT_ID]`), and the command now accepts `[app_label]` and `[migration_name]` as new positional arguments.
- gotcha The linter's deep analysis often depends on Django's `sqlmigrate` command, which typically requires an active database connection to generate and inspect the SQL for migration files. Running the linter in an environment without a configured or accessible database can lead to `sqlmigrate` failures, which will crash the linter (since v5.0.0).
- breaking Numerous releases have dropped support for older Python and Django versions. For instance, Python 3.7 and 3.8 were dropped in v5.2.0. Django 1.11, 2.0, 2.1, 3.0, and 3.1 were dropped in v4.0.0, alongside Python 2.7, 3.5, and 3.6.
Install
-
pip install django-migration-linter
Quickstart
# 1. Add 'django_migration_linter' to your INSTALLED_APPS in settings.py:
# INSTALLED_APPS = [
# ...,
# 'django_migration_linter',
# ]
# 2. Run the linter as a Django management command.
# This example compares new migrations against the 'main' Git branch
# and treats any linter warnings as errors (failing the check).
import subprocess
import os
# Ensure your Django settings are configured for manage.py
# (e.g., DJANGO_SETTINGS_MODULE environment variable)
try:
print("Running django-migration-linter...")
subprocess.check_call([
"python", "manage.py", "lintmigrations",
"--git-commit-id", "main",
"--warnings-as-errors"
])
print("\nMigrations linted successfully with no backward incompatible changes detected.")
except subprocess.CalledProcessError as e:
print(f"\nMigration linter failed with exit code {e.returncode}:")
print("Please review the output for backward incompatible changes and fix them.")
except FileNotFoundError:
print("Error: 'python' command not found. Ensure Python is in your PATH and manage.py exists.")