@@ -324,19 +324,24 @@ def champ_upsert_by!(type_de_champ, row_id)
324
324
# FIXME: This is a temporary on-demand migration. It will be removed once the full migration is over.
325
325
Champ . where ( dossier_id : id , row_id : Champ ::NULL_ROW_ID ) . update_all ( row_id : nil )
326
326
327
- champ = Dossier . no_touching do
328
- champs
329
- . create_with ( **type_de_champ . params_for_champ )
330
- . create_or_find_by! ( stable_id : type_de_champ . stable_id , row_id :, stream :)
327
+ # FIXME: Try to find the champ in memory before querying the database
328
+ champ = champs . find { _1 . stream == stream && _1 . public_id == type_de_champ . public_id ( row_id ) }
329
+
330
+ if champ . nil?
331
+ champ = Dossier . no_touching do
332
+ champs
333
+ . create_with ( **type_de_champ . params_for_champ )
334
+ . create_or_find_by! ( stable_id : type_de_champ . stable_id , row_id :, stream :)
335
+ end
331
336
end
332
337
333
338
# Needed when a revision change the champ type in this case, we reset the champ data
334
339
if champ . class != type_de_champ . champ_class
335
340
champ = champ . becomes! ( type_de_champ . champ_class )
336
341
champ . assign_attributes ( value : nil , value_json : nil , external_id : nil , data : nil )
337
342
elsif stream != Champ ::MAIN_STREAM && champ . previously_new_record?
338
- # main_stream_champ = champs.find_by(stable_id: type_de_champ.stable_id, row_id:, stream: Champ::MAIN_STREAM)
339
- # champ.clone_value_from(main_stream_champ) if main_stream_champ.present?
343
+ main_stream_champ = champs . find_by ( stable_id : type_de_champ . stable_id , row_id :, stream : Champ ::MAIN_STREAM )
344
+ champ . clone_value_from ( main_stream_champ ) if main_stream_champ . present?
340
345
end
341
346
342
347
# If the champ returned from `create_or_find_by` is not the same as the one already loaded in `dossier.champs`, we need to update the association cache
0 commit comments