Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 344441c

Browse files
committed
fetch using qset and change query count back
1 parent 147bfb4 commit 344441c

File tree

2 files changed

+17
-13
lines changed

2 files changed

+17
-13
lines changed

entity/sync.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -118,22 +118,26 @@ def _get_super_entities_by_ctype(model_objs_by_ctype, model_ids_to_sync, sync_al
118118
return super_entities_by_ctype
119119

120120

121-
def _fetch_entity_models(model_ids_to_sync, model_objs_map, model_objs_by_ctype):
121+
def _fetch_entity_models(model_ids_to_sync, model_objs_map, model_objs_by_ctype, sync_all):
122122
"""
123123
Fetch the entity models per content type. This will also handle the
124124
case where accounts are created before _get_super_entities_by_ctype and
125125
the model_ids_to_sync do not match the model_objs_map
126126
"""
127127
for ctype, model_ids in model_ids_to_sync.items():
128128

129-
# Build a set of ids of already fetched models
130-
fetched_model_ids = {
131-
model.id
132-
for model in model_objs_by_ctype[ctype]
133-
}
129+
if sync_all:
130+
131+
# Build a set of ids of already fetched models
132+
fetched_model_ids = {
133+
model.id
134+
for model in model_objs_by_ctype[ctype]
135+
}
134136

135-
# Compute the set diff to see if any records are missing
136-
unfetched_model_ids = model_ids - fetched_model_ids
137+
# Compute the set diff to see if any records are missing
138+
unfetched_model_ids = model_ids - fetched_model_ids
139+
else:
140+
unfetched_model_ids = model_ids
137141

138142
# Check if new records
139143
if unfetched_model_ids:
@@ -146,13 +150,13 @@ def _fetch_entity_models(model_ids_to_sync, model_objs_map, model_objs_by_ctype)
146150
model_objs_map[(ctype, model_obj.id)] = model_obj
147151

148152

149-
def _get_model_objs_to_sync(model_ids_to_sync, model_objs_map, model_objs_by_ctype):
153+
def _get_model_objs_to_sync(model_ids_to_sync, model_objs_map, model_objs_by_ctype, sync_all):
150154
"""
151155
Given the model IDs to sync, fetch all model objects to sync
152156
"""
153157
model_objs_to_sync = {}
154158

155-
_fetch_entity_models(model_ids_to_sync, model_objs_map, model_objs_by_ctype)
159+
_fetch_entity_models(model_ids_to_sync, model_objs_map, model_objs_by_ctype, sync_all)
156160

157161
for ctype, model_ids_to_sync_for_ctype in model_ids_to_sync.items():
158162
model_objs_to_sync[ctype] = [
@@ -261,7 +265,7 @@ def sync(self):
261265
# Now that we have all models we need to sync, fetch them so that we can extract
262266
# metadata and entity kinds. If we are syncing all entities, we've already fetched
263267
# everything and can fill in this data struct without doing another DB hit
264-
model_objs_to_sync = _get_model_objs_to_sync(model_ids_to_sync, model_objs_map, model_objs_by_ctype)
268+
model_objs_to_sync = _get_model_objs_to_sync(model_ids_to_sync, model_objs_map, model_objs_by_ctype, sync_all)
265269

266270
# Obtain all entity kind tuples associated with the models
267271
entity_kind_tuples_to_sync = set()

entity/tests/sync_tests.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -954,7 +954,7 @@ def test_optimal_queries_registered_entity_with_no_qset(self):
954954
team_group = G(TeamGroup)
955955

956956
ContentType.objects.clear_cache()
957-
with self.assertNumQueries(14):
957+
with self.assertNumQueries(15):
958958
team_group.save()
959959

960960
def test_optimal_queries_registered_entity_w_qset(self):
@@ -964,7 +964,7 @@ def test_optimal_queries_registered_entity_w_qset(self):
964964
account = G(Account)
965965

966966
ContentType.objects.clear_cache()
967-
with self.assertNumQueries(17):
967+
with self.assertNumQueries(18):
968968
account.save()
969969

970970
def test_sync_all_optimal_queries(self):

0 commit comments

Comments
 (0)