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

add_concurrent_foreign_key.rb « migration « cop « rubocop - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f538207d336482390833a68d66e98abe3efafa42 (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
47
48
49
50
51
52
53
# frozen_string_literal: true

require_relative '../../migration_helpers'

module RuboCop
  module Cop
    module Migration
      # Cop that checks if `add_concurrent_foreign_key` is used instead of
      # `add_foreign_key`.
      class AddConcurrentForeignKey < RuboCop::Cop::Base
        include MigrationHelpers

        MSG = '`add_foreign_key` requires downtime, use `add_concurrent_foreign_key` instead'

        def_node_matcher :false_node?, <<~PATTERN
          (false)
        PATTERN

        def_node_matcher :with_lock_retries?, <<~PATTERN
          (:send nil? :with_lock_retries)
        PATTERN

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

          name = node.children[1]

          return unless name == :add_foreign_key
          return if in_with_lock_retries?(node)
          return if not_valid_fk?(node)

          add_offense(node.loc.selector)
        end

        def method_name(node)
          node.children.first
        end

        def not_valid_fk?(node)
          node.each_node(:pair).any? do |pair|
            pair.children[0].children[0] == :validate && false_node?(pair.children[1])
          end
        end

        def in_with_lock_retries?(node)
          node.each_ancestor(:block).any? do |parent|
            with_lock_retries?(parent.to_a.first)
          end
        end
      end
    end
  end
end