From 0cdc4e6c01f7a05c53e8d96fd43af8db5272d8b3 Mon Sep 17 00:00:00 2001 From: Rafael Scheffer Date: Wed, 23 Nov 2022 00:43:00 -0300 Subject: [PATCH] Pass repo_options to Ecto functions --- lib/paper_trail.ex | 16 ++++++++++------ lib/paper_trail/multi.ex | 16 ++++++++++------ 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/lib/paper_trail.ex b/lib/paper_trail.ex index 09d36092..5d6b9361 100644 --- a/lib/paper_trail.ex +++ b/lib/paper_trail.ex @@ -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()} @@ -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 @@ -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) @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/lib/paper_trail/multi.ex b/lib/paper_trail/multi.ex index 6a5bf64e..f3878a5b 100644 --- a/lib/paper_trail/multi.ex +++ b/lib/paper_trail/multi.ex @@ -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 -> @@ -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, %{ @@ -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) @@ -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 -> @@ -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 = @@ -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) @@ -155,6 +157,7 @@ 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 -> @@ -162,7 +165,7 @@ defmodule PaperTrail.Multi do _ -> 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 @@ -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)