Skip to content

Commit 7873a51

Browse files
Merge pull request #4699 from specify/issue-4685
Fixes agent merging failing unexpectedly
2 parents ef2dc27 + 109e1a2 commit 7873a51

File tree

2 files changed

+15
-14
lines changed

2 files changed

+15
-14
lines changed

specifyweb/specify/api_tests.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,8 @@ def test_replace_agents(self):
584584
agent=agent_1,
585585
collectingevent=collecting_event
586586
)
587-
587+
self.collectionobjects[0].cataloger = agent_1
588+
self.collectionobjects[0].save()
588589
# Assert that the api request ran successfully
589590
response = c.post(
590591
f'/api/specify/agent/replace/{agent_2.id}/',

specifyweb/specify/record_merging.py

+13-13
Original file line numberDiff line numberDiff line change
@@ -192,18 +192,6 @@ def record_merge_fx(model_name: str, old_model_ids: List[int], new_model_id: int
192192
except ValueError:
193193
continue
194194

195-
# Handle case of updating a large amount of record ids in a foreign table.
196-
# Example: handle case of updating a large amount of agent ids in the audit logs.
197-
# Fix by optimizing the query by consolidating it here
198-
if model_name.lower() in MERGING_OPTIMIZATION_FIELDS and \
199-
table_name.lower() in MERGING_OPTIMIZATION_FIELDS[model_name.lower()]:
200-
for field_name in MERGING_OPTIMIZATION_FIELDS[model_name.lower()][table_name.lower()]:
201-
query = Q(**{field_name: old_model_ids[0]})
202-
for old_model_id in old_model_ids[1:]:
203-
query.add(Q(**{field_name: old_model_id}), Q.OR)
204-
foreign_model.objects.filter(query).update(**{field_name: new_model_id})
205-
progress(1, 0) if progress is not None else None
206-
continue
207195

208196
apply_order = add_ordering_to_key(table_name.lower().title())
209197
# BUG: timestampmodified could be null for one record, and not the other
@@ -221,6 +209,18 @@ def record_merge_fx(model_name: str, old_model_ids: List[int], new_model_id: int
221209
field_name_id = f'{field_name}_id'
222210
if not hasattr(foreign_model, field_name_id):
223211
continue
212+
# Handle case of updating a large amount of record ids in a foreign table.
213+
# Example: handle case of updating a large amount of agent ids in the audit logs.
214+
# Fix by optimizing the query by consolidating it here
215+
if model_name.lower() in MERGING_OPTIMIZATION_FIELDS and \
216+
table_name.lower() in MERGING_OPTIMIZATION_FIELDS[model_name.lower()]:
217+
if field_name_id in MERGING_OPTIMIZATION_FIELDS[model_name.lower()][table_name.lower()]:
218+
query = Q(**{field_name_id: old_model_ids[0]})
219+
for old_model_id in old_model_ids[1:]:
220+
query.add(Q(**{field_name_id: old_model_id}), Q.OR)
221+
foreign_model.objects.filter(query).update(**{field_name_id: new_model_id})
222+
progress(1, 0) if progress is not None else None
223+
continue
224224

225225
# Filter the objects in the foreign model that references the old target model
226226
foreign_objects = filter_and_lock_target_objects(foreign_model, old_model_ids, field_name_id)
@@ -308,7 +308,7 @@ def update_record(record: models.Model):
308308

309309
response: http.HttpResponse = update_record(obj)
310310
if response is not None and response.status_code != 204:
311-
return response
311+
raise FailedMergingException(response)
312312

313313
# Dedupe by deleting the record that is being replaced and updating the old model ID to the new one
314314
for old_model_id in old_model_ids:

0 commit comments

Comments
 (0)