-
Notifications
You must be signed in to change notification settings - Fork 83
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Wrap discard methods in a transaction #84
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We discussed this on the phone 📞 which is why I left you no notes 😄
This seeks to solve issue: jhawthorn#77 Given the situation where a `before/after` callback fails, particularly with related records, we shouldn't still discard records. Note: This is leveraging a private API that exists in [ActiveRecord::Transactions](https://github.com/rails/rails/blob/cd2949d2d936daa89b7e23f816f1c004aee85461/activerecord/lib/active_record/transactions.rb#L345) Co-authored-by: Will Cosgrove <[email protected]>
Thanks, @willcosgrove! |
Waiting for this MR to get merged soon! 🙂 |
I haven't yet had time to review this to determine if I'm happy with the solution. Feel free to use your the branch directly until I get time. This project merges so few changes that it's unlikely you'll miss out on anything. 🙂 |
I just wrapped |
Faced this issue too. For now, wrapped |
@jarednorman friendly bump here 😄 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Aren't AR operations only run in transactions when AR callbacks are present? I don't think we want to wrap every discard in a transaction.
Can you reference how this is done in AR? I'd like to keep the implementation here minimal and in line with roughly how they do it, if we're going to make this change.
Hey @jarednorman - apologies that I'm just now responding. I missed the fact that you requested changes and responded with a question.
If I understand correctly, all persistence operations are run within a transaction. This is to account for situations like failures as the database level (ex: You can see in the picture that there's a transaction that is started, and then a rollback on the database error. I'm certainly open to dig into how Rails is exactly handling this, though. |
Hmmm. I'm struggling to find a reference to when AR uses transactions automatically. If all persistence operations are run in a transaction and discard uses the public persistence API for updating records, then we wouldn't have to do anything for them to be in transactions. It must be something more subtle than that. |
NOTE: This PR doesn’t currently include tests (I tested against the provided Ruby script).
Should this code be something you all want in the project, I will by all means add specs 😄
———
This seeks to solve issue: #77
Given the situation where a
before/after
callback fails, particularlywith related records, we shouldn't still discard records.
Note: This is leveraging a private API that exists in
ActiveRecord::Transactions
To run this against the failing spec referenced in the issue (with a few minor adjustments of my own):