Class: RuboCop::Cop::Migration::BatchWithThrottling

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

Overview

Use throttling in batch processing.

Examples:

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

  def change
    User.in_batches do |relation|
      relation.update_all(some_column: 'some value')
    end
  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')
      sleep(0.01)
    end
  end
end

Constant Summary collapse

MSG =
'Use throttling 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)


49
50
51
52
53
54
55
# File 'lib/rubocop/cop/migration/batch_with_throttling.rb', line 49

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

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