Skip to content

Commit

Permalink
Do not create version when there are no changes (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
rschef authored Feb 15, 2021
1 parent 4220bcf commit e4c56ce
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 13 deletions.
21 changes: 11 additions & 10 deletions lib/paper_trail.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
defmodule PaperTrail do
import Ecto.Changeset

alias Ecto.Changeset
alias PaperTrail.Multi
alias PaperTrail.RepoClient
alias PaperTrail.Serializer
Expand Down Expand Up @@ -32,16 +33,16 @@ defmodule PaperTrail do
returning: boolean()
]

@type result :: {:ok, Ecto.Schema.t()} | {:error, Ecto.Changeset.t()}
@type result :: {:ok, Ecto.Schema.t()} | {:error, Changeset.t()}
@type all_result :: {integer, nil | [any]}

@callback insert(Ecto.Changeset.t(), options) :: result
@callback insert!(Ecto.Changeset.t(), options) :: Ecto.Schema.t()
@callback update(Ecto.Changeset.t(), options) :: result
@callback update!(Ecto.Changeset.t(), options) :: Ecto.Schema.t()
@callback insert(Changeset.t(), options) :: result
@callback insert!(Changeset.t(), options) :: Ecto.Schema.t()
@callback update(Changeset.t(), options) :: result
@callback update!(Changeset.t(), options) :: Ecto.Schema.t()
@callback update_all(queryable, updates, options) :: all_result
@callback delete(Ecto.Changeset.t(), options) :: result
@callback delete!(Ecto.Changeset.t(), options) :: Ecto.Schema.t()
@callback delete(Changeset.t(), options) :: result
@callback delete!(Changeset.t(), options) :: Ecto.Schema.t()

@callback get_version(Ecto.Schema.t()) :: Ecto.Query.t()
@callback get_version(module, any) :: Ecto.Query.t()
Expand Down Expand Up @@ -172,7 +173,7 @@ defmodule PaperTrail do
@doc """
Inserts a record to the database with a related version insertion in one transaction
"""
@spec insert(Ecto.Changeset.t(), options) :: result
@spec insert(Changeset.t(), options) :: result
def insert(changeset, options \\ []) do
Multi.new()
|> Multi.insert(changeset, options)
Expand Down Expand Up @@ -230,7 +231,7 @@ defmodule PaperTrail do
@doc """
Updates a record from the database with a related version insertion in one transaction
"""
@spec update(Ecto.Changeset.t(), options) :: result
@spec update(Changeset.t(), options) :: result
def update(changeset, options \\ []) do
Multi.new()
|> Multi.update(changeset, options)
Expand Down Expand Up @@ -292,7 +293,7 @@ defmodule PaperTrail do
@doc """
Deletes a record from the database with a related version insertion in one transaction
"""
@spec delete(Ecto.Changeset.t(), options) :: result
@spec delete(Changeset.t(), options) :: result
def delete(struct, options \\ []) do
Multi.new()
|> Multi.delete(struct, options)
Expand Down
12 changes: 9 additions & 3 deletions lib/paper_trail/multi.ex
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
defmodule PaperTrail.Multi do
import Ecto.Changeset

alias Ecto.Changeset
alias PaperTrail
alias PaperTrail.Version
alias PaperTrail.RepoClient
alias PaperTrail.Serializer

@type multi :: Ecto.Multi.t()
@type changeset :: Ecto.Changeset.t()
@type changeset :: Changeset.t()
@type options :: PaperTrail.options()
@type queryable :: PaperTrail.queryable()
@type updates :: PaperTrail.updates()
@type struct_or_changeset :: Ecto.Schema.t() | Ecto.Changeset.t()
@type struct_or_changeset :: Ecto.Schema.t() | Changeset.t()
@type result ::
{:ok, any()}
| {:error, any()}
Expand Down Expand Up @@ -133,7 +134,12 @@ defmodule PaperTrail.Multi do
|> Ecto.Multi.update(model_key, changeset)
|> Ecto.Multi.run(version_key, fn repo, _changes ->
version = make_version_struct(%{event: "update"}, changeset, options)
repo.insert(version)

if changeset.changes == %{} do
{:ok, nil}
else
repo.insert(version)
end
end)
end
end
Expand Down
23 changes: 23 additions & 0 deletions test/paper_trail/base_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,29 @@ defmodule PaperTrailTest do
assert company == first(Company, :id) |> @repo.one |> serialize
end

test "do not create version if there're no changes" do
{:ok, insert_result} = create_company_with_version()

{:ok, result} =
update_company_with_version(
insert_result[:model],
%{}
)

company_count = Company.count()
version_count = Version.count()

company = result[:model] |> serialize
version = result[:version]

assert Map.keys(result) == [:model, :version]
assert company_count == 1
assert version_count == 1

assert company == insert_result[:model] |> serialize
assert version == nil
end

test "updating a company with originator[user] creates a correct company version" do
user = create_user()
{:ok, insert_result} = create_company_with_version()
Expand Down

0 comments on commit e4c56ce

Please sign in to comment.