Django Migration Linter

6.0.0 · active · verified Sun Apr 12

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

Install

Quickstart

To quickly use `django-migration-linter`, add it to your `INSTALLED_APPS` and then execute the `lintmigrations` management command. The example demonstrates how to run the linter to compare migrations in your current branch against the 'main' branch, ensuring that any detected warnings will cause the command to fail. This is typically run in CI/CD pipelines.

# 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.")

view raw JSON →