diff --git a/lib/ash/error/framework/can_not_be_atomic.ex b/lib/ash/error/framework/can_not_be_atomic.ex new file mode 100644 index 00000000..c98e8cec --- /dev/null +++ b/lib/ash/error/framework/can_not_be_atomic.ex @@ -0,0 +1,14 @@ +defmodule Ash.Error.Framework.CanNotBeAtomic do + @moduledoc "Used when a change that is only atomic cannot be done atomically" + use Ash.Error.Exception + + use Splode.Error, fields: [:resource, :change, :reason], class: :framework + + def message(error) do + """ + #{inspect(error.resource)} #{error.change} only has an atomic/3 callback, but cannot be performed atomically + + Reason: #{error.reason} + """ + end +end diff --git a/test/actions/bulk/bulk_create_test.exs b/test/actions/bulk/bulk_create_test.exs index 57476d7a..a2254ea3 100644 --- a/test/actions/bulk/bulk_create_test.exs +++ b/test/actions/bulk/bulk_create_test.exs @@ -164,11 +164,6 @@ defmodule Ash.Test.Actions.BulkCreateTest do global? true end - validations do - validate AtomicOnlyValidation, - on: [:create] - end - calculations do calculate :hidden_calc, :string, expr("something") do public?(true) @@ -243,6 +238,10 @@ defmodule Ash.Test.Actions.BulkCreateTest do change set_context(%{authorize?: arg(:authorize?)}) end + + create :create_with_atomic_only_validation do + validate AtomicOnlyValidation + end end identities do @@ -1289,4 +1288,19 @@ defmodule Ash.Test.Actions.BulkCreateTest do } = tag.related_tags end end + + test "shows an error if an atomic only validation is used in a create" do + assert_raise Ash.Error.Framework.CanNotBeAtomic, + ~r/Post AtomicOnlyValidation only has an atomic\/3 callback, but cannot be performed atomically/, + fn -> + [%{title: "title1", authorize?: true}, %{title: "title2", authorize?: true}] + |> Ash.bulk_create!( + Post, + :create_with_atomic_only_validation, + authorize?: true, + return_stream?: true, + return_records?: true + ) + end + end end diff --git a/test/actions/create_test.exs b/test/actions/create_test.exs index 9f43c1d1..30d077c8 100644 --- a/test/actions/create_test.exs +++ b/test/actions/create_test.exs @@ -255,10 +255,6 @@ defmodule Ash.Test.Actions.CreateTest do private?(true) end - validations do - validate AtomicOnlyValidation - end - actions do default_accept :* defaults [:read, :destroy, create: :*, update: :*] @@ -270,6 +266,10 @@ defmodule Ash.Test.Actions.CreateTest do create :create_with_nested_array_argument do argument :array_of_names, {:array, {:array, :string}} end + + create :with_atomic_only_validations do + validate AtomicOnlyValidation + end end changes do @@ -1425,4 +1425,14 @@ defmodule Ash.Test.Actions.CreateTest do assert_receive {:error, _error} end + + test "shows an error if an atomic only validation is used in a create" do + assert_raise Ash.Error.Framework.CanNotBeAtomic, + ~r/Post AtomicOnlyValidation only has an atomic\/3 callback, but cannot be performed atomically/, + fn -> + Post + |> Ash.Changeset.for_create(:with_atomic_only_validations, %{title: "foo"}) + |> Ash.create!() + end + end end