Skip to content

Commit 1bcb905

Browse files
committed
Use primary keys for role and group queries
1 parent 96f1562 commit 1bcb905

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

rbac/migration_tool/migrate.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939

4040
def migrate_groups_for_tenant(tenant: Tenant, replicator: RelationReplicator):
4141
"""Generate user relationships and system role assignments for groups in a tenant."""
42-
groups = tenant.group_set.values("pk")
42+
groups = tenant.group_set.only("pk").values("pk")
4343
for group in groups:
4444
# The migrator deals with concurrency control.
4545
# We need an atomic block because the select_for_update is used in the dual write handler,
@@ -72,16 +72,16 @@ def migrate_groups_for_tenant(tenant: Tenant, replicator: RelationReplicator):
7272

7373
def migrate_roles_for_tenant(tenant, exclude_apps, replicator):
7474
"""Migrate all roles for a given tenant."""
75-
roles = tenant.role_set.all()
75+
roles = tenant.role_set.only("pk")
7676
if exclude_apps:
7777
roles = roles.exclude(access__permission__application__in=exclude_apps)
78-
79-
for role in roles:
78+
role_pks = roles.values_list("pk", flat=True)
79+
for role in role_pks:
8080
# The migrator deals with concurrency control and roles needs to be locked.
8181
with transaction.atomic():
82-
logger.info(f"Migrating role: {role.name} with UUID {role.uuid}.")
8382
# Requery and lock role
84-
role = Role.objects.select_for_update().get(pk=role.pk)
83+
role = Role.objects.select_for_update().get(pk=role)
84+
logger.info(f"Migrating role: {role.name} with UUID {role.uuid}.")
8585
dual_write_handler = RelationApiDualWriteHandler(
8686
role, ReplicationEventType.MIGRATE_CUSTOM_ROLE, replicator
8787
)

0 commit comments

Comments
 (0)