11# Generated by Django 4.2.23 on 2025-06-30 12:48
22
3+ import logging
4+
35from django .db import migrations
46
57
8+ logger = logging .getLogger (__name__ )
9+
10+
611def 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
4862class Migration (migrations .Migration ):
0 commit comments