Check for pending Django migrations

前端 未结 8 2073
孤独总比滥情好
孤独总比滥情好 2021-02-01 13:05

In Django, is there an easy way to check whether all database migrations have been run? I\'ve found manage.py migrate --list, which gives me the information I want,

8条回答
  •  梦谈多话
    2021-02-01 13:26

    Here is my Python soloution to get some information about the migration-states:

    from io import StringIO  # for Python 2 use from StringIO import StringIO  
    from django.core.management import call_command 
    
    def get_migration_state():
        result = []
        out = StringIO()
        call_command('showmigrations', format="plan", stdout=out)
        out.seek(0)
        for line in out.readlines():
            status, name = line.rsplit(' ', 1)
            result.append((status.strip() == '[X]', name.strip()))
        return result
    

    The result of this function looks like that:

    [(True, 'contenttypes.0001_initial'),
     (True, 'auth.0001_initial'),
     (False, 'admin.0001_initial'),
     (False, 'admin.0002_logentry_remove_auto_add')]
    

    Maybe it helps some of you guys..

提交回复
热议问题