@@ -192,18 +192,6 @@ def record_merge_fx(model_name: str, old_model_ids: List[int], new_model_id: int
192
192
except ValueError :
193
193
continue
194
194
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
207
195
208
196
apply_order = add_ordering_to_key (table_name .lower ().title ())
209
197
# 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
221
209
field_name_id = f'{ field_name } _id'
222
210
if not hasattr (foreign_model , field_name_id ):
223
211
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
224
224
225
225
# Filter the objects in the foreign model that references the old target model
226
226
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):
308
308
309
309
response : http .HttpResponse = update_record (obj )
310
310
if response is not None and response .status_code != 204 :
311
- return response
311
+ raise FailedMergingException ( response )
312
312
313
313
# Dedupe by deleting the record that is being replaced and updating the old model ID to the new one
314
314
for old_model_id in old_model_ids :
0 commit comments