reintroduce transaction and add impossible condition to first element of reduce step

This commit is contained in:
byte[] 2020-07-13 23:44:45 -04:00
parent e3c6610fcd
commit b97073ee18

View file

@ -37,13 +37,14 @@ defmodule Philomena.TagChanges do
to_remove = to_remove =
added added
|> Enum.map(&{&1.image_id, &1.tag_id}) |> Enum.map(&{&1.image_id, &1.tag_id})
|> Enum.reduce(Tagging, fn {image_id, tag_id}, q -> |> Enum.reduce(where(Tagging, fragment("'t' = 'f'")), fn {image_id, tag_id}, q ->
or_where(q, image_id: ^image_id, tag_id: ^tag_id) or_where(q, image_id: ^image_id, tag_id: ^tag_id)
end) end)
|> select([t], [t.image_id, t.tag_id]) |> select([t], [t.image_id, t.tag_id])
to_add = Enum.map(removed, &%{image_id: &1.image_id, tag_id: &1.tag_id}) to_add = Enum.map(removed, &%{image_id: &1.image_id, tag_id: &1.tag_id})
Repo.transaction(fn ->
{_count, inserted} = {_count, inserted} =
Repo.insert_all(Tagging, to_add, on_conflict: :nothing, returning: [:image_id, :tag_id]) Repo.insert_all(Tagging, to_add, on_conflict: :nothing, returning: [:image_id, :tag_id])
@ -86,10 +87,16 @@ defmodule Philomena.TagChanges do
upserts = added_upserts ++ removed_upserts upserts = added_upserts ++ removed_upserts
Repo.insert_all(Tag, upserts, on_conflict: update_query, conflict_target: [:id]) Repo.insert_all(Tag, upserts, on_conflict: update_query, conflict_target: [:id])
end)
|> case do
{:ok, _result} ->
Images.reindex_images(image_ids) Images.reindex_images(image_ids)
{:ok, tag_changes} {:ok, tag_changes}
error ->
error
end
end end
@doc """ @doc """