Skip to content

Allow handle_begin to return query #297

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
merged 2 commits into from
Sep 4, 2023
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
61 changes: 61 additions & 0 deletions integration_test/cases/transaction_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ defmodule TransactionTest do

alias TestPool, as: P
alias TestAgent, as: A
alias TestQuery, as: Q

test "transaction returns result" do
stack = [
Expand Down Expand Up @@ -1253,4 +1254,64 @@ defmodule TransactionTest do
handle_status: [_, :newest_state]
] = A.record(agent)
end

test "log query from handle_begin" do
stack = [
{:ok, :state},
{:ok, %Q{statement: "custom begin"}, :begin, :new_state},
{:ok, :committed, :newest_state}
]

{:ok, agent} = A.start_link(stack)

parent = self()
opts = [agent: agent, parent: parent]
{:ok, pool} = P.start_link(opts)

log = &send(parent, &1)

assert P.transaction(pool, fn _ -> :result end, log: log) == {:ok, :result}

assert_received %DBConnection.LogEntry{call: :begin} = entry
assert %{query: "custom begin"} = entry

assert [
connect: [_],
handle_begin: [_, :state],
handle_commit: [_, :new_state]
] = A.record(agent)
end

test "log query from handle_begin: transaction inside run" do
stack = [
{:ok, :state},
{:idle, :new_state},
{:ok, %Q{statement: "custom begin"}, :begin, :newer_state},
{:ok, :committed, :newest_state},
{:idle, :newest_state}
]

{:ok, agent} = A.start_link(stack)

parent = self()
opts = [agent: agent, parent: parent]
{:ok, pool} = P.start_link(opts)

log = &send(parent, &1)

assert P.run(pool, fn conn ->
P.transaction(conn, fn _ -> :result end, log: log)
end) == {:ok, :result}

assert_received %DBConnection.LogEntry{call: :begin} = entry
assert %{query: "custom begin"} = entry

assert [
connect: [_],
handle_status: [_, :state],
handle_begin: [_, :new_state],
handle_commit: [_, :newer_state],
handle_status: [_, :newest_state]
] = A.record(agent)
end
end
28 changes: 22 additions & 6 deletions lib/db_connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -201,9 +201,12 @@ defmodule DBConnection do
@doc """
Handle the beginning of a transaction.

Return `{:ok, result, state}` to continue, `{status, state}` to notify caller
that the transaction can not begin due to the transaction status `status`,
or `{:disconnect, exception, state}` to error and disconnect.
Return `{:ok, result, state}`/`{:ok, query, result, state}` to continue,
`{status, state}` to notify caller that the transaction can not begin due
to the transaction status `status`, or `{:disconnect, exception, state}`
to error and disconnect. If `{:ok, query, result, state}` is returned,
the query will be used to log the begin command. Otherwise, it will be
logged as `begin`.

A callback implementation should only return `status` if it
can determine the database's transaction status without side effect.
Expand All @@ -212,6 +215,7 @@ defmodule DBConnection do
"""
@callback handle_begin(opts :: Keyword.t(), state :: any) ::
{:ok, result, new_state :: any}
| {:ok, query, result, new_state :: any}
| {status, new_state :: any}
| {:disconnect, Exception.t(), new_state :: any}

Expand Down Expand Up @@ -1762,9 +1766,18 @@ defmodule DBConnection do
end

defp begin(conn, run, opts) do
conn
|> run.(&run_begin/3, meter(opts), opts)
|> log(:begin, :begin, nil)
case run.(conn, &run_begin/3, meter(opts), opts) do
{:ok, conn, {query, result}, meter} ->
query = String.Chars.to_string(query)
log({:ok, conn, result, meter}, :begin, query, nil)

{:ok, {query, result}, meter} ->
query = String.Chars.to_string(query)
log({:ok, result, meter}, :begin, query, nil)

other ->
log(other, :begin, :begin, nil)
end
end

defp run_begin(conn, meter, opts) do
Expand All @@ -1775,6 +1788,9 @@ defmodule DBConnection do
{status, _conn_state} when status in [:idle, :transaction, :error] ->
status_disconnect(conn, status, meter)

{:ok, query, result, _conn_status} ->
{:ok, {query, result}, meter}

other ->
handle_common_result(other, conn, meter)
end
Expand Down
8 changes: 7 additions & 1 deletion test/test_support.exs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,13 @@ defmodule TestConnection do
end

defmodule TestQuery do
defstruct [:state]
defstruct [:state, :statement]

defimpl String.Chars do
def to_string(%{statement: statement}) do
IO.iodata_to_binary(statement)
end
end
end

defmodule TestCursor do
Expand Down