Skip to content

Instantly share code, notes, and snippets.

Created January 23, 2014 21:16
Show Gist options
  • Save anonymous/8586982 to your computer and use it in GitHub Desktop.
Save anonymous/8586982 to your computer and use it in GitHub Desktop.
Southmageddon - automatically nuke and recreate Django South migrations when deployed
from django.core.management.base import BaseCommand
from south.models import MigrationHistory
class Command(BaseCommand):
args = ''
helps = 'Destroys South database migration history - DANGER, WILL ROBINSON'
def handle(self, *args, **options):
MigrationHistory.objects.all().delete()
from django.core.management.base import BaseCommand
from south.models import MigrationHistory
from django.conf import settings
from os.path import join, dirname, abspath, exists
import sys
class Command(BaseCommand):
args = ''
helps = 'Displays whether the South database has migrations not on disk'
def handle(self, *args, **options):
APPS = [..apps to check migrations for..]
has_ghosts = False
for m in MigrationHistory.objects.filter(app_name__in=APPS):
fname = abspath(join(dirname(__file__), '..', '..', '..',
'%s/migrations/%s.py' % (m.app_name, m.migration)))
if not exists(fname):
has_ghosts = True
sys.exit(0 if has_ghosts else -1)
#!/bin/bash
set -e
if python manage.py has_old_migrations; then
echo "Old migrations detected, cleaning up ..."
MYSELF=$(git log -n 1 '--pretty=format:%H' southmageddon.sh)
HEAD=$(git rev-parse --abbrev-ref HEAD)
if [ "$HEAD" = "HEAD" ]; then
HEAD=$(git rev-list HEAD --max-count=1)
fi
LAST_OLD_MIGRATIONS=$(git cat-file commit $MYSELF | grep '^parent ' | cut -f2 -d' ')
echo "Checking out $LAST_OLD_MIGRATIONS ..."
git checkout -q $LAST_OLD_MIGRATIONS
echo "Running latest migrations ..."
python manage.py migrate
echo "Checking out southmageddon code ($MYSELF) ..."
git checkout -q $MYSELF
echo "Running manage.py delete_south_history"
python manage.py delete_south_history
echo "Running fake initial migrations ..."
python manage.py migrate --fake
echo "Checking out current code ($HEAD) ..."
git checkout $HEAD
echo "Cleanup done."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment