diff --git a/awx/main/migrations/0003_squashed_v300_v303_updates.py b/awx/main/migrations/0003_squashed_v300_v303_updates.py index 82d781ec85..661debec98 100644 --- a/awx/main/migrations/0003_squashed_v300_v303_updates.py +++ b/awx/main/migrations/0003_squashed_v300_v303_updates.py @@ -8,14 +8,9 @@ from __future__ import unicode_literals from django.db import migrations, models from django.conf import settings import awx.main.fields -import jsonfield.fields - -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() +import _squashed +from _squashed_300 import SQUASHED_300 class Migration(migrations.Migration): @@ -27,12 +22,7 @@ class Migration(migrations.Migration): (b'main', '0025_v300_update_rbac_parents'), (b'main', '0026_v300_credential_unique'), (b'main', '0027_v300_team_migrations'), - (b'main', '0028_v300_org_team_cascade'), - (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'),] + (b'main', '0028_v300_org_team_cascade')] + _squashed.replaces(SQUASHED_300) dependencies = [ @@ -130,27 +120,4 @@ class Migration(migrations.Migration): field=models.ForeignKey(related_name='teams', to='main.Organization'), preserve_default=False, ), - # add ask skip tags - 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'), - ), - ] + ] + _squashed.operations(SQUASHED_300) diff --git a/awx/main/migrations/_squashed.py b/awx/main/migrations/_squashed.py index 8344f8b6ed..709a0a8869 100644 --- a/awx/main/migrations/_squashed.py +++ b/awx/main/migrations/_squashed.py @@ -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)] diff --git a/awx/main/migrations/_squashed_300.py b/awx/main/migrations/_squashed_300.py new file mode 100644 index 0000000000..904c036db1 --- /dev/null +++ b/awx/main/migrations/_squashed_300.py @@ -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']