Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Script to view any missing migrations for models. Will not work with native django migrations
# -*- coding: utf-8 -*-
from __future__ import print_function
import sys
from django.conf import settings
from import NoArgsCommand
from south.exceptions import NoMigrations
from south.migration import Migrations
from south.creator import changes, actions, freezer
def empty(*args, **kwargs):
class Command(NoArgsCommand):
can_import_settings = True
help = "Checks for any new model changes"
def handle_noargs(self, **options):
for app in settings.INSTALLED_APPS:
# Get the Migrations for this app (creating the migrations dir if needed)
migrations = Migrations(app, force_creation=False, verbose_creation=1)
last_migration = migrations[-1]
except (NoMigrations, IndexError):
except Exception as error:
# Make sure it has stored models
if migrations.app_label() not in getattr(last_migration.migration_class(), "complete_apps", []):
self.stderr.write("You cannot use automatic detection, since the previous migration does not have this whole app frozen.\nEither make migrations using '--freeze %s' or set 'SOUTH_AUTO_FREEZE_APP = True' in your" % migrations.app_label())
# Alright, construct two model dicts to run the differ on.
old_defs = dict(
(k, v) for k, v in last_migration.migration_class().models.items()
if k.split(".")[0] == migrations.app_label()
new_defs = dict(
(k, v) for k, v in freezer.freeze_apps([migrations.app_label()]).items()
if k.split(".")[0] == migrations.app_label()
change_source = changes.AutoChanges(
migrations = migrations,
old_defs = old_defs,
old_orm = last_migration.orm(),
new_defs = new_defs,
# Get the actions, and then insert them into the actions lists
forwards_actions = []
backwards_actions = []
if change_source:
for action_name, params in change_source.get_changes():
# Run the correct Action class
action_class = getattr(actions, action_name)
except AttributeError:
raise ValueError("Invalid action name from source: %s" % action_name)
action_class.deal_with_not_null_no_default = empty
action = action_class(**params)
print(action.console_line(), file=sys.stderr)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment