I'm trying to write a pre-commit hook to my Django project that checks for missing migrations. That is, it ensures all changes are reflected in a migrations file.
One way to implement this is to PASS the pre-commit hook if the makemigrations
command returns no changes.
$ ./manage.py makemigrations --dry-run
No changes detected
And to FAIL the pre-commit hook if it returns something:
$ ./manage.py makemigrations --dry-run
Migrations for 'myapp':
myapp/migrations/0003_auto_20201213_2233.py
- Alter field type on event
How can I write this pre-commit hook? Is there a better approach than using makemigrations
? This is what I have so far but it always passes (I think I need to parse the response):
repos:
- repo: local
hooks:
- id: pre-commit-django-migrations
name: Check django migrations
entry: ./manage.py makemigrations --dry-run
language: system
types: [python]
pass_filenames: false
From Django makemigrations
documentation
--check
Makes makemigrations exit with a non-zero status when model changes without migrations are detected.
So you can use --check
along with --dry-run
:
entry: python manage.py makemigrations --check --dry-run