Skip to content

Commit 8d9f34f

Browse files
authored
Merge pull request #11724 from tchak/maybe-fix-perf-problem-again
champs: try to find the champ in memory before querying the database
2 parents 512426a + 4002fe0 commit 8d9f34f

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

app/models/concerns/dossier_champs_concern.rb

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -324,19 +324,24 @@ def champ_upsert_by!(type_de_champ, row_id)
324324
# FIXME: This is a temporary on-demand migration. It will be removed once the full migration is over.
325325
Champ.where(dossier_id: id, row_id: Champ::NULL_ROW_ID).update_all(row_id: nil)
326326

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
331336
end
332337

333338
# Needed when a revision change the champ type in this case, we reset the champ data
334339
if champ.class != type_de_champ.champ_class
335340
champ = champ.becomes!(type_de_champ.champ_class)
336341
champ.assign_attributes(value: nil, value_json: nil, external_id: nil, data: nil)
337342
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?
340345
end
341346

342347
# 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

Comments
 (0)