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
Add new Rails/PrivateTransactionOption
cop
#1236
base: master
Are you sure you want to change the base?
Add new Rails/PrivateTransactionOption
cop
#1236
Conversation
This PR adds a new cop called `Rails/PrivateTransactionOption`. This cop checks whether `ActiveRecord::Base.transaction(joinable: _)` is used. The `joinable` option is a private API and is not intended to be called from outside Active Record core. rails/rails#39912 (comment) rails/rails#46182 (comment) Passing `joinable: false` may cause unexpected behavior such as the `after_commit` callback not firing at the appropriate time.
Can you first propose an addition to the Rails Style Guide? I'm not sure which articles have made incorrect references, but including a description in the Style Guide that RuboCop Rails follows would serve as one countermeasure to them. |
Opened rubocop/rails-style-guide#355.
A quick Google search shows the following articles:
Some articles mention unintended side effects, while others do not. |
https://api.rubyonrails.org/v6.0/classes/ActiveRecord/ConnectionAdapters/DatabaseStatements.html#method-i-transaction documents |
#414 Related |
|
Some more context rspec/rspec-rails#2598 (comment) - I withdraw my statement that adding |
Related cop suggestion #400 |
Agreed with your opinion. I don't believe it's always right to require In reality, there may be situations where you have to use |
Should it be possible to wrap a call to a gem that doesn’t It is worth mentioning that when reasoning about nested transactions and business logic, the error kernel design pattern comes handy. |
As far as I know, there's no way around it. You need to fix the gem. |
This PR adds a new cop called
Rails/PrivateTransactionOption
.This cop checks whether
ActiveRecord::Base.transaction(joinable: _)
is used. Thejoinable
option is a private API and is not intended to be called from outside Active Record core.after_create_callback
is called before the transaction is commited started withjoinable: false
rails/rails#46182 (comment)Passing
joinable: false
may cause unexpected behavior such as theafter_commit
callback not firing at the appropriate time.See also kufu/activerecord-bitemporal#156
The motivation for wanting to add this is that even though the
joinable
is undocumented, it was recommended in some articles, so it may be inadvertently widely used. It is possible that it is being used incorrectly to simply enablerequires_new
implicitly without realizing that it affects the behavior ofafter_commit
.Before submitting the PR make sure the following are checked:
[Fix #issue-number]
(if the related issue exists).master
(if not - rebase it).bundle exec rake default
. It executes all tests and runs RuboCop on its own code.{change_type}_{change_description}.md
if the new code introduces user-observable changes. See changelog entry format for details.