Best way to run bulk ops in a transaction
I’m new to Ash, and I have a fairly simple code problem. I need to delete all the records for a resource for a given user and then bulk create new ones of the same resource for the same user. I want these two bulk operations to be in a transaction so they’re rolled back on error. I’ve been able to do this in a generic action by using bulk_destroy followed by bulk_create. However, I’ve not been able to figure out how to do this in a multi-step way by using callbacks (to delete first) which seems like it would be the Ash way.
Am I missing something or is a generic action the way to go?
5 Replies
A generic action is a very reasonable solution to this task. However, if you want it to be "hooks all the way down" (which there isn't generally a reason to do), or if you want to compose it using changes etc. then you could make it an update action on users
and those can define hooks etc.
we'e recently created a new guide that may help
Hexdocs Search Results
Searched
ash-3.5.15
for multi-step actions
ash-3.5.15 | extras
Thank you! Would batch_change be relevant to use in this case? I think I was confused about having to use that due to using bulk functions.
The batch change is for attaching behavior to batches of logic, but isn't required for calling actions. An example might be a change that requires some other data from the db. You might write a batch version of it that can optimize the behavior for batches of records.
Appreciate it!