Class: RuboCop::Cop::Migration::BatchInBatches

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Includes:
Migration::CopConcerns::BatchProcessing
Defined in:
lib/rubocop/cop/migration/batch_in_batches.rb

Overview

Use ‘in_batches` in batch processing.

For more efficient batch processing.

Examples:

# bad
class BackfillSomeColumn < ActiveRecord::Migration[7.0]
  disable_ddl_transaction!

  def change
    User.update_all(some_column: 'some value')
  end
end

# good
class BackfillSomeColumnToUsers < ActiveRecord::Migration[7.0]
  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
  end
end

Constant Summary collapse

MSG =
'Use `in_batches` in batch processing.'
RESTRICT_ON_SEND =
%i[
  delete_all
  update_all
].freeze

Constants included from Migration::CopConcerns::BatchProcessing

Migration::CopConcerns::BatchProcessing::BATCH_PROCESSING_METHOD_NAMES

Instance Method Summary collapse

Methods included from Migration::CopConcerns::BatchProcessing

included

Instance Method Details

#on_send(node) ⇒ void

This method returns an undefined value.

Parameters:

  • node (RuboCop::AST::SendNode)


57
58
59
60
61
62
63
# File 'lib/rubocop/cop/migration/batch_in_batches.rb', line 57

def on_send(node)
  return unless wrong?(node)

  add_offense(node) do |corrector|
    autocorrect(corrector, node)
  end
end