Skip to content
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

Fix inline in_batches false-positive on Migration/BatchInBatches #16

Merged
merged 1 commit into from
Jan 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 25 additions & 4 deletions lib/rubocop/cop/migration/batch_in_batches.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ module Migration
# disable_ddl_transaction!
#
# def up
# User.in_batches.update_all(some_column: 'some value')
# end
# end
#
# # good
# class BackfillSomeColumnToUsers < ActiveRecord::Migration[7.0]
# disable_ddl_transaction!
#
# def up
# User.in_batches do |relation|
# relation.update_all(some_column: 'some value')
# end
Expand Down Expand Up @@ -75,19 +84,31 @@ def autocorrect(
)
end

# @param node [RuboCop::AST::Node]
# @param node [RuboCop::AST::SendNode]
# @return [Boolean]
def within_in_batches?(node)
def in_batches?(node)
in_block_batches?(node) || in_inline_batches?(node)
end

# @param node [RuboCop::AST::SendNode]
# @return [Boolean]
def in_block_batches?(node)
node.each_ancestor(:block).any? do |ancestor|
ancestor.method?(:in_batches)
end
end

# @param node [RuboCop::AST::SendNode]
# @return [Boolean]
def in_inline_batches?(node)
node.receiver.is_a?(::RuboCop::AST::SendNode) &&
node.receiver.method?(:in_batches)
end

# @param node [RuboCop::AST::SendNode]
# @return [Boolean]
def wrong?(node)
batch_processing?(node) &&
!within_in_batches?(node)
batch_processing?(node) && !in_batches?(node)
end
end
end
Expand Down
16 changes: 14 additions & 2 deletions spec/rubocop/cop/migration/batch_in_batches_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

RSpec.describe RuboCop::Cop::Migration::BatchInBatches, :config do
context 'when `update_all` is used within `in_batches`' do
context 'when `update_all` is used with block `in_batches`' do
it 'does not register an offense' do
expect_no_offenses(<<~RUBY)
class BackfillUsersSomeColumn < ActiveRecord::Migration[7.0]
Expand All @@ -15,7 +15,19 @@ def change
end
end

context 'when `update_all` is used not within `in_batches`' do
context 'when `update_all` is used with inline `in_batches`' do
it 'does not register an offense' do
expect_no_offenses(<<~RUBY)
class BackfillUsersSomeColumn < ActiveRecord::Migration[7.0]
def change
User.in_batches.update_all(some_column: 'some value')
end
end
RUBY
end
end

context 'when `update_all` is used not with block `in_batches`' do
it 'registers an offense' do
expect_offense(<<~RUBY)
class BackfillUsersSomeColumn < ActiveRecord::Migration[7.0]
Expand Down
Loading