Skip to content

Commit

Permalink
fix: ensure that around_transaction and around_action hooks incur sim…
Browse files Browse the repository at this point in the history
…ple updates
  • Loading branch information
zachdaniel committed Sep 19, 2024
1 parent 7ddc424 commit 66cc0e4
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/ash/actions/update/bulk.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1510,7 +1510,8 @@ defmodule Ash.Actions.Update.Bulk do

{batch, must_be_simple} =
Enum.reduce(batch, {[], []}, fn changeset, {batch, must_be_simple} ->
if changeset.after_transaction in [[], nil] do
if changeset.around_transaction in [[], nil] and changeset.after_transaction in [[], nil] and
changeset.around_action in [[], nil] do
changeset = Ash.Changeset.run_before_transaction_hooks(changeset)
{[changeset | batch], must_be_simple}
else
Expand Down
26 changes: 26 additions & 0 deletions test/actions/atomic_update_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,16 @@ defmodule Ash.Test.Actions.AtomicUpdateTest do
validate compare(:score, greater_than_or_equal_to: 0, less_than_or_equal_to: 10)
end

update :with_around_action do
require_atomic? false

change fn changeset, _ ->
Ash.Changeset.around_action(changeset, fn changeset, callback ->
raise "Around action!"
end)
end
end

update :increment_score do
accept []
change increment(:score, amount: 1, overflow_limit: 5), always_atomic?: true
Expand Down Expand Up @@ -106,6 +116,22 @@ defmodule Ash.Test.Actions.AtomicUpdateTest do
assert changeset.valid?
end

test "a changes" do
author =
Author
|> Ash.Changeset.for_create(:create, %{name: "fred", score: 0})
|> Ash.create!()

assert_raise Ash.Error.Unknown, ~r/Around action/, fn ->
Author
|> Ash.Query.filter(id == ^author.id)
|> Ash.bulk_update!(:with_around_action, %{name: "george"},
return_errors?: true,
strategy: :stream
)
end
end

test "values are eagerly validated" do
changeset =
Ash.Changeset.fully_atomic_changeset(Author, :with_validation, %{name: "fred weasly"})
Expand Down

0 comments on commit 66cc0e4

Please sign in to comment.