Check for pending Django migrations
Shell
The only simple solution I've found so far is running
./manage.py showmigrations | grep '\[ \]'
which will output an empty string in case all migrations have been applied.
However, it is closely tied to the output format.
Python
I checked the source code of migrate
command and it seems like this should do the trick:
from django.db.migrations.executor import MigrationExecutorfrom django.db import connections, DEFAULT_DB_ALIASdef is_database_synchronized(database): connection = connections[database] connection.prepare_database() executor = MigrationExecutor(connection) targets = executor.loader.graph.leaf_nodes() return not executor.migration_plan(targets)# Usage example.if is_database_synchronized(DEFAULT_DB_ALIAS): # All migrations have been applied. passelse: # Unapplied migrations found. pass
1.10 release notes:
The new
makemigrations --check
option makes the command exit with a non-zero status when model changes without migrations are detected.
If you don't want to create the migrations, combine it with --dry-run
:
python manage.py makemigrations --check --dry-run
Note that this doesn't check whether the migrations were applied, it only checks whether the migration files were created.
Try,
python manage.py migrate --list | grep "\[ \]\|^[a-z]" | grep "[ ]" -B 1
returns,
<app_1> [ ] 0001_initial [ ] 0002_auto_01201244 [ ] 0003_auto_12334333<app_2> [ ] 0031_auto_12344544 [ ] 0032_auto_45456767 [ ] 0033_auto_23346566<app_3> [ ] 0008_auto_3446677
Update:
If you have updated Django version >= 1.11
, use below command,
python manage.py showmigrations | grep '\[ \]\|^[a-z]' | grep '[ ]' -B 1