Welcome to mirror list, hosted at ThFree Co, Russian Federation.

add_index.rb « migration « cop « rubocop - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 506af97866f6f7c78ce34354ad3497c796a91a67 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
module RuboCop
  module Cop
    module Migration
      # Cop that checks if indexes are added in a concurrent manner.
      class AddIndex < RuboCop::Cop::Cop
        include MigrationHelpers

        MSG = '`add_index` requires downtime, use `add_concurrent_index` instead'

        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)

            add_offense(send_node, :selector)
          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