Skip to content

Commit

Permalink
when there's no changes to save, rely on ecto's update being a no-op
Browse files Browse the repository at this point in the history
  • Loading branch information
mayel committed Jul 22, 2023
1 parent a1935bf commit 09c246f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
6 changes: 1 addition & 5 deletions lib/paper_trail.ex
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,7 @@ defmodule PaperTrail do
@spec update(changeset :: Ecto.Changeset.t(model), options :: Keyword.t()) ::
{:ok, %{model: model, version: Version.t()}} | {:error, Ecto.Changeset.t(model) | term}
when model: struct
def update(changeset, options \\ @default_transaction_options)
def update(%Ecto.Changeset{changes: changes}, _) when changes==%{} do
{:ok, :no_changes}
end
def update(changeset, options) do
def update(changeset, options \\ @default_transaction_options) do
PaperTrail.Multi.new()
|> PaperTrail.Multi.update(changeset, options)
|> PaperTrail.Multi.commit()
Expand Down
16 changes: 15 additions & 1 deletion lib/paper_trail/multi.ex
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,21 @@ defmodule PaperTrail.Multi do
end
end

def update(%Ecto.Multi{} = multi, changeset, options \\ @default_transaction_options) do
def update(multi, changeset, options \\ @default_transaction_options)
def update(%Ecto.Multi{} = multi, %Ecto.Changeset{changes: changes} = changeset, options) when changes==%{} do
# when there's no changes to save, rely on ecto's update being a no-op
model_key = options[:model_key] || :model
ecto_options = options[:ecto_options] || []

multi
|> Ecto.Multi.update(
model_key,
changeset,
ecto_options ++ Keyword.take(options, [:returning])
)
end

def update(%Ecto.Multi{} = multi, changeset, options) do
model_key = options[:model_key] || :model
version_key = options[:version_key] || :version
initial_version_key = options[:initial_version_key] || :initial_version
Expand Down

0 comments on commit 09c246f

Please sign in to comment.