fix: properly turn custom index keys into atoms

improvement: support new transaction info with hooks
This commit is contained in:
Zach Daniel 2022-11-30 19:06:51 -05:00
parent 059837651d
commit c035eed21a
3 changed files with 24 additions and 12 deletions

View file

@ -59,6 +59,7 @@ defmodule AshPostgres.CustomIndex do
# sobelow_skip ["DOS.StringToAtom"] # sobelow_skip ["DOS.StringToAtom"]
def transform(%__MODULE__{fields: fields} = index) do def transform(%__MODULE__{fields: fields} = index) do
{:ok,
%{ %{
index index
| fields: | fields:
@ -69,7 +70,7 @@ defmodule AshPostgres.CustomIndex do
String.to_atom(field) String.to_atom(field)
end end
end) end)
} }}
end end
def name(_resource, %{name: name}) when is_binary(name) do def name(_resource, %{name: name}) when is_binary(name) do

View file

@ -52,6 +52,7 @@ defmodule AshPostgres.DataLayer do
], ],
target: AshPostgres.CustomIndex, target: AshPostgres.CustomIndex,
schema: AshPostgres.CustomIndex.schema(), schema: AshPostgres.CustomIndex.schema(),
transform: {AshPostgres.CustomIndex, :transform, []},
args: [:fields] args: [:fields]
} }
@ -1633,7 +1634,12 @@ defmodule AshPostgres.DataLayer do
end end
@impl true @impl true
def transaction(resource, func, timeout \\ nil) do def transaction(resource, func, timeout \\ nil, reason \\ %{type: :custom, metadata: %{}}) do
func = fn ->
AshPostgres.DataLayer.Info.repo(resource).on_transaction_begin(reason)
func.()
end
if timeout do if timeout do
AshPostgres.DataLayer.Info.repo(resource).transaction(func, timeout: timeout) AshPostgres.DataLayer.Info.repo(resource).transaction(func, timeout: timeout)
else else

View file

@ -37,6 +37,8 @@ defmodule AshPostgres.Repo do
""" """
@callback min_pg_version() :: integer() @callback min_pg_version() :: integer()
@callback on_transaction_begin(reason :: Ash.DataLayer.transaction_reason()) :: term
@doc "Return a list of all schema names (only relevant for a multitenant implementation)" @doc "Return a list of all schema names (only relevant for a multitenant implementation)"
@callback all_tenants() :: [String.t()] @callback all_tenants() :: [String.t()]
@doc "The path where your tenant migrations are stored (only relevant for a multitenant implementation)" @doc "The path where your tenant migrations are stored (only relevant for a multitenant implementation)"
@ -87,7 +89,10 @@ defmodule AshPostgres.Repo do
{:ok, new_config} {:ok, new_config}
end end
def on_transaction_begin(_reason), do: :ok
defoverridable init: 2, defoverridable init: 2,
on_transaction_begin: 1,
installed_extensions: 0, installed_extensions: 0,
all_tenants: 0, all_tenants: 0,
tenant_migrations_path: 0, tenant_migrations_path: 0,