fly_postgres_elixir icon indicating copy to clipboard operation
fly_postgres_elixir copied to clipboard

Feature request: Repo.transaction_on_primary()

Open Qqwy opened this issue 1 year ago • 1 comments

Currently, it is recommended that you change your 'non-distributed' code:

def update(some, params) do
  Myapp.Repo.transaction(fn ->
    # ... complicated work involving DB manipulation here.
  end)
end

into

def update(some, params) do
  Fly.Postgres.rpc_and_wait(__MODULE__, :do_update, [some, params])
end

def do_update(some, params) do
  Myapp.Repo.transaction(fn ->
    # ... complicated work involving DB manipulation here.
  end)
end

However, the following alternative would require much less boilerplate and therefore fit the library's goal as 'easy to introduce, easy to rip out':

def update(some, params) do
  Myapp.Repo.transaction_on_primary(fn ->
    # ... complicated work involving DB manipulation here.
  end)
end

The main required change, would be to amend the fly_rpc library to allow passing an anonymous function (which would desugar to calling e.g. Fly.RPC.rpc(node, :erlang, :apply, [fun, []]).

The one drawback of this change, is that logging an anonymous function is not as insightful as logging a MFA-tuple. However, inspecting an anonymous function like the one constructed above will still look like:

#Function<3.103361360/1 in YourModule.update/2>

In my opinion, that should be good enough if you need to debug the RPC.

Qqwy avatar Mar 21 '23 14:03 Qqwy