summaryrefslogtreecommitdiff
path: root/rubocop/cop/migration
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2020-10-21 07:08:36 +0000
committerGitLab Bot <gitlab-bot@gitlab.com>2020-10-21 07:08:36 +0000
commit48aff82709769b098321c738f3444b9bdaa694c6 (patch)
treee00c7c43e2d9b603a5a6af576b1685e400410dee /rubocop/cop/migration
parent879f5329ee916a948223f8f43d77fba4da6cd028 (diff)
downloadgitlab-ce-48aff82709769b098321c738f3444b9bdaa694c6.tar.gz
Add latest changes from gitlab-org/gitlab@13-5-stable-eev13.5.0-rc42
Diffstat (limited to 'rubocop/cop/migration')
-rw-r--r--rubocop/cop/migration/add_concurrent_foreign_key.rb20
-rw-r--r--rubocop/cop/migration/add_limit_to_text_columns.rb10
-rw-r--r--rubocop/cop/migration/create_table_with_foreign_keys.rb2
-rw-r--r--rubocop/cop/migration/with_lock_retries_disallowed_method.rb18
4 files changed, 44 insertions, 6 deletions
diff --git a/rubocop/cop/migration/add_concurrent_foreign_key.rb b/rubocop/cop/migration/add_concurrent_foreign_key.rb
index 236de6224a4..31cf426b2d4 100644
--- a/rubocop/cop/migration/add_concurrent_foreign_key.rb
+++ b/rubocop/cop/migration/add_concurrent_foreign_key.rb
@@ -11,7 +11,11 @@ module RuboCop
MSG = '`add_foreign_key` requires downtime, use `add_concurrent_foreign_key` instead'.freeze
def_node_matcher :false_node?, <<~PATTERN
- (false)
+ (false)
+ PATTERN
+
+ def_node_matcher :with_lock_retries?, <<~PATTERN
+ (:send nil? :with_lock_retries)
PATTERN
def on_send(node)
@@ -19,9 +23,11 @@ module RuboCop
name = node.children[1]
- if name == :add_foreign_key && !not_valid_fk?(node)
- add_offense(node, location: :selector)
- end
+ return unless name == :add_foreign_key
+ return if in_with_lock_retries?(node)
+ return if not_valid_fk?(node)
+
+ add_offense(node, location: :selector)
end
def method_name(node)
@@ -33,6 +39,12 @@ module RuboCop
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
diff --git a/rubocop/cop/migration/add_limit_to_text_columns.rb b/rubocop/cop/migration/add_limit_to_text_columns.rb
index b578e73f19e..b2e37ad5137 100644
--- a/rubocop/cop/migration/add_limit_to_text_columns.rb
+++ b/rubocop/cop/migration/add_limit_to_text_columns.rb
@@ -6,6 +6,10 @@ module RuboCop
module Cop
module Migration
# Cop that enforces always adding a limit on text columns
+ #
+ # Text columns starting with `encrypted_` are very likely used
+ # by `attr_encrypted` which controls the text length. Those columns
+ # should not add a text limit.
class AddLimitToTextColumns < RuboCop::Cop::Cop
include MigrationHelpers
@@ -102,6 +106,8 @@ module RuboCop
# Check if there is an `add_text_limit` call for the provided
# table and attribute name
def text_limit_missing?(node, table_name, attribute_name)
+ return false if encrypted_attribute_name?(attribute_name)
+
limit_found = false
node.each_descendant(:send) do |send_node|
@@ -118,6 +124,10 @@ module RuboCop
!limit_found
end
+
+ def encrypted_attribute_name?(attribute_name)
+ attribute_name.to_s.start_with?('encrypted_')
+ end
end
end
end
diff --git a/rubocop/cop/migration/create_table_with_foreign_keys.rb b/rubocop/cop/migration/create_table_with_foreign_keys.rb
index 01cab032049..382a2d6f65b 100644
--- a/rubocop/cop/migration/create_table_with_foreign_keys.rb
+++ b/rubocop/cop/migration/create_table_with_foreign_keys.rb
@@ -9,7 +9,7 @@ module RuboCop
include MigrationHelpers
MSG = 'Creating a table with more than one foreign key at once violates our migration style guide. ' \
- 'For more details check the https://docs.gitlab.com/ce/development/migration_style_guide.html#examples'
+ 'For more details check the https://docs.gitlab.com/ee/development/migration_style_guide.html#examples'
def_node_matcher :create_table_with_block?, <<~PATTERN
(block
diff --git a/rubocop/cop/migration/with_lock_retries_disallowed_method.rb b/rubocop/cop/migration/with_lock_retries_disallowed_method.rb
index a89c33c298b..aef19517a9d 100644
--- a/rubocop/cop/migration/with_lock_retries_disallowed_method.rb
+++ b/rubocop/cop/migration/with_lock_retries_disallowed_method.rb
@@ -29,9 +29,14 @@ module RuboCop
].sort.freeze
MSG = "The method is not allowed to be called within the `with_lock_retries` block, the only allowed methods are: #{ALLOWED_MIGRATION_METHODS.join(', ')}"
+ MSG_ONLY_ONE_FK_ALLOWED = "Avoid adding more than one foreign key within the `with_lock_retries`. See https://docs.gitlab.com/ee/development/migration_style_guide.html#examples"
def_node_matcher :send_node?, <<~PATTERN
- send
+ send
+ PATTERN
+
+ def_node_matcher :add_foreign_key?, <<~PATTERN
+ (send nil? :add_foreign_key ...)
PATTERN
def on_block(node)
@@ -53,6 +58,17 @@ module RuboCop
name = node.children[1]
add_offense(node, location: :expression) unless ALLOWED_MIGRATION_METHODS.include?(name)
+ add_offense(node, location: :selector, message: MSG_ONLY_ONE_FK_ALLOWED) if multiple_fks?(node)
+ end
+
+ def multiple_fks?(node)
+ return unless add_foreign_key?(node)
+
+ count = node.parent.each_descendant(:send).count do |node|
+ add_foreign_key?(node)
+ end
+
+ count > 1
end
end
end