summaryrefslogtreecommitdiff
path: root/rubocop
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2019-12-19 00:08:01 +0000
committerGitLab Bot <gitlab-bot@gitlab.com>2019-12-19 00:08:01 +0000
commit1caa60060b2f9e3417ab335e2f1dea1064163434 (patch)
tree01c0d5825bd345ee625bb70b7433c6e10307fcce /rubocop
parent7f8330873c1a5860b8a9a52d111083a65d210249 (diff)
downloadgitlab-ce-1caa60060b2f9e3417ab335e2f1dea1064163434.tar.gz
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'rubocop')
-rw-r--r--rubocop/cop/migration/add_column_with_default.rb64
-rw-r--r--rubocop/rubocop.rb1
2 files changed, 65 insertions, 0 deletions
diff --git a/rubocop/cop/migration/add_column_with_default.rb b/rubocop/cop/migration/add_column_with_default.rb
new file mode 100644
index 00000000000..8d1ab333dcf
--- /dev/null
+++ b/rubocop/cop/migration/add_column_with_default.rb
@@ -0,0 +1,64 @@
+# frozen_string_literal: true
+
+require_relative '../../migration_helpers'
+
+module RuboCop
+ module Cop
+ module Migration
+ # Cop that checks if columns are added in a way that doesn't require
+ # downtime.
+ class AddColumnWithDefault < RuboCop::Cop::Cop
+ include MigrationHelpers
+
+ WHITELISTED_TABLES = [:application_settings].freeze
+
+ MSG = '`add_column_with_default` with `allow_null: false` may cause prolonged lock situations and downtime, ' \
+ 'see https://gitlab.com/gitlab-org/gitlab/issues/38060'.freeze
+
+ def on_send(node)
+ return unless in_migration?(node)
+
+ name = node.children[1]
+
+ return unless name == :add_column_with_default
+
+ # Ignore whitelisted tables.
+ return if table_whitelisted?(node.children[2])
+
+ opts = node.children.last
+
+ return unless opts && opts.type == :hash
+
+ opts.each_node(:pair) do |pair|
+ if disallows_null_values?(pair)
+ add_offense(node, location: :selector)
+ end
+ end
+ end
+
+ def table_whitelisted?(symbol)
+ symbol && symbol.type == :sym &&
+ WHITELISTED_TABLES.include?(symbol.children[0])
+ end
+
+ def disallows_null_values?(pair)
+ options = [hash_key_type(pair), hash_key_name(pair), hash_value(pair)]
+
+ options == [:sym, :allow_null, :false] # rubocop:disable Lint/BooleanSymbol
+ end
+
+ def hash_key_type(pair)
+ pair.children[0].type
+ end
+
+ def hash_key_name(pair)
+ pair.children[0].children[0]
+ end
+
+ def hash_value(pair)
+ pair.children[1].type
+ end
+ end
+ end
+ end
+end
diff --git a/rubocop/rubocop.rb b/rubocop/rubocop.rb
index 1465c73d570..5f95703df01 100644
--- a/rubocop/rubocop.rb
+++ b/rubocop/rubocop.rb
@@ -17,6 +17,7 @@ require_relative 'cop/prefer_class_methods_over_module'
require_relative 'cop/put_project_routes_under_scope'
require_relative 'cop/put_group_routes_under_scope'
require_relative 'cop/migration/add_column'
+require_relative 'cop/migration/add_column_with_default'
require_relative 'cop/migration/add_concurrent_foreign_key'
require_relative 'cop/migration/add_concurrent_index'
require_relative 'cop/migration/add_index'