Skip to content

Instantly share code, notes, and snippets.

@miraculixx
Created February 4, 2014 08:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save miraculixx/85c33dc7f7561b2e5fda to your computer and use it in GitHub Desktop.
Save miraculixx/85c33dc7f7561b2e5fda to your computer and use it in GitHub Desktop.
Get latest django-south migrations per app
def get_latest_migrations():
"""
using South' MigrationHistory, get the latest migration id per app
using this, we can apply a --fake later on to get back the previous
state
"""
from south.models import MigrationHistory
from django.db.models.aggregates import Max
latest = MigrationHistory.objects.values('app_name').annotate(latest=Max('applied'))
migrations = {}
for l in latest:
m = MigrationHistory.objects.filter(app_name=l['app_name']).filter(applied=l['latest'])
mid = m[0].migration.split('_')[0]
migrations[l['app_name']] = mid
return migrations
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment