-
Notifications
You must be signed in to change notification settings - Fork 159
Description
I am wondering if it makes sense to have some sort of "bulk transition" or "bulk trigger" functionality, where I can supply a big list of models (or IDs), and an event, and get the behavior that would come with mymodels.each { |model| model.transition(from: :foo, to: :bar) }.
I think this is a use case many people probably have, and one which has pretty slow performance. I am hoping there's a way to transition many records, using fewer queries. I understand that this might not be possible, with concurrency concerns, etc. I am also totally open to having a significantly restricted API for the "bulk" version of a given task.
Anyway, please let me know if this seems realistic, or if there's a recommended workaround that I might use to do something like this in my own app!
I guess this is going to be tough since Rails 6 is the first version to offer bulk-inserts.
I've been thinking about what an "optimistic" approach to concurrency would look like here. Basically, look up all of the sort-keys of the existing most-recent transition records. Then begin a single transaction where the new transitions are inserted, and the relevant most_recent flags are updated. This way, if someone moves the DB into an unexpected state under my nose, my whole transaction will fail due to uniqueness constraint on the sort-keys.
Alternatively, I could actively lock/select for update those most-recent transition records, which would keep anyone from executing any harmful transitions under my nose.