Skip to content

Commit

Permalink
Merge pull request #14 from nash-io/pass-repo-options-to-ecto-functions
Browse files Browse the repository at this point in the history
Pass repo options to Ecto functions
  • Loading branch information
adrienmo authored Nov 23, 2022
2 parents 6ab8b29 + 0cdc4e6 commit 8db5b77
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
16 changes: 10 additions & 6 deletions lib/paper_trail.ex
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ defmodule PaperTrail do
model_key: multi_name,
version_key: multi_name,
return_operation: multi_name,
returning: boolean()
returning: boolean(),
repo_options: Keyword.t()
]

@type result :: {:ok, Ecto.Schema.t()} | {:error, Changeset.t()}
Expand Down Expand Up @@ -186,6 +187,7 @@ defmodule PaperTrail do
@spec insert!(Ecto.Schema.t(), options) :: Ecto.Schema.t()
def insert!(changeset, options \\ []) do
repo = RepoClient.repo(options)
repo_options = Keyword.get(options, :repo_options, [])

repo.transaction(fn ->
case RepoClient.strict_mode(options) do
Expand All @@ -211,7 +213,7 @@ defmodule PaperTrail do
current_version_id: initial_version.id
})

model = repo.insert!(updated_changeset)
model = repo.insert!(updated_changeset, repo_options)

target_version =
make_version_struct(%{event: "insert"}, model, options) |> serialize(options)
Expand All @@ -220,7 +222,7 @@ defmodule PaperTrail do
model

_ ->
model = repo.insert!(changeset)
model = repo.insert!(changeset, repo_options)
make_version_struct(%{event: "insert"}, model, options) |> repo.insert!
model
end
Expand All @@ -244,6 +246,7 @@ defmodule PaperTrail do
@spec update!(Ecto.Schema.t(), options) :: Ecto.Schema.t()
def update!(changeset, options \\ []) do
repo = RepoClient.repo(options)
repo_options = Keyword.get(options, :repo_options, [])

repo.transaction(fn ->
case RepoClient.strict_mode(options) do
Expand All @@ -258,7 +261,7 @@ defmodule PaperTrail do
target_version = make_version_struct(%{event: "update"}, target_changeset, options)
initial_version = repo.insert!(target_version)
updated_changeset = changeset |> change(%{current_version_id: initial_version.id})
model = repo.update!(updated_changeset)
model = repo.update!(updated_changeset, repo_options)

new_item_changes =
initial_version.item_changes
Expand All @@ -270,7 +273,7 @@ defmodule PaperTrail do
model

_ ->
model = repo.update!(changeset)
model = repo.update!(changeset, repo_options)
version_struct = make_version_struct(%{event: "update"}, changeset, options)
repo.insert!(version_struct)
model
Expand Down Expand Up @@ -306,9 +309,10 @@ defmodule PaperTrail do
@spec delete!(Ecto.Schema.t(), options) :: Ecto.Schema.t()
def delete!(struct, options \\ []) do
repo = RepoClient.repo(options)
repo_options = Keyword.get(options, :repo_options, [])

repo.transaction(fn ->
model = repo.delete!(struct, options)
model = repo.delete!(struct, repo_options)
version_struct = make_version_struct(%{event: "delete"}, struct, options)
repo.insert!(version_struct, options)
model
Expand Down
16 changes: 10 additions & 6 deletions lib/paper_trail/multi.ex
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ defmodule PaperTrail.Multi do
def insert(%Ecto.Multi{} = multi, changeset, options \\ []) do
model_key = get_model_key(options)
version_key = get_version_key(options)
repo_options = Keyword.get(options, :repo_options, [])

case RepoClient.strict_mode(options) do
true ->
Expand All @@ -69,7 +70,7 @@ defmodule PaperTrail.Multi do
current_version_id: initial_version.id
})

repo.insert(updated_changeset)
repo.insert(updated_changeset, repo_options)
end)
|> Ecto.Multi.run(version_key, fn repo,
%{
Expand All @@ -84,7 +85,7 @@ defmodule PaperTrail.Multi do

_ ->
multi
|> Ecto.Multi.insert(model_key, changeset)
|> Ecto.Multi.insert(model_key, changeset, repo_options)
|> Ecto.Multi.run(version_key, fn repo, %{^model_key => model} ->
version = make_version_struct(%{event: "insert"}, model, options)
repo.insert(version)
Expand All @@ -100,6 +101,7 @@ defmodule PaperTrail.Multi do
) do
model_key = get_model_key(options)
version_key = get_version_key(options)
repo_options = Keyword.get(options, :repo_options, [])

case RepoClient.strict_mode(options) do
true ->
Expand All @@ -117,7 +119,7 @@ defmodule PaperTrail.Multi do
end)
|> Ecto.Multi.run(model_key, fn repo, %{initial_version: initial_version} ->
updated_changeset = changeset |> change(%{current_version_id: initial_version.id})
repo.update(updated_changeset)
repo.update(updated_changeset, repo_options)
end)
|> Ecto.Multi.run(version_key, fn repo, %{initial_version: initial_version} ->
new_item_changes =
Expand All @@ -131,7 +133,7 @@ defmodule PaperTrail.Multi do

_ ->
multi
|> Ecto.Multi.update(model_key, changeset)
|> Ecto.Multi.update(model_key, changeset, repo_options)
|> Ecto.Multi.run(version_key, fn repo, _changes ->
version = make_version_struct(%{event: "update"}, changeset, options)

Expand All @@ -155,14 +157,15 @@ defmodule PaperTrail.Multi do
version_key = get_version_key(options)
entries = make_version_structs(%{event: "update"}, queryable, changes, options)
returning = !!options[:returning] && RepoClient.return_operation(options) == version_key
repo_options = Keyword.get(options, :repo_options, [])

case RepoClient.strict_mode(options) do
true ->
raise "Strict mode not implemented for update_all"

_ ->
multi
|> Ecto.Multi.update_all(model_key, queryable, updates)
|> Ecto.Multi.update_all(model_key, queryable, updates, repo_options)
|> Ecto.Multi.insert_all(version_key, Version, entries, returning: returning)
end
end
Expand All @@ -175,9 +178,10 @@ defmodule PaperTrail.Multi do
) do
model_key = get_model_key(options)
version_key = get_version_key(options)
repo_options = Keyword.get(options, :repo_options, [])

multi
|> Ecto.Multi.delete(model_key, struct_or_changeset, options)
|> Ecto.Multi.delete(model_key, struct_or_changeset, repo_options)
|> Ecto.Multi.run(version_key, fn repo, %{} ->
version = make_version_struct(%{event: "delete"}, struct_or_changeset, options)
repo.insert(version, options)
Expand Down

0 comments on commit 8db5b77

Please sign in to comment.