Skip to content

Commit

Permalink
improvement: support prefer_transaction?
Browse files Browse the repository at this point in the history
  • Loading branch information
zachdaniel committed Oct 27, 2024
1 parent 879c8f1 commit d861ce0
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 7 deletions.
22 changes: 18 additions & 4 deletions lib/data_layer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,11 @@ defmodule AshPostgres.DataLayer do

import Ecto.Query, only: [from: 2, subquery: 1]

@impl true
def prefer_transaction?(resource) do
AshPostgres.DataLayer.Info.repo(resource, :mutate).prefer_transaction?()
end

@impl true
def can?(_, :async_engine), do: true
def can?(_, :bulk_create), do: true
Expand Down Expand Up @@ -1773,7 +1778,12 @@ defmodule AshPostgres.DataLayer do
|> AshSql.Bindings.default_bindings(resource, AshPostgres.SqlImplementation)

upsert_set =
upsert_set(resource, changesets, options)
upsert_set(
resource,
changesets,
options[:upsert_keys] || Ash.Resource.Info.primary_key(resource),
options
)

on_conflict =
case AshSql.Atomics.query_with_atomics(
Expand All @@ -1785,7 +1795,11 @@ defmodule AshPostgres.DataLayer do
upsert_set
) do
:empty ->
{:replace, options[:upsert_keys] || Ash.Resource.Info.primary_key(resource)}
if options[:return_records?] do
{:replace, options[:upsert_keys] || Ash.Resource.Info.primary_key(resource)}
else
:nothing
end

{:ok, query} ->
query
Expand Down Expand Up @@ -1913,7 +1927,7 @@ defmodule AshPostgres.DataLayer do
fun.()
end

defp upsert_set(resource, changesets, options) do
defp upsert_set(resource, changesets, keys, options) do
attributes_changing_anywhere =
changesets |> Enum.flat_map(&Map.keys(&1.attributes)) |> Enum.uniq()

Expand All @@ -1926,7 +1940,7 @@ defmodule AshPostgres.DataLayer do

fields_to_upsert =
upsert_fields --
Keyword.keys(Enum.at(changesets, 0).atomics)
Keyword.keys(Enum.at(changesets, 0).atomics) -- keys

fields_to_upsert
|> Enum.uniq()
Expand Down
7 changes: 7 additions & 0 deletions lib/repo.ex
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ defmodule AshPostgres.Repo do
@doc "The default prefix(postgres schema) to use when building queries"
@callback default_prefix() :: String.t()

@doc "Whether or not to explicitly start and close a transaction for each action, even if there are no transaction hooks"
@callback prefer_transaction?() :: boolean

@doc "Allows overriding a given migration type for *all* fields, for example if you wanted to always use :timestamptz for :utc_datetime fields"
@callback override_migration_type(atom) :: atom
@doc "Should the repo should be created by `mix ash_postgres.create`?"
Expand Down Expand Up @@ -102,6 +105,9 @@ defmodule AshPostgres.Repo do
def create?, do: true
def drop?, do: true

# default to false in 4.0
def prefer_transaction?, do: true

def transaction!(fun) do
case fun.() do
{:ok, value} -> value
Expand Down Expand Up @@ -242,6 +248,7 @@ defmodule AshPostgres.Repo do
on_transaction_begin: 1,
installed_extensions: 0,
all_tenants: 0,
prefer_transaction?: 0,
tenant_migrations_path: 0,
default_prefix: 0,
override_migration_type: 1,
Expand Down
2 changes: 0 additions & 2 deletions test/cve/empty_atomic_non_bulk_actions_policy_bypass_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ defmodule AshPostgres.EmptyAtomicNonBulkActionsPolicyBypassTest do
|> Ash.Changeset.for_create(:create, %{})
|> Ash.create!()

Logger.configure(level: :debug)

assert_raise Ash.Error.Forbidden, fn ->
post
|> Ash.Changeset.for_update(:empty_update, %{}, authorize?: true)
Expand Down
2 changes: 2 additions & 0 deletions test/support/test_repo.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ defmodule AshPostgres.TestRepo do
send(self(), data)
end

def prefer_transaction?, do: false

def installed_extensions do
["ash-functions", "uuid-ossp", "pg_trgm", "citext", AshPostgres.TestCustomExtension, "ltree"] --
Application.get_env(:ash_postgres, :no_extensions, [])
Expand Down
2 changes: 1 addition & 1 deletion test/test_helper.exs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ExUnit.start(capture_log: true)
ExUnit.start(capture_log: false)

exclude_tags =
case System.get_env("PG_VERSION") do
Expand Down

0 comments on commit d861ce0

Please sign in to comment.