Skip to content

Commit

Permalink
fix: Handle properly erpc calls on Database.transaction/4 (#1229)
Browse files Browse the repository at this point in the history
  • Loading branch information
filipecabaco authored Nov 19, 2024
1 parent 0147dbb commit ac00218
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 14 deletions.
22 changes: 10 additions & 12 deletions lib/realtime/database.ex
Original file line number Diff line number Diff line change
Expand Up @@ -229,23 +229,21 @@ defmodule Realtime.Database do
Runs database transaction in local node or against a target node withing a Postgrex transaction
"""
@spec transaction(pid | DBConnection.t(), fun(), keyword()) :: {:ok, any()} | {:error, any()}
def transaction(db_conn, func, opts \\ [])
def transaction(db_conn, func, opts \\ [], metadata \\ [])

def transaction(%DBConnection{} = db_conn, func, opts),
do: transaction_catched(db_conn, func, opts)
def transaction(%DBConnection{} = db_conn, func, opts, metadata),
do: transaction_catched(db_conn, func, opts, metadata)

def transaction(db_conn, func, opts) when node() == node(db_conn),
do: transaction_catched(db_conn, func, opts)
def transaction(db_conn, func, opts, metadata) when node() == node(db_conn),
do: transaction_catched(db_conn, func, opts, metadata)

def transaction(db_conn, func, opts) do
metadata =
Keyword.take(Logger.metadata(), [:project_id, :tenant_id])
|> Keyword.put(:target, node(db_conn))

Rpc.enhanced_call(node(db_conn), __MODULE__, :transaction, [db_conn, func, opts, metadata])
def transaction(db_conn, func, opts, metadata) do
metadata = Keyword.put(metadata, :target, node(db_conn))
args = [db_conn, func, opts, metadata]
Rpc.enhanced_call(node(db_conn), __MODULE__, :transaction, args)
end

defp transaction_catched(db_conn, func, opts, metadata \\ []) do
defp transaction_catched(db_conn, func, opts, metadata) do
Postgrex.transaction(db_conn, func, opts)
rescue
e ->
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule Realtime.MixProject do
def project do
[
app: :realtime,
version: "2.33.49",
version: "2.33.51",
elixir: "~> 1.16.0",
elixirc_paths: elixirc_paths(Mix.env()),
start_permanent: Mix.env() == :prod,
Expand Down
15 changes: 15 additions & 0 deletions test/realtime/database_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,20 @@ defmodule Realtime.DatabaseTest do
|> Task.await(15000)
end) =~ "ErrorExecutingTransaction"
end

test "run call using RPC", %{db_conn: db_conn} do
assert {:ok, %{rows: [[1]]}} =
Realtime.Rpc.enhanced_call(
node(db_conn),
Database,
:transaction,
[
db_conn,
fn db_conn -> Postgrex.query!(db_conn, "SELECT 1", []) end,
[backoff: :stop],
[tenant_id: "test"]
]
)
end
end
end
2 changes: 1 addition & 1 deletion test/realtime/tenants/janitor_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ defmodule Realtime.Tenants.JanitorTest do
start_supervised!(
{Task.Supervisor,
name: Realtime.Tenants.Janitor.TaskSupervisor,
max_children: 2,
max_children: 5,
max_seconds: 500,
max_restarts: 1}
)
Expand Down

0 comments on commit ac00218

Please sign in to comment.