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 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
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
Loading