Skip to content
Closed
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
192 changes: 102 additions & 90 deletions lib/ash/actions/create/bulk.ex
Original file line number Diff line number Diff line change
Expand Up @@ -389,14 +389,14 @@ defmodule Ash.Actions.Create.Bulk do

defp pre_template_all_changes(action, resource, :create, base, actor, tenant) do
action.changes
|> Enum.concat(Ash.Resource.Info.changes(resource, action.type))
|> then(fn changes ->
if action.skip_global_validations? do
changes
else
Enum.concat(changes, Ash.Resource.Info.validations(resource, action.type))
end
end)
|> Enum.concat(Ash.Resource.Info.changes(resource, action.type))
|> Enum.map(fn
%{change: {module, opts}} = change ->
%{change | change: {module, pre_template(opts, base, actor, tenant)}}
Expand Down Expand Up @@ -1845,95 +1845,17 @@ defmodule Ash.Actions.Create.Bulk do
fn
{%{validation: {module, opts}} = validation, _change_index}, %{batch: batch} = state ->
batch =
Enum.map(batch, fn changeset ->
cond do
!module.has_validate?() ->
Ash.Changeset.add_error(
changeset,
Ash.Error.Framework.CanNotBeAtomic.exception(
resource: changeset.resource,
change: module,
reason: "Create actions cannot be made atomic"
)
)

invalid =
Enum.find(validation.where, fn {module, _} -> !module.has_validate?() end) ->
{module, _} = invalid

Ash.Changeset.add_error(
changeset,
Ash.Error.Framework.CanNotBeAtomic.exception(
resource: changeset.resource,
change: module,
reason: "Create actions cannot be made atomic"
)
)

validation.only_when_valid? && !changeset.valid? ->
changeset

Enum.all?(validation.where || [], fn {module, opts} ->
opts =
templated_opts(
opts,
actor,
changeset.to_tenant,
changeset.arguments,
changeset.context,
changeset
)

{:ok, opts} = module.init(opts)

Ash.Resource.Validation.validate(
module,
changeset,
opts,
struct(Ash.Resource.Validation.Context, context)
) == :ok
end) ->
opts =
templated_opts(
opts,
actor,
changeset.to_tenant,
changeset.arguments,
changeset.context,
changeset
)

{:ok, opts} = module.init(opts)

case Ash.Resource.Validation.validate(
module,
changeset,
opts,
struct(
Ash.Resource.Validation.Context,
Map.put(context, :message, validation.message)
)
) do
:ok ->
changeset

{:error, error} ->
error = Ash.Error.to_ash_error(error)

if validation.message do
error =
Ash.Error.override_validation_message(error, validation.message)

Ash.Changeset.add_error(changeset, error)
else
Ash.Changeset.add_error(changeset, error)
end
end

true ->
changeset
end
end)
if validation.before_action? do
Enum.map(batch, fn changeset ->
Ash.Changeset.before_action(changeset, fn changeset ->
run_bulk_validation(changeset, validation, module, opts, context, actor)
end)
end)
else
Enum.map(batch, fn changeset ->
run_bulk_validation(changeset, validation, module, opts, context, actor)
end)
end

%{
state
Expand Down Expand Up @@ -2035,6 +1957,96 @@ defmodule Ash.Actions.Create.Bulk do
)
end

defp run_bulk_validation(changeset, validation, module, opts, context, actor) do
cond do
!module.has_validate?() ->
Ash.Changeset.add_error(
changeset,
Ash.Error.Framework.CanNotBeAtomic.exception(
resource: changeset.resource,
change: module,
reason: "Create actions cannot be made atomic"
)
)

invalid =
Enum.find(validation.where, fn {module, _} -> !module.has_validate?() end) ->
{module, _} = invalid

Ash.Changeset.add_error(
changeset,
Ash.Error.Framework.CanNotBeAtomic.exception(
resource: changeset.resource,
change: module,
reason: "Create actions cannot be made atomic"
)
)

validation.only_when_valid? && !changeset.valid? ->
changeset

Enum.all?(validation.where || [], fn {module, opts} ->
opts =
templated_opts(
opts,
actor,
changeset.to_tenant,
changeset.arguments,
changeset.context,
changeset
)

{:ok, opts} = module.init(opts)

Ash.Resource.Validation.validate(
module,
changeset,
opts,
struct(Ash.Resource.Validation.Context, context)
) == :ok
end) ->
opts =
templated_opts(
opts,
actor,
changeset.to_tenant,
changeset.arguments,
changeset.context,
changeset
)

{:ok, opts} = module.init(opts)

case Ash.Resource.Validation.validate(
module,
changeset,
opts,
struct(
Ash.Resource.Validation.Context,
Map.put(context, :message, validation.message)
)
) do
:ok ->
changeset

{:error, error} ->
error = Ash.Error.to_ash_error(error)

error =
if validation.message do
Ash.Error.override_validation_message(error, validation.message)
else
error
end

Ash.Changeset.add_error(changeset, error)
end

true ->
changeset
end
end

defp batch_change(module, batch, change_opts, context, actor) do
case change_opts do
{:templated, change_opts} ->
Expand Down
2 changes: 1 addition & 1 deletion lib/ash/actions/destroy/bulk.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1442,14 +1442,14 @@ defmodule Ash.Actions.Destroy.Bulk do

defp pre_template_all_changes(action, resource, :destroy, base, actor, tenant) do
action.changes
|> Enum.concat(Ash.Resource.Info.changes(resource, action.type))
|> then(fn changes ->
if action.skip_global_validations? do
changes
else
Enum.concat(changes, Ash.Resource.Info.validations(resource, action.type))
end
end)
|> Enum.concat(Ash.Resource.Info.changes(resource, action.type))
|> Enum.map(fn
%{change: {module, opts}} = change ->
%{change | change: {module, pre_template(opts, base, actor, tenant)}}
Expand Down
34 changes: 26 additions & 8 deletions lib/ash/actions/update/bulk.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1791,14 +1791,14 @@ defmodule Ash.Actions.Update.Bulk do

defp pre_template_all_changes(action, resource, _type, base, actor, tenant) do
action.changes
|> Enum.concat(Ash.Resource.Info.changes(resource, action.type))
|> then(fn changes ->
if action.skip_global_validations? do
changes
else
Enum.concat(changes, Ash.Resource.Info.validations(resource, action.type))
end
end)
|> Enum.concat(Ash.Resource.Info.changes(resource, action.type))
|> Enum.map(fn
%{change: {module, opts}} = change ->
%{change | change: {module, pre_template(opts, base, actor, tenant)}}
Expand Down Expand Up @@ -3504,7 +3504,23 @@ defmodule Ash.Actions.Update.Bulk do
Enum.all?(validation.where, fn {module, _opts} ->
module.has_validate?()
end) do
validate_batch_non_atomically(validation, batch, validation_context, actor)
if validation.before_action? do
Enum.map(batch, fn changeset ->
Ash.Changeset.before_action(changeset, fn changeset ->
[changeset] =
validate_batch_non_atomically(
validation,
[changeset],
validation_context,
actor
)

changeset
end)
end)
else
validate_batch_non_atomically(validation, batch, validation_context, actor)
end
else
if module.atomic?() do
validate_batch_atomically(validation, batch, validation_context, context, actor)
Expand Down Expand Up @@ -3659,12 +3675,14 @@ defmodule Ash.Actions.Update.Bulk do
changeset

{:error, error} ->
if validation.message do
error = Ash.Error.override_validation_message(error, validation.message)
Ash.Changeset.add_error(changeset, error)
else
Ash.Changeset.add_error(changeset, error)
end
error =
if validation.message do
Ash.Error.override_validation_message(error, validation.message)
else
error
end

Ash.Changeset.add_error(changeset, error)
end
else
changeset
Expand Down
Loading
Loading