40
40
41
41
def migrate_groups_for_tenant (tenant : Tenant , replicator : RelationReplicator ):
42
42
"""Generate user relationships and system role assignments for groups in a tenant."""
43
- groups = tenant .group_set .all ( )
43
+ groups = tenant .group_set .values ( "pk" )
44
44
for group in groups :
45
- principals : list [ Principal ] = []
46
- system_roles : list [ Role ] = []
47
- if not group . platform_default :
48
- principals = group . principals . all ()
49
- if group . system is False and group . admin_default is False :
50
- system_roles = group . roles (). public_tenant_only ()
51
- if any ( True for _ in system_roles ) or any ( True for _ in principals ):
52
- # The migrator deals with concurrency control.
53
- # We need an atomic block because the select_for_update is used in the dual write handler,
54
- # and the group must be locked to add principals to the groups.
55
- # NOTE: The lock on the group is not necessary when adding system roles to the group,
56
- # as the binding mappings are locked during this process to ensure concurrency control.
57
- # Start of transaction for group operations
58
- with transaction . atomic () :
59
- # Lock group
60
- Group . objects . select_for_update (). get ( pk = group . pk )
45
+ # The migrator deals with concurrency control.
46
+ # We need an atomic block because the select_for_update is used in the dual write handler,
47
+ # and the group must be locked to add principals to the groups.
48
+ # NOTE: The lock on the group is not necessary when adding system roles to the group,
49
+ # as the binding mappings are locked during this process to ensure concurrency control.
50
+ # Start of transaction for group operations
51
+ with transaction . atomic ( ):
52
+ # Requery the group with a lock
53
+ group = Group . objects . select_for_update (). get ( pk = group [ "pk" ])
54
+ principals : list [ Principal ] = []
55
+ system_roles : list [ Role ] = []
56
+ if not group . platform_default :
57
+ principals = group . principals . all ()
58
+ if group . system is False and group . admin_default is False :
59
+ system_roles = group . roles (). public_tenant_only ()
60
+ if any ( True for _ in system_roles ) or any ( True for _ in principals ):
61
61
dual_write_handler = RelationApiDualWriteGroupHandler (
62
62
group , ReplicationEventType .MIGRATE_TENANT_GROUPS , replicator = replicator
63
63
)
@@ -68,7 +68,7 @@ def migrate_groups_for_tenant(tenant: Tenant, replicator: RelationReplicator):
68
68
# dual_write_handler
69
69
dual_write_handler .generate_relations_to_add_roles (system_roles )
70
70
dual_write_handler .replicate ()
71
- # End of transaction for group operations, locks are released
71
+ # End of transaction for group operations, locks are released
72
72
73
73
74
74
def migrate_roles_for_tenant (tenant , exclude_apps , replicator ):
0 commit comments