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,17 +59,18 @@ defmodule AshPostgres.CustomIndex do
# sobelow_skip ["DOS.StringToAtom"]
def transform(%__MODULE__{fields: fields} = index) do
%{
index
| fields:
Enum.map(fields, fn field ->
if is_atom(field) do
field
else
String.to_atom(field)
end
end)
}
{:ok,
%{
index
| fields:
Enum.map(fields, fn field ->
if is_atom(field) do
field
else
String.to_atom(field)
end
end)
}}
end
def name(_resource, %{name: name}) when is_binary(name) do

View file

@ -52,6 +52,7 @@ defmodule AshPostgres.DataLayer do
],
target: AshPostgres.CustomIndex,
schema: AshPostgres.CustomIndex.schema(),
transform: {AshPostgres.CustomIndex, :transform, []},
args: [:fields]
}
@ -1633,7 +1634,12 @@ defmodule AshPostgres.DataLayer do
end
@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
AshPostgres.DataLayer.Info.repo(resource).transaction(func, timeout: timeout)
else

View file

@ -37,6 +37,8 @@ defmodule AshPostgres.Repo do
"""
@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)"
@callback all_tenants() :: [String.t()]
@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}
end
def on_transaction_begin(_reason), do: :ok
defoverridable init: 2,
on_transaction_begin: 1,
installed_extensions: 0,
all_tenants: 0,
tenant_migrations_path: 0,