diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-08-18 12:10:05 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-08-18 12:10:05 +0300 |
commit | 89eff770d213e684b5fa4df121cb51a059e8d263 (patch) | |
tree | f2783e2aaf50ebba6051f11335a45029b8549d34 /rubocop | |
parent | 681ca59b6f81a3a0057e3d528c27bcf96c2edd1b (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'rubocop')
-rw-r--r-- | rubocop/cop/usage_data/distinct_count_by_large_foreign_key.rb | 43 | ||||
-rw-r--r-- | rubocop/rubocop-usage-data.yml | 13 |
2 files changed, 56 insertions, 0 deletions
diff --git a/rubocop/cop/usage_data/distinct_count_by_large_foreign_key.rb b/rubocop/cop/usage_data/distinct_count_by_large_foreign_key.rb new file mode 100644 index 00000000000..098410e5ad2 --- /dev/null +++ b/rubocop/cop/usage_data/distinct_count_by_large_foreign_key.rb @@ -0,0 +1,43 @@ +# 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` for large foreign keys.'.freeze + + 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 allowed_foreign_key?(method_arguments[1]) + + add_offense(node, location: :selector, message: format(MSG, method_name)) + end + end + + private + + def allowed_foreign_key?(key) + key.type == :sym && allowed_foreign_keys.include?(key.value) + end + + def allowed_foreign_keys + cop_config['AllowedForeignKeys'] || [] + end + end + end + end +end diff --git a/rubocop/rubocop-usage-data.yml b/rubocop/rubocop-usage-data.yml index 887d7aa9427..bd0dd58dcdf 100644 --- a/rubocop/rubocop-usage-data.yml +++ b/rubocop/rubocop-usage-data.yml @@ -30,3 +30,16 @@ UsageData/LargeTable: - :arel_table - :minimum - :maximum +UsageData/DistinctCountByLargeForeignKey: + Enabled: true + Include: + - 'lib/gitlab/usage_data.rb' + - 'ee/lib/ee/gitlab/usage_data.rb' + AllowedForeignKeys: + - :user_id + - :author_id + - :creator_id + - :owner_id + - :project_id + - :issue_id + - :merge_request_id |