chore: keep only correct changes from earlier reverts

This commit is contained in:
Zach Daniel 2024-07-17 15:08:19 -04:00
parent 05cdaa04c5
commit 6632f599bc
2 changed files with 65 additions and 24 deletions

View file

@ -2429,6 +2429,7 @@ defmodule Ash.Changeset do
)} )}
:error -> :error ->
if changeset.action.type == :update || Map.get(changeset.action, :soft?) do
[first_pkey_field | _] = Ash.Resource.Info.primary_key(changeset.resource) [first_pkey_field | _] = Ash.Resource.Info.primary_key(changeset.resource)
full_atomic_update = full_atomic_update =
@ -2457,6 +2458,19 @@ defmodule Ash.Changeset do
{:not_atomic, {:not_atomic,
"Failed to validate expression #{inspect(full_atomic_update)}: #{inspect(error)}"}} "Failed to validate expression #{inspect(full_atomic_update)}: #{inspect(error)}"}}
end end
else
{:cont,
filter(
changeset,
expr(
if ^condition_expr do
^error_expr
else
true
end
)
)}
end
end end
else else
{:expr, {:error, error}, expr} -> {:expr, {:error, error}, expr} ->

View file

@ -129,6 +129,10 @@ defmodule Ash.Test.Actions.BulkDestroyTest do
end end
end end
destroy :destroy_with_validation do
validate attribute_does_not_equal(:title, "can't delete")
end
destroy :destroy_with_argument do destroy :destroy_with_argument do
require_atomic? false require_atomic? false
@ -425,6 +429,29 @@ defmodule Ash.Test.Actions.BulkDestroyTest do
assert [] = Ash.read!(Post) assert [] = Ash.read!(Post)
end end
test "runs validations" do
assert_raise Ash.Error.Invalid, ~r/must not equal "can't delete"/, fn ->
assert %Ash.BulkResult{
records: [
%{title: "title1", title2: nil},
%{title: "title2", title2: nil}
]
} =
Ash.bulk_create!([%{title: "can't delete"}, %{title: "title2"}], Post, :create,
return_stream?: true,
return_records?: true
)
|> Stream.map(fn {:ok, result} ->
result
end)
|> Ash.bulk_destroy!(:destroy_with_validation, %{},
resource: Post,
return_records?: true,
return_errors?: true
)
end
end
test "runs after batch hooks" do test "runs after batch hooks" do
assert %Ash.BulkResult{ assert %Ash.BulkResult{
records: [ records: [