Class: RuboCop::Cop::Migration::AddIndexConcurrently

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Includes:
RangeHelp, Migration::CopConcerns::DisableDdlTransaction
Defined in:
lib/rubocop/cop/migration/add_index_concurrently.rb

Overview

Use ‘algorithm: :concurrently` on adding indexes to existing tables in PostgreSQL.

To avoid blocking writes.

Examples:

# bad
class AddIndexToUsersName < ActiveRecord::Migration[7.0]
  def change
    add_index :users, :name
  end
end

# good
class AddIndexToUsersNameConcurrently < ActiveRecord::Migration[7.0]
  disable_ddl_transaction!

  def change
    add_index :users, :name, algorithm: :concurrently
  end
end

Constant Summary collapse

MSG =
'Use `algorithm: :concurrently` on adding indexes to existing tables in PostgreSQL.'
MESSAGE_FOR_DUPLICATED_DISABLE_DDL_TRANSACTION =
'Remove duplicated `disable_ddl_transaction!`.'
RESTRICT_ON_SEND =
%i[
  add_index
  index
].freeze

Instance Method Summary collapse

Methods included from Migration::CopConcerns::DisableDdlTransaction

included

Instance Method Details

#on_send(node) ⇒ void

This method returns an undefined value.

Parameters:

  • node (RuboCop::AST::SendNode)


46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/rubocop/cop/migration/add_index_concurrently.rb', line 46

def on_send(node)
  if add_index_without_concurrency?(node)
    add_offense(node) do |corrector|
      autocorrect(corrector, node)
    end
  end

  duplicated_disable_ddl_transactions_from(node).each do |disable_ddl_transactions_node|
    add_offense(node, message: MESSAGE_FOR_DUPLICATED_DISABLE_DDL_TRANSACTION) do |corrector|
      corrector.remove(
        range_with_surrounding_space(
          disable_ddl_transactions_node.source_range,
          side: :left
        )
      )
    end
  end
end