Skip to content
Merged
Show file tree
Hide file tree
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
14 changes: 14 additions & 0 deletions lib/events/action_wrapper_helpers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,22 @@ defmodule AshEvents.Events.ActionWrapperHelpers do
end
end)

has_atomics? = not Enum.empty?(changeset.atomics)

event_log_resource
|> Ash.Changeset.for_create(:create, event_params, opts)
|> then(fn cs ->
if has_atomics? do
Ash.Changeset.add_error(
cs,
Ash.Error.Changes.InvalidChanges.exception(
message: "atomic changes are not compatible with ash_events"
)
)
else
cs
end
end)
|> Ash.create!()
end
end
35 changes: 35 additions & 0 deletions test/ash_events_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -361,4 +361,39 @@ defmodule AshEventsTest do
assert user.given_name == "John"
assert user.family_name == "Doe"
end

test "atomic changes throws error" do
assert_raise Ash.Error.Invalid, fn ->
Accounts.create_user_with_atomic(
%{
email: "user@example.com",
given_name: "John",
family_name: "Doe"
},
context: %{ash_events_metadata: %{source: "Signup form"}},
actor: %SystemActor{name: "test_runner"}
)
end

user = create_user()

assert_raise Ash.Error.Invalid, fn ->
Accounts.update_user_with_atomic(
user,
%{
given_name: "Jack",
family_name: "Smith"
},
actor: user
)
end

assert_raise Ash.Error.Invalid, fn ->
Accounts.destroy_user_with_atomic(
user,
%{},
actor: user
)
end
end
end
3 changes: 3 additions & 0 deletions test/support/accounts/domain.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ defmodule AshEvents.Test.Accounts do
resource AshEvents.Test.Accounts.User do
define :get_user_by_id, action: :get_by_id, args: [:id]
define :create_user, action: :create
define :create_user_with_atomic, action: :create_with_atomic
define :update_user, action: :update
define :update_user_with_atomic, action: :update_with_atomic
define :destroy_user, action: :destroy
define :destroy_user_with_atomic, action: :destroy_with_atomic
end

resource AshEvents.Test.Accounts.UserRole do
Expand Down
23 changes: 23 additions & 0 deletions test/support/accounts/user.ex
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ defmodule AshEvents.Test.Accounts.User do
change __MODULE__.CreateUserRole
end

create :create_with_atomic do
accept [:id, :created_at, :updated_at, :email, :given_name, :family_name]
argument :role, :string, default: "user"
change __MODULE__.CreateUserRole
change atomic_update(:given_name, expr(given_name + "should_fail"))
end

update :update do
require_atomic? false
accept [:given_name, :family_name]
Expand All @@ -34,12 +41,28 @@ defmodule AshEvents.Test.Accounts.User do
change __MODULE__.UpdateUserRole
end

update :update_with_atomic do
require_atomic? false
accept [:given_name, :family_name]
argument :role, :string, allow_nil?: true

change __MODULE__.UpdateUserRole
change atomic_update(:given_name, expr(given_name + "should_fail"))
end

destroy :destroy do
require_atomic? false
primary? true
accept []
end

destroy :destroy_with_atomic do
require_atomic? false
accept []

change atomic_update(:given_name, expr(given_name + "should_fail"))
end

read :get_by_id do
get_by [:id]
end
Expand Down
Loading