Skip to content

Instantly share code, notes, and snippets.

Embed
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 django.core.management.base import NoArgsCommand
from south.exceptions import NoMigrations
from south.migration import Migrations
from south.creator import changes, actions, freezer
def empty(*args, **kwargs):
pass
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:
try:
# 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):
continue
except Exception as error:
self.stderr.write(str(error))
continue
# 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 settings.py." % 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
try:
action_class = getattr(actions, action_name)
except AttributeError:
raise ValueError("Invalid action name from source: %s" % action_name)
else:
action_class.deal_with_not_null_no_default = empty
action = action_class(**params)
action.add_forwards(forwards_actions)
action.add_backwards(backwards_actions)
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
You can’t perform that action at this time.