Skip to content

Commit cd3f0c9

Browse files
committed
Accept :ok as successful return for Repo.transact/2 fun
1 parent 0709545 commit cd3f0c9

3 files changed

Lines changed: 17 additions & 5 deletions

File tree

integration_test/cases/repo.exs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2321,6 +2321,15 @@ defmodule Ecto.Integration.RepoTest do
23212321

23222322
describe "transact/2 with function" do
23232323
test "return ok" do
2324+
assert :ok =
2325+
TestRepo.transact(fn ->
2326+
post1 = TestRepo.insert!(%Post{title: "1"})
2327+
post2 = TestRepo.insert!(%Post{title: "2"})
2328+
:ok
2329+
end)
2330+
end
2331+
2332+
test "return ok with tuple" do
23242333
assert {:ok, [post1, post2]} =
23252334
TestRepo.transact(fn ->
23262335
post1 = TestRepo.insert!(%Post{title: "1"})

lib/ecto/repo.ex

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2306,9 +2306,9 @@ defmodule Ecto.Repo do
23062306
end)
23072307
23082308
The return value is the same as of the given `fun` which must be
2309-
`{:ok, result}` or `{:error, reason}`.
2309+
`:ok`, `{:ok, result}` or `{:error, reason}`.
23102310
2311-
If this function returns `{:ok, result}`, it means the transaction
2311+
If this function returns either `:ok`, or `{:ok, result}`, it means the transaction
23122312
was successfully committed. On the other hand, if it returns `{:error, reason}`,
23132313
it means the transaction was rolled back.
23142314
@@ -2324,8 +2324,8 @@ defmodule Ecto.Repo do
23242324
If an Elixir exception occurs the transaction will be rolled back
23252325
and the exception will bubble up from the transaction function.
23262326
If no exception occurs, the transaction is committed if the function
2327-
returns `{:ok, result}`. Returning `{:error, result}` will rollback the transaction
2328-
and this function will return `{:error, result}` as well.
2327+
returns either `:ok` or `{:ok, result}`. Returning `{:error, reason}` will rollback the transaction
2328+
and this function will return `{:error, reason}` as well.
23292329
A transaction can be explicitly rolled back
23302330
by calling `c:rollback/1`, this will immediately leave the function
23312331
and return the value given to `rollback` as `{:error, value}`.
@@ -2499,7 +2499,7 @@ defmodule Ecto.Repo do
24992499
"""
25002500
@doc group: "Transaction API"
25012501
@callback transact(fun :: (-> result), opts :: Keyword.t()) :: result
2502-
when result: {:ok, any()} | {:error, any()}
2502+
when result: :ok | {:ok, any()} | {:error, any()}
25032503
@callback transact(multi :: Ecto.Multi.t(), opts :: Keyword.t()) ::
25042504
{:ok, map()}
25052505
| Ecto.Multi.failure()

lib/ecto/repo/transaction.ex

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ defmodule Ecto.Repo.Transaction do
99
def transact(repo, _name, fun, {adapter_meta, opts}) when is_function(fun, 1) do
1010
adapter_meta.adapter.transaction(adapter_meta, opts, fn ->
1111
case fun.(repo) do
12+
:ok ->
13+
:ok
14+
1215
{:ok, result} ->
1316
result
1417

0 commit comments

Comments
 (0)