From ac00218ec00a606463a836c4df9dce6d38f4d29a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filipe=20Caba=C3=A7o?= Date: Tue, 19 Nov 2024 12:46:34 +0000 Subject: [PATCH] fix: Handle properly erpc calls on Database.transaction/4 (#1229) --- lib/realtime/database.ex | 22 ++++++++++------------ mix.exs | 2 +- test/realtime/database_test.exs | 15 +++++++++++++++ test/realtime/tenants/janitor_test.exs | 2 +- 4 files changed, 27 insertions(+), 14 deletions(-) diff --git a/lib/realtime/database.ex b/lib/realtime/database.ex index c9063f442..01d0aeaaf 100644 --- a/lib/realtime/database.ex +++ b/lib/realtime/database.ex @@ -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 -> diff --git a/mix.exs b/mix.exs index d62e1a432..d243d3bd9 100644 --- a/mix.exs +++ b/mix.exs @@ -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, diff --git a/test/realtime/database_test.exs b/test/realtime/database_test.exs index b8604fe1e..15ac90a86 100644 --- a/test/realtime/database_test.exs +++ b/test/realtime/database_test.exs @@ -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 diff --git a/test/realtime/tenants/janitor_test.exs b/test/realtime/tenants/janitor_test.exs index e3aea0ecd..0023bea59 100644 --- a/test/realtime/tenants/janitor_test.exs +++ b/test/realtime/tenants/janitor_test.exs @@ -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} )