Skip to content
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

fix: Handle properly erpc calls on Database.transaction/4 #1229

Merged
merged 3 commits into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
23 changes: 9 additions & 14 deletions lib/realtime/database.ex
Original file line number Diff line number Diff line change
Expand Up @@ -228,24 +228,19 @@ defmodule Realtime.Database do
@doc """
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 \\ [])
@spec transaction(pid, fun(), keyword()) :: {:ok, any()} | {:error, any()}
def transaction(db_conn, func, opts \\ [], metadata \\ [])

def transaction(%DBConnection{} = db_conn, func, opts),
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) when node() == node(db_conn),
do: transaction_catched(db_conn, func, opts)

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
8 changes: 8 additions & 0 deletions test/realtime/database_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,13 @@ defmodule Realtime.DatabaseTest do
|> Task.await(15000)
end) =~ "ErrorExecutingTransaction"
end

test "run call using RPC", %{db_conn: db_conn} do
func = fn db_conn -> Postgrex.query!(db_conn, "SELECT 1", []) end
args = [db_conn, func, [backoff: :stop], [tenant_id: "test"]]

assert {:ok, %{rows: [[1]]}} =
Realtime.Rpc.enhanced_call(node(db_conn), Database, :transaction, args)
end
end
end
Loading