diff --git a/core/lib/core/repo.ex b/core/lib/core/repo.ex index d68867cb1..b9321438e 100644 --- a/core/lib/core/repo.ex +++ b/core/lib/core/repo.ex @@ -7,6 +7,8 @@ defmodule Core.Repo do import Ecto.Query, warn: false alias Ecto.Multi + require Logger + def multi_update(multi, name, changeset) do multi |> multi_update_guard(name, changeset.data) @@ -14,16 +16,18 @@ defmodule Core.Repo do end def multi_update_guard(multi, name, model) do - Multi.run(multi, "#{name}_exists?", fn repo, _ -> - if valid?(repo, model) do + Multi.run(multi, "#{name}_up_to_date?", fn repo, _ -> + if up_to_date?(repo, model) do {:ok, true} else - {:error, false} + message = "#{String.capitalize(name)} is not up to date, preventing database change" + Logger.warning(message) + {:error, message} end end) end - defp valid?(repo, %schema{id: id, updated_at: updated_at}) do + defp up_to_date?(repo, %schema{id: id, updated_at: updated_at}) do from(e in schema, where: e.id == ^id and e.updated_at == ^updated_at) |> repo.exists?() end diff --git a/core/systems/assignment/_public.ex b/core/systems/assignment/_public.ex index cab22ec9b..c1c03835a 100644 --- a/core/systems/assignment/_public.ex +++ b/core/systems/assignment/_public.ex @@ -682,8 +682,14 @@ end defimpl Core.Persister, for: Systems.Assignment.Model do def save(_assignment, changeset) do case Frameworks.Utility.EctoHelper.update_and_dispatch(changeset, :assignment) do - {:ok, %{assignment: assignment}} -> {:ok, assignment} - _ -> {:error, changeset} + {:ok, %{assignment: assignment}} -> + {:ok, assignment} + + {:error, new_changeset} -> + {:error, new_changeset} + + _ -> + {:error, changeset} end end end diff --git a/core/systems/assignment/content_page_builder.ex b/core/systems/assignment/content_page_builder.ex index 808a0abdc..8b68ec2b4 100644 --- a/core/systems/assignment/content_page_builder.ex +++ b/core/systems/assignment/content_page_builder.ex @@ -160,7 +160,7 @@ defmodule Systems.Assignment.ContentPageBuilder do end defp set_status(%{assigns: %{model: assignment}} = socket, status) do - assignment = Assignment.Public.update!(assignment, %{status: status}) + {:ok, assignment} = Assignment.Public.update(assignment, %{status: status}) socket |> Phoenix.Component.assign(model: assignment) end diff --git a/core/systems/observatory/_public.ex b/core/systems/observatory/_public.ex index a9175c713..46bf5baf1 100644 --- a/core/systems/observatory/_public.ex +++ b/core/systems/observatory/_public.ex @@ -73,7 +73,8 @@ defmodule Systems.Observatory.Public do { :noreply, socket - |> Public.update_view_model(__MODULE__, model, @presenter) + |> assign(model: model) + |> update_view_model() |> handle_view_model_updated() |> put_updated_info_flash() }