From bd401cf967078ac8fa5beb8e6561d4254ad7bd0e Mon Sep 17 00:00:00 2001 From: "byte[]" Date: Sun, 29 Mar 2020 23:04:30 -0400 Subject: [PATCH] attempt non-atomic update for gallery ordering --- lib/philomena/galleries.ex | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/lib/philomena/galleries.ex b/lib/philomena/galleries.ex index 399937d2..0010a888 100644 --- a/lib/philomena/galleries.ex +++ b/lib/philomena/galleries.ex @@ -255,21 +255,29 @@ defmodule Philomena.Galleries do [ [ id: interaction.id, - gallery_id: interaction.gallery_id, - image_id: interaction.image_id, position: interaction_positions[new_index] ] ] end end) + changes + |> Enum.map(fn change -> + id = Keyword.fetch!(change, :id) + change = Keyword.delete(change, :id) + + Interaction + |> where([i], i.id == ^id) + |> Repo.update_all(set: change) + end) + # Do the update in a single statement - Repo.insert_all( - Interaction, - changes, - on_conflict: {:replace, [:position]}, - conflict_target: [:id] - ) + # Repo.insert_all( + # Interaction, + # changes, + # on_conflict: {:replace, [:position]}, + # conflict_target: [:id] + # ) # Now update all the associated images Images.reindex_images(Map.keys(requested))