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

@@ -8,14 +8,9 @@ from __future__ import unicode_literals
from django.db import migrations, models from django.db import migrations, models
from django.conf import settings from django.conf import settings
import awx.main.fields import awx.main.fields
import jsonfield.fields
import _squashed
def update_dashed_host_variables(apps, schema_editor): from _squashed_300 import SQUASHED_300
Host = apps.get_model('main', 'Host')
for host in Host.objects.filter(variables='---'):
host.variables = ''
host.save()
class Migration(migrations.Migration): class Migration(migrations.Migration):
@@ -27,12 +22,7 @@ class Migration(migrations.Migration):
(b'main', '0025_v300_update_rbac_parents'), (b'main', '0025_v300_update_rbac_parents'),
(b'main', '0026_v300_credential_unique'), (b'main', '0026_v300_credential_unique'),
(b'main', '0027_v300_team_migrations'), (b'main', '0027_v300_team_migrations'),
(b'main', '0028_v300_org_team_cascade'), (b'main', '0028_v300_org_team_cascade')] + _squashed.replaces(SQUASHED_300)
(b'main', '0029_v302_add_ask_skip_tags'),
(b'main', '0030_v302_job_survey_passwords'),
(b'main', '0031_v302_migrate_survey_passwords'),
(b'main', '0032_v302_credential_permissions_update'),
(b'main', '0033_v303_v245_host_variable_fix'),]
dependencies = [ dependencies = [
@@ -130,27 +120,4 @@ class Migration(migrations.Migration):
field=models.ForeignKey(related_name='teams', to='main.Organization'), field=models.ForeignKey(related_name='teams', to='main.Organization'),
preserve_default=False, preserve_default=False,
), ),
# add ask skip tags ] + _squashed.operations(SQUASHED_300)
migrations.AddField(
model_name='jobtemplate',
name='ask_skip_tags_on_launch',
field=models.BooleanField(default=False),
),
# job survery passwords
migrations.AddField(
model_name='job',
name='survey_passwords',
field=jsonfield.fields.JSONField(default={}, editable=False, blank=True),
),
# RBAC credential permission updates
migrations.AlterField(
model_name='credential',
name='admin_role',
field=awx.main.fields.ImplicitRoleField(related_name='+', parent_role=[b'singleton:system_administrator', b'organization.admin_role'], to='main.Role', null=b'True'),
),
migrations.AlterField(
model_name='credential',
name='use_role',
field=awx.main.fields.ImplicitRoleField(related_name='+', parent_role=[b'admin_role'], to='main.Role', null=b'True'),
),
]

View File

@@ -2,20 +2,21 @@ from itertools import chain
from django.db import ( from django.db import (
connection, connection,
migrations, migrations,
OperationalError,
) )
def squash_data(SQUASHED): def squash_data(squashed):
'''Returns a tuple of the squashed_keys and the key position to begin '''Returns a tuple of the squashed_keys and the key position to begin
processing replace and operation lists''' processing replace and operation lists'''
cm = current_migration() cm = current_migration()
squashed_keys = sorted(SQUASHED.keys()) squashed_keys = sorted(squashed.keys())
if cm is None:
return squashed_keys, 0
try: try:
if cm is None: key_index = squashed_keys.index(cm.name) + 1
key_index = 0
else:
key_index = squashed_keys.index(cm.name) + 1
except ValueError: except ValueError:
key_index = 0 key_index = 0
return squashed_keys, key_index return squashed_keys, key_index
@@ -26,25 +27,25 @@ def current_migration():
try: try:
recorder = migrations.recorder.MigrationRecorder(connection) recorder = migrations.recorder.MigrationRecorder(connection)
return recorder.migration_qs.filter(app='main').exclude(name__contains='squashed').latest('id') return recorder.migration_qs.filter(app='main').exclude(name__contains='squashed').latest('id')
except recorder.Migration.DoesNotExist: except (recorder.Migration.DoesNotExist, OperationalError):
return None return None
def replaces(SQUASHED): def replaces(squashed):
'''Build a list of replacement migrations based on the most recent non-squashed migration '''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 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. 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:]] 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 '''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 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. 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:] 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)] return [op for op in chain.from_iterable(ops)]

View File

@@ -0,0 +1,60 @@
from django.db import (
migrations,
models,
)
import jsonfield.fields
import awx.main.fields
from awx.main.migrations import _save_password_keys
from awx.main.migrations import _migration_utils as migration_utils
def update_dashed_host_variables(apps, schema_editor):
Host = apps.get_model('main', 'Host')
for host in Host.objects.filter(variables='---'):
host.variables = ''
host.save()
SQUASHED_300 = {
'0029_v302_add_ask_skip_tags': [
# add ask skip tags
migrations.AddField(
model_name='jobtemplate',
name='ask_skip_tags_on_launch',
field=models.BooleanField(default=False),
),
],
'0030_v302_job_survey_passwords': [
# job survery passwords
migrations.AddField(
model_name='job',
name='survey_passwords',
field=jsonfield.fields.JSONField(default={}, editable=False, blank=True),
),
],
'0031_v302_migrate_survey_passwords': [
migrations.RunPython(migration_utils.set_current_apps_for_migrations),
migrations.RunPython(_save_password_keys.migrate_survey_passwords),
],
'0032_v302_credential_permissions_update': [
# RBAC credential permission updates
migrations.AlterField(
model_name='credential',
name='admin_role',
field=awx.main.fields.ImplicitRoleField(related_name='+', parent_role=[b'singleton:system_administrator', b'organization.admin_role'], to='main.Role', null=b'True'),
),
migrations.AlterField(
model_name='credential',
name='use_role',
field=awx.main.fields.ImplicitRoleField(related_name='+', parent_role=[b'admin_role'], to='main.Role', null=b'True'),
),
],
'0033_v303_v245_host_variable_fix': [
migrations.RunPython(migration_utils.set_current_apps_for_migrations),
migrations.RunPython(update_dashed_host_variables),
],
}
__all__ = ['SQUASHED_300']