2021-02-11 16:09:00 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-02-08 17:31:15 -05:00
|
|
|
require_relative '../../migration_helpers'
|
|
|
|
|
2016-06-24 11:26:28 -04:00
|
|
|
module RuboCop
|
|
|
|
module Cop
|
|
|
|
module Migration
|
|
|
|
# Cop that checks if indexes are added in a concurrent manner.
|
|
|
|
class AddIndex < RuboCop::Cop::Cop
|
|
|
|
include MigrationHelpers
|
|
|
|
|
2021-03-23 11:09:28 -04:00
|
|
|
MSG = '`add_index` requires downtime, use `add_concurrent_index` instead'
|
2016-06-24 11:26:28 -04:00
|
|
|
|
|
|
|
def on_def(node)
|
|
|
|
return unless in_migration?(node)
|
|
|
|
|
|
|
|
new_tables = []
|
|
|
|
|
|
|
|
node.each_descendant(:send) do |send_node|
|
|
|
|
first_arg = first_argument(send_node)
|
|
|
|
|
|
|
|
# The first argument of "create_table" / "add_index" is the table
|
|
|
|
# name.
|
|
|
|
new_tables << first_arg if create_table?(send_node)
|
|
|
|
|
|
|
|
next if method_name(send_node) != :add_index
|
|
|
|
|
|
|
|
# Using "add_index" is fine for newly created tables as there's no
|
|
|
|
# data in these tables yet.
|
|
|
|
next if new_tables.include?(first_arg)
|
|
|
|
|
2017-09-19 11:25:42 -04:00
|
|
|
add_offense(send_node, location: :selector)
|
2016-06-24 11:26:28 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_table?(node)
|
|
|
|
method_name(node) == :create_table
|
|
|
|
end
|
|
|
|
|
|
|
|
def method_name(node)
|
|
|
|
node.children[1]
|
|
|
|
end
|
|
|
|
|
|
|
|
def first_argument(node)
|
|
|
|
node.children[2] ? node.children[0] : nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|