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

distinct_count_by_large_foreign_key.rb « usage_data « cop « rubocop - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3aad089d961b4ea0e84013ebbecbc2db0a2792e7 (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
54
55
56
57
58
59
# frozen_string_literal: true

module RuboCop
  module Cop
    module UsageData
      # Allows counts only for selected tables' foreign keys for `distinct_count` method.
      #
      # Because distinct_counts over large tables' foreign keys will take a long time
      #
      # @example
      #
      #   # bad because pipeline_id points to a large table
      #   distinct_count(Ci::Build, :commit_id)
      #
      class DistinctCountByLargeForeignKey < RuboCop::Cop::Cop
        MSG = 'Avoid doing `%s` on foreign keys for large tables having above 100 million rows.'

        def_node_matcher :distinct_count?, <<-PATTERN
          (send _ $:distinct_count $...)
        PATTERN

        def on_send(node)
          distinct_count?(node) do |method_name, method_arguments|
            next unless method_arguments && method_arguments.length >= 2
            next if batch_set_to_false?(method_arguments[2])
            next if allowed_foreign_key?(method_arguments[1])

            add_offense(node, location: :selector, message: format(MSG, method_name))
          end
        end

        private

        def allowed_foreign_key?(key)
          [:sym, :str].include?(key.type) && allowed_foreign_keys.include?(key.value.to_s)
        end

        def allowed_foreign_keys
          (cop_config['AllowedForeignKeys'] || []).map(&:to_s)
        end

        def batch_set_to_false?(options)
          return false unless options.is_a?(RuboCop::AST::HashNode)

          batch_set_to_false = false
          options.each_pair do |key, value|
            next unless value.boolean_type? && value.falsey_literal?
            next unless key.type == :sym && key.value == :batch

            batch_set_to_false = true
            break
          end

          batch_set_to_false
        end
      end
    end
  end
end