mirror of
https://github.com/ansible/awx.git
synced 2026-03-19 18:07:33 -02:30
[AAP-48771]wfjt migration to catch renaming (#6991)
* wfjt migration to catch renaming * Added rename_wfjt function to template constraint migration * Add test to add duplicate names and verify that the duplicates are renamed * move object creation * add missing rename_wfjt operation * fix linter issues * fix tox issues * test manually and move operation * added back credential type validation code
This commit is contained in:
@@ -26,6 +26,11 @@ def change_inventory_source_org_unique(apps, schema_editor):
|
|||||||
logger.info(f'Set database constraint rule for {r} inventory source objects')
|
logger.info(f'Set database constraint rule for {r} inventory source objects')
|
||||||
|
|
||||||
|
|
||||||
|
def rename_wfjt(apps, schema_editor):
|
||||||
|
cls = apps.get_model('main', 'WorkflowJobTemplate')
|
||||||
|
_rename_duplicates(cls)
|
||||||
|
|
||||||
|
|
||||||
class Migration(migrations.Migration):
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
dependencies = [
|
dependencies = [
|
||||||
@@ -40,6 +45,7 @@ class Migration(migrations.Migration):
|
|||||||
name='org_unique',
|
name='org_unique',
|
||||||
field=models.BooleanField(blank=True, default=True, editable=False, help_text='Used internally to selectively enforce database constraint on name'),
|
field=models.BooleanField(blank=True, default=True, editable=False, help_text='Used internally to selectively enforce database constraint on name'),
|
||||||
),
|
),
|
||||||
|
migrations.RunPython(rename_wfjt, migrations.RunPython.noop),
|
||||||
migrations.RunPython(change_inventory_source_org_unique, migrations.RunPython.noop),
|
migrations.RunPython(change_inventory_source_org_unique, migrations.RunPython.noop),
|
||||||
migrations.AddConstraint(
|
migrations.AddConstraint(
|
||||||
model_name='unifiedjobtemplate',
|
model_name='unifiedjobtemplate',
|
||||||
|
|||||||
@@ -110,21 +110,35 @@ class TestMigrationSmoke:
|
|||||||
# Test create a Project with a duplicate name
|
# Test create a Project with a duplicate name
|
||||||
Organization = new_state.apps.get_model('main', 'Organization')
|
Organization = new_state.apps.get_model('main', 'Organization')
|
||||||
Project = new_state.apps.get_model('main', 'Project')
|
Project = new_state.apps.get_model('main', 'Project')
|
||||||
|
WorkflowJobTemplate = new_state.apps.get_model('main', 'WorkflowJobTemplate')
|
||||||
org = Organization.objects.create(name='duplicate-obj-organization', created=now(), modified=now())
|
org = Organization.objects.create(name='duplicate-obj-organization', created=now(), modified=now())
|
||||||
proj_ids = []
|
proj_ids = []
|
||||||
for i in range(3):
|
for i in range(3):
|
||||||
proj = Project.objects.create(name='duplicate-project-name', organization=org, created=now(), modified=now())
|
proj = Project.objects.create(name='duplicate-project-name', organization=org, created=now(), modified=now())
|
||||||
proj_ids.append(proj.id)
|
proj_ids.append(proj.id)
|
||||||
|
|
||||||
|
# Test create WorkflowJobTemplate with duplicate names
|
||||||
|
wfjt_ids = []
|
||||||
|
for i in range(3):
|
||||||
|
wfjt = WorkflowJobTemplate.objects.create(name='duplicate-workflow-name', organization=org, created=now(), modified=now())
|
||||||
|
wfjt_ids.append(wfjt.id)
|
||||||
|
|
||||||
# The uniqueness rules will not apply to InventorySource
|
# The uniqueness rules will not apply to InventorySource
|
||||||
Inventory = new_state.apps.get_model('main', 'Inventory')
|
Inventory = new_state.apps.get_model('main', 'Inventory')
|
||||||
InventorySource = new_state.apps.get_model('main', 'InventorySource')
|
InventorySource = new_state.apps.get_model('main', 'InventorySource')
|
||||||
inv = Inventory.objects.create(name='migration-test-inv', organization=org, created=now(), modified=now())
|
inv = Inventory.objects.create(name='migration-test-inv', organization=org, created=now(), modified=now())
|
||||||
InventorySource.objects.create(name='migration-test-src', source='file', inventory=inv, organization=org, created=now(), modified=now())
|
InventorySource.objects.create(name='migration-test-src', source='file', inventory=inv, organization=org, created=now(), modified=now())
|
||||||
|
|
||||||
|
# Apply migration 0200 which should rename duplicates
|
||||||
new_state = migrator.apply_tested_migration(
|
new_state = migrator.apply_tested_migration(
|
||||||
('main', '0200_template_name_constraint'),
|
('main', '0200_template_name_constraint'),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# Get the models from the new state for verification
|
||||||
|
Project = new_state.apps.get_model('main', 'Project')
|
||||||
|
WorkflowJobTemplate = new_state.apps.get_model('main', 'WorkflowJobTemplate')
|
||||||
|
InventorySource = new_state.apps.get_model('main', 'InventorySource')
|
||||||
|
|
||||||
for i, proj_id in enumerate(proj_ids):
|
for i, proj_id in enumerate(proj_ids):
|
||||||
proj = Project.objects.get(id=proj_id)
|
proj = Project.objects.get(id=proj_id)
|
||||||
if i == 0:
|
if i == 0:
|
||||||
@@ -133,11 +147,18 @@ class TestMigrationSmoke:
|
|||||||
assert proj.name != 'duplicate-project-name'
|
assert proj.name != 'duplicate-project-name'
|
||||||
assert proj.name.startswith('duplicate-project-name')
|
assert proj.name.startswith('duplicate-project-name')
|
||||||
|
|
||||||
|
# Verify WorkflowJobTemplate duplicates are renamed
|
||||||
|
for i, wfjt_id in enumerate(wfjt_ids):
|
||||||
|
wfjt = WorkflowJobTemplate.objects.get(id=wfjt_id)
|
||||||
|
if i == 0:
|
||||||
|
assert wfjt.name == 'duplicate-workflow-name'
|
||||||
|
else:
|
||||||
|
assert wfjt.name != 'duplicate-workflow-name'
|
||||||
|
assert wfjt.name.startswith('duplicate-workflow-name')
|
||||||
|
|
||||||
# The inventory source had this field set to avoid the constrains
|
# The inventory source had this field set to avoid the constrains
|
||||||
InventorySource = new_state.apps.get_model('main', 'InventorySource')
|
|
||||||
inv_src = InventorySource.objects.get(name='migration-test-src')
|
inv_src = InventorySource.objects.get(name='migration-test-src')
|
||||||
assert inv_src.org_unique is False
|
assert inv_src.org_unique is False
|
||||||
Project = new_state.apps.get_model('main', 'Project')
|
|
||||||
for proj in Project.objects.all():
|
for proj in Project.objects.all():
|
||||||
assert proj.org_unique is True
|
assert proj.org_unique is True
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user