Fix 3.0 to 3.2 migration paths

This commit is contained in:
Wayne Witzel III
2017-10-31 00:46:33 -04:00
parent 84fb908261
commit fc56a1c170
3 changed files with 79 additions and 51 deletions

View File

@@ -2,20 +2,21 @@ from itertools import chain
from django.db import (
connection,
migrations,
OperationalError,
)
def squash_data(SQUASHED):
def squash_data(squashed):
'''Returns a tuple of the squashed_keys and the key position to begin
processing replace and operation lists'''
cm = current_migration()
squashed_keys = sorted(SQUASHED.keys())
squashed_keys = sorted(squashed.keys())
if cm is None:
return squashed_keys, 0
try:
if cm is None:
key_index = 0
else:
key_index = squashed_keys.index(cm.name) + 1
key_index = squashed_keys.index(cm.name) + 1
except ValueError:
key_index = 0
return squashed_keys, key_index
@@ -26,25 +27,25 @@ def current_migration():
try:
recorder = migrations.recorder.MigrationRecorder(connection)
return recorder.migration_qs.filter(app='main').exclude(name__contains='squashed').latest('id')
except recorder.Migration.DoesNotExist:
except (recorder.Migration.DoesNotExist, OperationalError):
return None
def replaces(SQUASHED):
def replaces(squashed):
'''Build a list of replacement migrations based on the most recent non-squashed migration
and the provided list of SQUASHED migrations. If the most recent non-squashed migration
is not present anywhere in the SQUASHED dictionary, assume they have all been applied.
'''
squashed_keys, key_index = squash_data(SQUASHED)
squashed_keys, key_index = squash_data(squashed)
return [(b'main', key) for key in squashed_keys[key_index:]]
def operations(SQUASHED):
def operations(squashed):
'''Build a list of migration operations based on the most recent non-squashed migration
and the provided list of SQUASHED migrations. If the most recent non-squashed migration
is not present anywhere in the SQUASHED dictionary, assume they have all been applied.
and the provided list of squashed migrations. If the most recent non-squashed migration
is not present anywhere in the `squashed` dictionary, assume they have all been applied.
'''
squashed_keys, key_index = squash_data(SQUASHED)
squashed_keys, key_index = squash_data(squashed)
op_keys = squashed_keys[key_index:]
ops = [SQUASHED[op_key] for op_key in op_keys]
ops = [squashed[op_key] for op_key in op_keys]
return [op for op in chain.from_iterable(ops)]