Skip to content

fix: support before_action? validations in bulk_create and bulk_update#2634

Open
nallwhy wants to merge 1 commit intoash-project:mainfrom
nallwhy:fix/bulk-before-action-support
Open

fix: support before_action? validations in bulk_create and bulk_update#2634
nallwhy wants to merge 1 commit intoash-project:mainfrom
nallwhy:fix/bulk-before-action-support

Conversation

@nallwhy
Copy link
Contributor

@nallwhy nallwhy commented Mar 13, 2026

Contributor checklist

Leave anything that you believe does not apply unchecked.

  • I accept the AI Policy, or AI was not used in the creation of this PR.
  • Bug fixes include regression tests
  • Chores
  • Documentation changes
  • Features include unit/acceptance tests
  • Refactoring
  • Update dependencies

Summary

  • Add before_action? validation support in bulk create and bulk update stream paths
  • Wrap before_action? validations in Ash.Changeset.before_action/2 so they run at the correct point in the action lifecycle, consistent with non-bulk actions
  • Extract inline validation logic into run_bulk_validation/6 helper in bulk create for clarity

Current scope

This change only affects the stream (non-atomic) execution path. The before_action? check lives inside the has_validate?() branch, which is the non-atomic validation path. In the atomic path, validations go through validate_batch_atomically / atomic changeset expressions, which is a completely separate mechanism.

This means:

  • bulk_create: always works (creates always process individual changesets)
  • bulk_update with strategy: :stream: works
  • bulk_update with strategy: :atomic: before_action? flag is effectively ignored — the validation runs atomically as a DB expression regardless

What should happen in the atomic path?

There are a few options for how to handle before_action? validations when bulk_update runs atomically:

Option A: Stream-only support (current implementation)
The atomic path doesn't touch our code at all, so before_action? is silently a no-op there. This is the simplest approach but could be surprising — a user might set before_action?: true expecting deferred execution and not realize it's ignored in atomic mode.

Option B: Raise an error when atomic + before_action?
Add a check in validate_batch_atomically that raises if validation.before_action? is true, with a message like "before_action? validations cannot run atomically, use require_atomic? false or strategy: [:stream]". This is the most explicit approach — consistent with how Ash already handles atomic incompatibilities (e.g. require_atomic?).

Option C: Auto-fallback to stream
Detect before_action? validations before attempting atomic execution and automatically fall back to stream strategy. Convenient, but changes performance characteristics in a way the user might not expect.

Copilot AI review requested due to automatic review settings March 13, 2026 06:35
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds support (and regression tests) for before_action?: true global validations in bulk create/update flows, ensuring validation ordering matches non-bulk action behavior.

Changes:

  • Introduces a new ValidateStatusNotUgly validation used to exercise before_action? validation timing.
  • Updates bulk create/update validation execution to schedule before_action? validations via Ash.Changeset.before_action/2.
  • Expands bulk validation ordering tests for both bulk_create and bulk_update.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

File Description
test/actions/bulk/bulk_update_test.exs Adds a before_action? global validation and a new bulk update test asserting correct error ordering/behavior.
test/actions/bulk/bulk_create_test.exs Adds a before_action? global validation and a new bulk create test asserting correct error ordering/behavior.
lib/ash/actions/update/bulk.ex Schedules before_action? validations via before_action hooks and refactors error handling in non-atomic validation.
lib/ash/actions/create/bulk.ex Refactors bulk validation logic into run_bulk_validation/6 and adds before_action? scheduling.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 3677 to +3685
{: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)
Comment on lines +3507 to +3519
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)
@zachdaniel
Copy link
Contributor

Yeah, I think we should disallow before_action, your option B

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants