Skip to content

Add Repo.prepare_transaction/2 #4605

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 35 additions & 6 deletions lib/ecto/repo.ex
Original file line number Diff line number Diff line change
Expand Up @@ -292,12 +292,12 @@ defmodule Ecto.Repo do
def transaction(fun_or_multi, opts \\ []) do
repo = get_dynamic_repo()

Ecto.Repo.Transaction.transaction(
__MODULE__,
repo,
fun_or_multi,
{adapter_meta, opts} =
Ecto.Repo.Supervisor.tuplet(repo, prepare_opts(:transaction, opts))
)

{fun_or_multi, opts} = prepare_transaction(fun_or_multi, opts)

Ecto.Repo.Transaction.transaction(__MODULE__, repo, fun_or_multi, {adapter_meta, opts})
end

def in_transaction? do
Expand Down Expand Up @@ -618,6 +618,9 @@ defmodule Ecto.Repo do

def prepare_query(operation, query, opts), do: {query, opts}
defoverridable prepare_query: 3

def prepare_transaction(fun_or_multi, opts), do: {fun_or_multi, opts}
defoverridable prepare_transaction: 2
end
end
end
Expand Down Expand Up @@ -1274,6 +1277,31 @@ defmodule Ecto.Repo do
{Ecto.Query.t(), Keyword.t()}
when operation: :all | :update_all | :delete_all | :stream | :insert_all

@doc """
A user-customizable callback invoked on transaction operations.

This callback can be used to further modify the given Ecto Multi and options in a transaction operation
before it is transformed and sent to the database.

This callback is only invoked in transactions.

## Examples

Imagine you want to prepend a SQL comment to commit statements using the `commit_comment` option on transactions.

@impl true
def prepare_transaction(multi_or_fun, opts) do
opts = Keyword.put_new_lazy(opts, :commit_comment, fn -> extract_comment(opts) end)
{multi_or_fun, opts}
end

The callback will be invoked for every transaction operation, and it will try to extract the appropriate commit comment,
that will be subsequently used by the adapters if they support this option.
"""
@doc group: "User callbacks"
@callback prepare_transaction(fun_or_multi :: fun | Ecto.Multi.t(), opts :: Keyword.t()) ::
{fun_or_multi :: fun | Ecto.Multi.t(), Keyword.t()}

@doc """
A user customizable callback invoked to retrieve default options
for operations.
Expand Down Expand Up @@ -1504,7 +1532,8 @@ defmodule Ecto.Repo do
delete!: 2,
insert_or_update: 2,
insert_or_update!: 2,
prepare_query: 3
prepare_query: 3,
prepare_transaction: 2

@doc """
Inserts all entries into the repository.
Expand Down
18 changes: 9 additions & 9 deletions test/ecto/multi_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ defmodule Ecto.MultiTest do
assert [{:fun, {:run, _fun}}] = multi.operations

assert {:ok, changes} = TestRepo.transaction(multi)
assert_received {:transaction, _}
assert_received {:transaction, _, _}

assert changes[:fun] == {1, nil}
end
Expand All @@ -319,7 +319,7 @@ defmodule Ecto.MultiTest do
assert [{:fun, {:run, _fun}}] = multi.operations

assert {:ok, changes} = TestRepo.transaction(multi)
assert_received {:transaction, _}
assert_received {:transaction, _, _}

assert changes[:fun] == {1, nil}
end
Expand All @@ -345,7 +345,7 @@ defmodule Ecto.MultiTest do
assert [{:fun, {:run, _fun}}] = multi.operations

assert {:ok, changes} = TestRepo.transaction(multi)
assert_received {:transaction, _}
assert_received {:transaction, _, _}

assert changes[:fun] == {1, nil}
end
Expand Down Expand Up @@ -554,7 +554,7 @@ defmodule Ecto.MultiTest do
|> Multi.delete_all(:delete_all, Comment)

assert {:ok, changes} = TestRepo.transaction(multi)
assert_received {:transaction, _}
assert_received {:transaction, _, _}

assert {:messages,
[
Expand Down Expand Up @@ -598,7 +598,7 @@ defmodule Ecto.MultiTest do

test "with empty multi" do
assert {:ok, changes} = TestRepo.transaction(Multi.new())
refute_received {:transaction, _}
refute_received {:transaction, _, _}
assert changes == %{}
end

Expand All @@ -613,7 +613,7 @@ defmodule Ecto.MultiTest do
|> Multi.delete(:delete, changeset)

assert {:error, :run, "error from run", changes} = TestRepo.transaction(multi)
assert_received {:transaction, _}
assert_received {:transaction, _, _}
assert_received {:rollback, _}
assert {:messages, [{:insert, %{source: "comments"}}]} = Process.info(self(), :messages)
assert %Comment{} = changes.insert
Expand All @@ -636,7 +636,7 @@ defmodule Ecto.MultiTest do
|> Multi.delete(:delete, changeset)

assert {:error, :update, error, changes} = TestRepo.transaction(multi)
assert_received {:transaction, _}
assert_received {:transaction, _, _}
assert_received {:rollback, _}
assert {:messages, [{:insert, %{source: "comments"}}]} = Process.info(self(), :messages)
assert %Comment{} = changes.insert
Expand All @@ -657,14 +657,14 @@ defmodule Ecto.MultiTest do

assert {:error, :invalid, invalid, %{}} = TestRepo.transaction(multi)
assert invalid.data == changeset.data
refute_received {:transaction, _}
refute_received {:transaction, _, _}
end

test "checks error operation before starting transaction" do
multi = Multi.new() |> Multi.error(:invalid, "error")

assert {:error, :invalid, "error", %{}} = TestRepo.transaction(multi)
refute_received {:transaction, _}
refute_received {:transaction, _, _}
end
end

Expand Down
16 changes: 8 additions & 8 deletions test/ecto/repo/belongs_to_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,9 @@ defmodule Ecto.Repo.BelongsToTest do
refute changeset.valid?

# Just one transaction was used
assert_received {:transaction, _}
assert_received {:transaction, _, _}
assert_received {:rollback, ^changeset}
refute_received {:transaction, _}
refute_received {:transaction, _, _}
refute_received {:rollback, _}
end

Expand All @@ -174,7 +174,7 @@ defmodule Ecto.Repo.BelongsToTest do
assert schema.assoc.sub_assoc_id == schema.assoc.sub_assoc.id

# Just one transaction was used
assert_received {:transaction, _}
assert_received {:transaction, _, _}
refute_received {:rollback, _}
end

Expand Down Expand Up @@ -212,9 +212,9 @@ defmodule Ecto.Repo.BelongsToTest do
refute changeset.valid?

# Just one transaction was used
assert_received {:transaction, _}
assert_received {:transaction, _, _}
assert_received {:rollback, ^changeset}
refute_received {:transaction, _}
refute_received {:transaction, _, _}
refute_received {:rollback, _}
end

Expand Down Expand Up @@ -444,7 +444,7 @@ defmodule Ecto.Repo.BelongsToTest do
assert schema.assoc.sub_assoc.id

# One transaction was used
assert_received {:transaction, _}
assert_received {:transaction, _, _}
refute_received {:rollback, _}
end

Expand All @@ -469,9 +469,9 @@ defmodule Ecto.Repo.BelongsToTest do
refute changeset.valid?

# Just one transaction was used
assert_received {:transaction, _}
assert_received {:transaction, _, _}
assert_received {:rollback, ^changeset}
refute_received {:transaction, _}
refute_received {:transaction, _, _}
refute_received {:rollback, _}
end

Expand Down
16 changes: 8 additions & 8 deletions test/ecto/repo/has_assoc_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -215,9 +215,9 @@ defmodule Ecto.Repo.HasAssocTest do
refute changeset.valid?

# Just one transaction was used
assert_received {:transaction, _}
assert_received {:transaction, _, _}
assert_received {:rollback, ^changeset}
refute_received {:transaction, _}
refute_received {:transaction, _, _}
refute_received {:rollback, _}
end

Expand All @@ -234,7 +234,7 @@ defmodule Ecto.Repo.HasAssocTest do
assert schema.assoc.sub_assoc.id

# Just one transaction was used
assert_received {:transaction, _}
assert_received {:transaction, _, _}
refute_received {:rollback, _}
end

Expand Down Expand Up @@ -271,9 +271,9 @@ defmodule Ecto.Repo.HasAssocTest do
refute changeset.valid?

# Just one transaction was used
assert_received {:transaction, _}
assert_received {:transaction, _, _}
assert_received {:rollback, ^changeset}
refute_received {:transaction, _}
refute_received {:transaction, _, _}
refute_received {:rollback, _}
end

Expand Down Expand Up @@ -573,7 +573,7 @@ defmodule Ecto.Repo.HasAssocTest do
assert schema.assoc.sub_assoc.id

# One transaction was used
assert_received {:transaction, _}
assert_received {:transaction, _, _}
refute_received {:rollback, _}
end

Expand All @@ -598,9 +598,9 @@ defmodule Ecto.Repo.HasAssocTest do
refute changeset.valid?

# Just one transaction was used
assert_received {:transaction, _}
assert_received {:transaction, _, _}
assert_received {:rollback, ^changeset}
refute_received {:transaction, _}
refute_received {:transaction, _, _}
refute_received {:rollback, _}
end

Expand Down
16 changes: 8 additions & 8 deletions test/ecto/repo/many_to_many_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -274,9 +274,9 @@ defmodule Ecto.Repo.ManyToManyTest do
refute changeset.valid?

# Just one transaction was used
assert_received {:transaction, _}
assert_received {:transaction, _, _}
assert_received {:rollback, ^changeset}
refute_received {:transaction, _}
refute_received {:transaction, _, _}
refute_received {:rollback, _}
refute_received {:insert_all, _, _}
end
Expand All @@ -294,7 +294,7 @@ defmodule Ecto.Repo.ManyToManyTest do
assert hd(schema.assocs).sub_assoc.id

# Just one transaction was used
assert_received {:transaction, _}
assert_received {:transaction, _, _}
refute_received {:rollback, _}
assert_received {:insert_all, %{source: "schemas_assocs"}, [[my_assoc_id: 1, my_schema_id: 1]]}
end
Expand Down Expand Up @@ -332,9 +332,9 @@ defmodule Ecto.Repo.ManyToManyTest do
refute Map.has_key?(assoc.changes.sub_assoc.changes, :id)

# Just one transaction was used
assert_received {:transaction, _}
assert_received {:transaction, _, _}
assert_received {:rollback, ^changeset}
refute_received {:transaction, _}
refute_received {:transaction, _, _}
refute_received {:rollback, _}
refute_received {:insert_all, _, _}
end
Expand Down Expand Up @@ -624,7 +624,7 @@ defmodule Ecto.Repo.ManyToManyTest do
assert hd(schema.assocs).sub_assoc.id

# One transaction was used
assert_received {:transaction, _}
assert_received {:transaction, _, _}
refute_received {:rollback, _}
refute_received {:insert_all, _, _}
end
Expand Down Expand Up @@ -652,9 +652,9 @@ defmodule Ecto.Repo.ManyToManyTest do
refute Map.has_key?(assoc.changes.sub_assoc.changes, :my_assoc_id)

# Just one transaction was used
assert_received {:transaction, _}
assert_received {:transaction, _, _}
assert_received {:rollback, ^changeset}
refute_received {:transaction, _}
refute_received {:transaction, _, _}
refute_received {:rollback, _}
refute_received {:insert_all, _, _}
end
Expand Down
Loading
Loading