-
-
Notifications
You must be signed in to change notification settings - Fork 263
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
Cop Idea: Disallow return
/ break
/ throw
in transaction
#642
Comments
Can you write bad and good examples? The specification will be clearer. |
@koic Thank you. I've updated the issue description. 👍🏼 |
Hi guys! My organisation was talking about implementing a cop just for this, so I thought it would be cool to open a PR for this.
As a side note, it's not a good idea to raise ActiveRecord::Rollback in general as it can have unwanted effects (see https://api.rubyonrails.org/classes/ActiveRecord/Transactions/ClassMethods.html, in "Nested Transactions"). What would be better is to just raise an error in my opinion. @ybiquitous |
[Fix #642] Add new `Rails/TransactionExitStatement` cop
@teckwan Thank you so much for implementing the new cop and sharing the caution about |
Is your feature request related to a problem? Please describe.
The use of
return
,break
, orthrow
in anActiveRecord::Base.transaction
block was deprecated since Rails 6.1.0. (rails/rails@31be40d)And with Rails 7.0.0, the behavior has changed to rollback. (rails/rails@15aa420)
For example, the following code seems unclear to me:
Describe the solution you'd like
So, I suggest a new cop detect the use of
return
,break
, orthrow
in atransaction
block:Here is a PoC:
This new cop suggests to:
return
,break
, orthrow
raise ActiveRecord::Rollback
if hoping rollbackEDIT: I've added the bad/good examples to clarify this Cop's advantage as below:
Describe alternatives you've considered
No alternatives.
Additional context
No additional context.
The text was updated successfully, but these errors were encountered: