pythondjangopostgresqlmigrationprometheus

Is there a way to check (from code) if django migrations have been executed?


with:

./manage.py showmigrations

one sees the list of executed and pending migrations. I'd like to get this list (or number of migrations) at runtime so that I can expose it via a monitoring tool to see if any migrations failed.

I checked the internals, but can't find any way to expose this for now.


Solution

  • You can use the MigrationRecorder:

    from django.db.migrations.recorder import MigrationRecorder
    
    all_migrations = MigrationRecorder.Migration.objects.all()
    

    Migration is a standard Django model, for example:

    applied_migrations = [migration for migration in all_migrations if migration.applied].

    That should probably be enough to get you started.