Skip to content

Commit fcfb2c2

Browse files
committed
Fix migration corner case
1 parent 894af11 commit fcfb2c2

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

ansible_base/rbac/managed.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@ def get_content_type(self, apps):
4242
# NOTE: this is subject to major migration-related hazards
4343
try:
4444
content_type_cls = apps.get_model('dab_rbac', 'DABContentType')
45+
if not content_type_cls.objects.exists():
46+
# Extra special migration hazard
47+
# an old app migration file is trying to create managed roles with current models
48+
# but permissions are not yet created
49+
from ansible_base.rbac.management import sync_dab_permissions
50+
51+
sync_dab_permissions(apps=apps)
4552
except LookupError:
4653
content_type_cls = apps.get_model('contenttypes', 'ContentType')
4754
return content_type_cls.objects.get_for_model(model)

ansible_base/rbac/management/_old.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ def create_dab_permissions(app_config, verbosity=2, interactive=True, using=DEFA
1616
this will only create permissions for registered models
1717
"""
1818
if not getattr(app_config, 'models_module', None):
19+
logger.info(f'Not running permission creation method for {app_config.label} because it has no models')
1920
return
2021

2122
# exit early if nothing is registered for this app
@@ -39,10 +40,12 @@ def create_dab_permissions(app_config, verbosity=2, interactive=True, using=DEFA
3940
app_config = apps.get_app_config(app_label)
4041
ContentType = apps.get_model("contenttypes", "ContentType")
4142
Permission = apps.get_model("dab_rbac", "DABPermission")
42-
except LookupError:
43+
except LookupError as exc:
44+
logger.info(f'Not running create_dab_permissions because model not available {str(exc)}')
4345
return
4446

4547
if not router.allow_migrate_model(using, Permission):
48+
logger.info('Not running create_dab_permissions because permission model does not allow migrating')
4649
return
4750

4851
# This will hold the permissions we're looking for as (content_type, (codename, name))

0 commit comments

Comments
 (0)