Class: RuboCop::Cop::PostgresMigrationCops::AddIndexesConcurrently

Inherits:
RuboCop::Cop
  • Object
show all
Defined in:
lib/cops/add_indexes_concurrently.rb

Overview

Check that indexes are added concurrently with disable_ddl_transaction! thoughtbot.com/blog/how-to-create-postgres-indexes-concurrently-in

Examples:

# bad
 class SomeTableMigrations < ActiveRecord::Migration
   def change
     add_column :table, :some_column_1, :integer, default: 0, null: false
     add_column :table, :other_column, :string
     add_column :table, :doneit_at, :datetime
     add_index  :table,
                :other_column,
                unique: true
   end
 end

# good
 class SomeTableMigrations < ActiveRecord::Migration
   disable_ddl_transaction!
   def change
     add_column :table, :some_column_1, :integer, default: 0, null: false
     add_column :table, :other_column, :string
     add_column :table, :doneit_at, :datetime
     add_index  :table,
                :other_column,
                unique: true,
                algorithm: :concurrently
     end
 end

Constant Summary collapse

IGNORE_DDL =
'Concurrent indexes require "disable_ddl_transaction!"'
CONCURRENTLY =
"Indexes should be added with 'algorithm: :concurrently'"

Instance Method Summary collapse

Instance Method Details

#on_class(class_node) ⇒ Object



39
40
41
42
43
44
45
46
47
48
# File 'lib/cops/add_indexes_concurrently.rb', line 39

def on_class(class_node)
  is_migration = class_node.children.any? { |n| is_migration?(n) }
  return unless is_migration

  index_nodes = find_index_methods(class_node)
  return unless creating_indexes?(index_nodes)

  ensure_disable_dll_transaction_offense(class_node, index_nodes)
  ensure_indexes_added_non_concurrently_offenses(index_nodes)
end