Fix makemigrations bug detected by @DiogoMarques29 when Page has

dependencies
This commit is contained in:
Dario Marcelino 2017-12-28 16:44:23 +00:00
parent bd93e06adc
commit 271920d6eb

View file

@ -1,18 +1,30 @@
from django.core.management.commands.makemigrations import Command as MakeMigrationsCommand
from django.db.migrations.autodetector import MigrationAutodetector
import copy
# decorate MigrationAutodetector.changes so we can silently remove wagtailcore changes
def changes_decorator(func):
def wrapper(self, graph, trim_to_apps=None, convert_apps=None, migration_name=None):
changes = func(self, graph, trim_to_apps, convert_apps, migration_name)
if 'wagtailcore' in changes:
del changes['wagtailcore']
return changes
def autodetector_decorator(func):
def wrapper(self, from_state, to_state, questioner=None):
# Replace to_state.app_configs.models and to_state.models' version of page with the old one
# so no changes are detected by MigrationAutodetector
from_state_page = from_state.concrete_apps.get_model('wagtailcore', 'page')
new_to_state = copy.deepcopy(to_state)
new_to_state.apps.app_configs['wagtailcore'].models['page'] = from_state_page
new_to_state.models['wagtailcore', 'page'] = from_state.models['wagtailcore', 'page']
return func(self, from_state, new_to_state, questioner)
return wrapper
MigrationAutodetector.changes = changes_decorator(MigrationAutodetector.changes)
class Command(MakeMigrationsCommand):
help = "Creates new migration(s) for apps except wagtailcore."
help = "Creates new migration(s) for apps except wagtailcore's Page."
def handle(self, *args, **options):
old_autodetector_init = MigrationAutodetector.__init__
MigrationAutodetector.__init__ = autodetector_decorator(MigrationAutodetector.__init__)
try:
super(Command, self).handle(*args, **options)
finally:
MigrationAutodetector.__init__ = old_autodetector_init