summaryrefslogtreecommitdiff
path: root/rubocop
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2020-03-30 15:07:51 +0000
committerGitLab Bot <gitlab-bot@gitlab.com>2020-03-30 15:07:51 +0000
commit4e9acbfba3682c552b3de707c535e6257ef41054 (patch)
tree8b1fd5f89ad3f1be68d8944815b13bb7d498e4a6 /rubocop
parent506d6dcd7c787ba71a8a53102f3d4fdb6adcfa5e (diff)
downloadgitlab-ce-4e9acbfba3682c552b3de707c535e6257ef41054.tar.gz
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'rubocop')
-rw-r--r--rubocop/cop/migration/add_columns_to_wide_tables.rb47
-rw-r--r--rubocop/migration_helpers.rb11
2 files changed, 57 insertions, 1 deletions
diff --git a/rubocop/cop/migration/add_columns_to_wide_tables.rb b/rubocop/cop/migration/add_columns_to_wide_tables.rb
new file mode 100644
index 00000000000..4618e4ae890
--- /dev/null
+++ b/rubocop/cop/migration/add_columns_to_wide_tables.rb
@@ -0,0 +1,47 @@
+# frozen_string_literal: true
+
+require_relative '../../migration_helpers'
+
+module RuboCop
+ module Cop
+ module Migration
+ # Cop that prevents adding columns to wide tables.
+ class AddColumnsToWideTables < RuboCop::Cop::Cop
+ include MigrationHelpers
+
+ MSG = '`%s` is a wide table with several columns, addig more should be avoided unless absolutely necessary.' \
+ ' Consider storing the column in a different table or creating a new one.'.freeze
+
+ BLACKLISTED_METHODS = %i[
+ add_column
+ add_column_with_default
+ add_reference
+ add_timestamps_with_timezone
+ ].freeze
+
+ def on_send(node)
+ return unless in_migration?(node)
+
+ method_name = node.children[1]
+ table_name = node.children[2]
+
+ return unless offense?(method_name, table_name)
+
+ add_offense(node, location: :selector, message: format(MSG, table_name.value))
+ end
+
+ private
+
+ def offense?(method_name, table_name)
+ wide_table?(table_name) &&
+ BLACKLISTED_METHODS.include?(method_name)
+ end
+
+ def wide_table?(table_name)
+ table_name && table_name.type == :sym &&
+ WIDE_TABLES.include?(table_name.value)
+ end
+ end
+ end
+ end
+end
diff --git a/rubocop/migration_helpers.rb b/rubocop/migration_helpers.rb
index b5edb502d4c..5fa6f8c2a2c 100644
--- a/rubocop/migration_helpers.rb
+++ b/rubocop/migration_helpers.rb
@@ -6,7 +6,7 @@ module RuboCop
plan_limits
].freeze
- # Blacklisted table due to:
+ # Blacklisted tables due to:
# - size in GB (>= 10 GB on GitLab.com as of 02/2020)
# - number of records
BLACKLISTED_TABLES = %i[
@@ -44,6 +44,15 @@ module RuboCop
web_hook_logs
].freeze
+ # Blacklisted tables due to:
+ # - number of columns (> 50 on GitLab.com as of 03/2020)
+ # - number of records
+ WIDE_TABLES = %i[
+ users
+ projects
+ ci_builds
+ ].freeze
+
# Returns true if the given node originated from the db/migrate directory.
def in_migration?(node)
dirname(node).end_with?('db/migrate', 'db/geo/migrate') || in_post_deployment_migration?(node)