Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions test/actions/bulk/bulk_update_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,21 @@ defmodule Ash.Test.Actions.BulkUpdateTest do
end
end

defmodule AtomicWithAfterTransaction do
use Ash.Resource.Change

def change(changeset, _opts, _context) do
Ash.Changeset.after_transaction(changeset, fn _changeset, {:ok, result} ->
send(self(), {:after_transaction_called, result.id})
{:ok, result}
end)
end

def atomic(changeset, opts, context) do
{:ok, change(changeset, opts, context)}
end
end

defmodule AtomicallyRequireActor do
use Ash.Resource.Change

Expand Down Expand Up @@ -346,6 +361,10 @@ defmodule Ash.Test.Actions.BulkUpdateTest do
update :update_with_filter do
change filter(expr(title == "foo"))
end

update :update_with_atomic_after_transaction do
change AtomicWithAfterTransaction
end
end

identities do
Expand Down Expand Up @@ -1750,4 +1769,24 @@ defmodule Ash.Test.Actions.BulkUpdateTest do
refute Enum.empty?(notifications)
end
end

describe "atomic changes with after_transaction hooks" do
test "after_transaction hooks cannot be added in atomic changes" do
post =
Post
|> Ash.Changeset.for_create(:create, %{title: "test"})
|> Ash.create!()

# Attempting to use an atomic change that adds an after_transaction hook
# currently fails with an error.
assert_raise Ash.Error.Invalid, ~r/Cannot add after_transaction hooks/, fn ->
Post
|> Ash.Query.filter(id == ^post.id)
|> Ash.bulk_update!(:update_with_atomic_after_transaction, %{},
return_records?: true,
strategy: :atomic
)
end
end
end
end
Loading