Skip to content

fix: support atomic-only changes in bulk_create#2635

Merged
zachdaniel merged 2 commits intoash-project:mainfrom
barnabasJ:fix/bulk-create-atomic-set
Mar 13, 2026
Merged

fix: support atomic-only changes in bulk_create#2635
zachdaniel merged 2 commits intoash-project:mainfrom
barnabasJ:fix/bulk-create-atomic-set

Conversation

@barnabasJ
Copy link
Contributor

Summary

  • Refactors batch_change in Ash.Actions.Create.Bulk to support change modules that only implement atomic/3 (no change/3 or batch_change/3)
  • Previously, such modules would crash in batch_change because the fallback always called change/3, which doesn't exist on atomic-only modules
  • Adds a cond-based dispatch (has_batch_change?has_change?atomic?) replacing the old 2-branch if/else
  • Extracts batch_atomic_change/6 helper to avoid duplication across the templated and non-templated code paths

Test plan

  • Added test for atomic_set with per-record arguments in bulk_create (multi-record and single-record)
  • Added test for atomic-only change module (no change/3) exercising the new atomic? fallback path
  • Added test for {:not_atomic, reason} error handling in bulk context
  • All 63 existing bulk_create tests pass
  • mix compile --warnings-as-errors clean
  • mix format --check-formatted clean

The `batch_change` function in `create/bulk.ex` only called `change/3`
on change modules, never checking `module.atomic?()` or calling
`run_atomic_change`. This meant `atomic_set` expressions were evaluated
in-memory rather than being passed to the database as SQL.

The update bulk path (`update/bulk.ex`) already had the correct
three-branch cond: batch_change > change > atomic. This commit adds the
same `module.atomic?()` branch to the create bulk path.

Without this fix, `atomic_set` with expressions like SQL fragments would
silently produce incorrect values when used with `Ash.bulk_create!`.
Add tests for change modules that only implement atomic/3 (no change/3
or batch_change/3) and for the {:not_atomic, reason} error path.
Extract batch_atomic_change/6 helper to deduplicate identical blocks.
@zachdaniel
Copy link
Contributor

This is cool, I'm actually not even sure if atomic only changes properly work in creates? I added support for atomics in creates but I don't recall how I handled that 🤔

@zachdaniel zachdaniel merged commit d44256a into ash-project:main Mar 13, 2026
45 checks passed
@zachdaniel
Copy link
Contributor

🚀 Thank you for your contribution! 🚀

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.

2 participants