Projet

Général

Profil

0002-provisionning-grab-existing-roles-by-uuid-in-one-que.patch

Emmanuel Cazenave, 22 juin 2021 17:16

Télécharger (2,37 ko)

Voir les différences:

Subject: [PATCH 2/4] provisionning: grab existing roles by uuid in one query
 (#55043)

 hobo/provisionning/utils.py           | 15 +++++++++++++--
 tests_multitenant/test_hobo_notify.py |  2 +-
 2 files changed, 14 insertions(+), 3 deletions(-)
hobo/provisionning/utils.py
109 109
    def provision_role(cls, issuer, action, data, full=False):
110 110
        logger = logging.getLogger(__name__)
111 111
        uuids = set()
112
        roles_by_uuid = dict()
113

  
114
        if action == 'provision':
115
            # first pass to gather existing roles by uuid
116
            target_uuids = set()
117
            for o in data:
118
                assert 'uuid' in o
119
                target_uuids.add(o['uuid'])
120
            for role in Role.objects.filter(uuid__in=target_uuids):
121
                roles_by_uuid[role.uuid] = role
122

  
112 123
        for o in data:
113 124
            assert 'uuid' in o
114 125
            uuids.add(o['uuid'])
......
118 129
                if len(role_name) > 70:
119 130
                    role_name = role_name[:70] + '(...)'
120 131
                try:
121
                    role = Role.objects.get(uuid=o['uuid'])
132
                    role = roles_by_uuid[o['uuid']]
122 133
                    created = False
123
                except Role.DoesNotExist:
134
                except KeyError:
124 135
                    try:
125 136
                        with atomic():
126 137
                            role, created = Role.objects.get_or_create(
tests_multitenant/test_hobo_notify.py
243 243
            }
244 244
            with CaptureQueriesContext(connection) as ctx:
245 245
                Command.process_notification(tenant, notification)
246
                assert len(ctx.captured_queries) == 36
246
                assert len(ctx.captured_queries) == 33
247 247
            assert Group.objects.count() == 2
248 248
            assert Role.objects.count() == 2
249 249

  
250
-