Skip to content

Commit 19072ac

Browse files
committed
Improve logging for DAB RBAC migrations
1 parent 27ca162 commit 19072ac

File tree

4 files changed

+29
-5
lines changed

4 files changed

+29
-5
lines changed

ansible_base/rbac/management/__init__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,14 @@ def create_dab_permissions(app_config, verbosity=2, interactive=True, using=DEFA
3030
try:
3131
dab_ct_cls = apps.get_model("dab_rbac", "RoleDefinition")
3232
except LookupError:
33-
logger.warning('Skipping DAB RBAC type and permission creation initial migration has not happened')
33+
if app_config.label == 'dab_rbac':
34+
logger.warning('Skipping DAB RBAC type and permission creation initial migration has not happened')
3435
return
3536

3637
try:
3738
dab_ct_cls = apps.get_model("dab_rbac", "DABContentType")
3839
except LookupError:
39-
logger.info('Running historical permission creation method')
40+
logger.info(f'Running historical permission creation method for {app_config.label} app')
4041
old_create_dab_permissions(app_config, apps=apps)
4142
return
4243

ansible_base/rbac/migrations/0005_remote_permissions_data.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
# Generated by Django 4.2.23 on 2025-06-30 12:48
22

3+
import logging
4+
35
from django.db import migrations
46

57

8+
logger = logging.getLogger(__name__)
9+
10+
611
def create_types_if_needed(apps, schema_editor):
712
"""Before we can migrate to the new DABContentType, entries in that table must be created.
813
@@ -12,6 +17,7 @@ def create_types_if_needed(apps, schema_editor):
1217
permission_cls = apps.get_model('dab_rbac', 'DABPermission')
1318
rd_cls = apps.get_model('dab_rbac', 'RoleDefinition')
1419
if permission_cls.objects.exists() or rd_cls.objects.exists():
20+
logger.info('Running DABContentType creation script as part of 0005 migration')
1521
from ansible_base.rbac.management.create_types import create_DAB_contenttypes
1622

1723
create_DAB_contenttypes(apps=apps)
@@ -22,6 +28,7 @@ def migrate_content_type(apps, schema_editor):
2228
ct_cls.objects.clear_cache()
2329
for model_name in ('dabpermission', 'objectrole', 'roledefinition', 'roleuserassignment', 'roleteamassignment'):
2430
cls = apps.get_model('dab_rbac', model_name)
31+
update_ct = 0
2532
for obj in cls.objects.all():
2633
old_ct = obj.content_type
2734
if old_ct:
@@ -33,16 +40,23 @@ def migrate_content_type(apps, schema_editor):
3340
f"Failed to get new content type for a {model_name} pk={obj.pk}, obj={obj.__dict__}"
3441
) from e
3542
obj.save()
43+
update_ct += 1
44+
if update_ct:
45+
logger.info(f'Updated content_type reference to new model for {model_name} for {update_ct} entries')
3646
for model_name in ('roleevaluation', 'roleevaluationuuid'):
3747
cls = apps.get_model('dab_rbac', model_name)
3848
cls.objects.all().delete()
3949

4050
# DABPermission model had api_slug added in last migration
4151
# if records existed before this point, it needs to be filled in
52+
mod_ct = 0
4253
permission_cls = apps.get_model('dab_rbac', 'DABPermission')
4354
for permission in permission_cls.objects.all():
4455
permission.api_slug = f'{permission.new_content_type.service}.{permission.codename}'
4556
permission.save()
57+
mod_ct += 1
58+
if mod_ct:
59+
logger.info(f'Set new field DABPermission.api_slug for {mod_ct} existing permissions')
4660

4761

4862
class Migration(migrations.Migration):

ansible_base/rbac/migrations/0006_remote_data_reverse.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
# Generated by Django 4.2.23 on 2025-06-30 12:48
22

3+
import logging
4+
35
from django.db import migrations
46

57

8+
logger = logging.getLogger(__name__)
9+
10+
611
def migrate_content_type_forward_noop(apps, schema_editor):
712
"""Forward no-op: data migration already happened in 0005."""
813
pass
@@ -19,19 +24,23 @@ def migrate_content_type_reverse(apps, schema_editor):
1924

2025
for model_name in ('dabpermission', 'objectrole', 'roledefinition', 'roleuserassignment', 'roleteamassignment'):
2126
cls = apps.get_model('dab_rbac', model_name)
27+
update_ct = 0
2228
for obj in cls.objects.all():
2329
new_ct = obj.new_content_type
2430
if new_ct:
2531
try:
2632
# Find the corresponding Django ContentType by app_label and model
2733
django_ct = django_ct_cls.objects.get(app_label=new_ct.app_label, model=new_ct.model)
28-
obj.content_type = django_ct
29-
obj.save()
3034
except Exception as e:
3135
raise RuntimeError(
3236
f"Failed to get Django content type for a {model_name} pk={obj.pk}, "
3337
f"DABContentType app_label='{new_ct.app_label}', model='{new_ct.model}'"
3438
) from e
39+
obj.content_type = django_ct
40+
obj.save()
41+
update_ct += 1
42+
if update_ct:
43+
logger.info(f'Reverse-migrated content_type reference to old model for {model_name} for {update_ct} entries')
3544

3645

3746
class Migration(migrations.Migration):

ansible_base/rbac/triggers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ def rbac_post_user_delete(instance, *args, **kwargs):
276276

277277
def post_migration_rbac_setup(sender, *args, **kwargs):
278278
if not migrations_are_complete():
279-
logger.info('Not running DAB RBAC post_migrate logic because of suspected reverse migration')
279+
logger.info('Not running DAB RBAC post_migrate logic because of incomplete migration')
280280
return
281281

282282
dab_post_migrate.send(sender=sender)

0 commit comments

Comments
 (0)