fix: support before_action? validations in bulk_create and bulk_update#2634
Open
nallwhy wants to merge 1 commit intoash-project:mainfrom
Open
fix: support before_action? validations in bulk_create and bulk_update#2634nallwhy wants to merge 1 commit intoash-project:mainfrom
nallwhy wants to merge 1 commit intoash-project:mainfrom
Conversation
There was a problem hiding this comment.
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
ValidateStatusNotUglyvalidation used to exercisebefore_action?validation timing. - Updates bulk create/update validation execution to schedule
before_action?validations viaAsh.Changeset.before_action/2. - Expands bulk validation ordering tests for both
bulk_createandbulk_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) |
Contributor
|
Yeah, I think we should disallow before_action, your option B |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Contributor checklist
Leave anything that you believe does not apply unchecked.
Summary
before_action?validation support in bulk create and bulk update stream pathsbefore_action?validations inAsh.Changeset.before_action/2so they run at the correct point in the action lifecycle, consistent with non-bulk actionsrun_bulk_validation/6helper in bulk create for clarityCurrent scope
This change only affects the stream (non-atomic) execution path. The
before_action?check lives inside thehas_validate?()branch, which is the non-atomic validation path. In the atomic path, validations go throughvalidate_batch_atomically/ atomic changeset expressions, which is a completely separate mechanism.This means:
strategy: :stream: worksstrategy: :atomic:before_action?flag is effectively ignored — the validation runs atomically as a DB expression regardlessWhat 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 setbefore_action?: trueexpecting 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_atomicallythat raises ifvalidation.before_action?is true, with a message like "before_action? validations cannot run atomically, userequire_atomic? falseorstrategy: [: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.