summaryrefslogtreecommitdiff
path: root/rubocop
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2022-01-20 09:16:11 +0000
committerGitLab Bot <gitlab-bot@gitlab.com>2022-01-20 09:16:11 +0000
commitedaa33dee2ff2f7ea3fac488d41558eb5f86d68c (patch)
tree11f143effbfeba52329fb7afbd05e6e2a3790241 /rubocop
parentd8a5691316400a0f7ec4f83832698f1988eb27c1 (diff)
downloadgitlab-ce-edaa33dee2ff2f7ea3fac488d41558eb5f86d68c.tar.gz
Add latest changes from gitlab-org/gitlab@14-7-stable-eev14.7.0-rc42
Diffstat (limited to 'rubocop')
-rw-r--r--rubocop/code_reuse_helpers.rb16
-rw-r--r--rubocop/cop/database/establish_connection.rb20
-rw-r--r--rubocop/cop/migration/prevent_global_enable_lock_retries_with_disable_ddl_transaction.rb41
-rw-r--r--rubocop/cop/migration/schedule_async.rb27
4 files changed, 71 insertions, 33 deletions
diff --git a/rubocop/code_reuse_helpers.rb b/rubocop/code_reuse_helpers.rb
index 6ea12999cae..45cfa7ba78d 100644
--- a/rubocop/code_reuse_helpers.rb
+++ b/rubocop/code_reuse_helpers.rb
@@ -1,7 +1,15 @@
# frozen_string_literal: true
+require 'forwardable'
+
+require_relative '../lib/gitlab_edition'
+
module RuboCop
module CodeReuseHelpers
+ extend Forwardable
+
+ def_delegators :GitlabEdition, :ee?, :jh?
+
# Returns true for a `(send const ...)` node.
def send_to_constant?(node)
node.type == :send && node.children&.first&.type == :const
@@ -180,13 +188,5 @@ module RuboCop
def rails_root
File.expand_path('..', __dir__)
end
-
- def ee?
- File.exist?(File.expand_path('../ee/app/models/license.rb', __dir__)) && !%w[true 1].include?(ENV['FOSS_ONLY'].to_s)
- end
-
- def jh?
- ee? && Dir.exist?(File.expand_path('../jh', __dir__)) && !%w[true 1].include?(ENV['EE_ONLY'].to_s)
- end
end
end
diff --git a/rubocop/cop/database/establish_connection.rb b/rubocop/cop/database/establish_connection.rb
new file mode 100644
index 00000000000..20454887ce2
--- /dev/null
+++ b/rubocop/cop/database/establish_connection.rb
@@ -0,0 +1,20 @@
+# frozen_string_literal: true
+
+module RuboCop
+ module Cop
+ module Database
+ class EstablishConnection < RuboCop::Cop::Cop
+ MSG = "Don't establish new database connections, as this slows down " \
+ 'tests and may result in new connections using an incorrect configuration'
+
+ def_node_matcher :establish_connection?, <<~PATTERN
+ (send (const ...) :establish_connection ...)
+ PATTERN
+
+ def on_send(node)
+ add_offense(node, location: :expression) if establish_connection?(node)
+ end
+ end
+ end
+ end
+end
diff --git a/rubocop/cop/migration/prevent_global_enable_lock_retries_with_disable_ddl_transaction.rb b/rubocop/cop/migration/prevent_global_enable_lock_retries_with_disable_ddl_transaction.rb
new file mode 100644
index 00000000000..f5343f973e1
--- /dev/null
+++ b/rubocop/cop/migration/prevent_global_enable_lock_retries_with_disable_ddl_transaction.rb
@@ -0,0 +1,41 @@
+# frozen_string_literal: true
+
+require_relative '../../migration_helpers'
+
+module RuboCop
+ module Cop
+ module Migration
+ # Cop that prevents usage of `enable_lock_retries!` within the `disable_ddl_transaction!` method.
+ class PreventGlobalEnableLockRetriesWithDisableDdlTransaction < RuboCop::Cop::Cop
+ include MigrationHelpers
+
+ MSG = '`enable_lock_retries!` cannot be used with `disable_ddl_transaction!`. Use the `with_lock_retries` helper method to define retriable code blocks.'
+
+ def_node_matcher :enable_lock_retries?, <<~PATTERN
+ (send _ :enable_lock_retries! ...)
+ PATTERN
+
+ def_node_matcher :disable_ddl_transaction?, <<~PATTERN
+ (send _ :disable_ddl_transaction! ...)
+ PATTERN
+
+ def on_begin(node)
+ return unless in_migration?(node)
+
+ has_enable_lock_retries = false
+ has_disable_ddl_transaction = false
+
+ node.each_descendant(:send) do |send_node|
+ has_enable_lock_retries = true if enable_lock_retries?(send_node)
+ has_disable_ddl_transaction = true if disable_ddl_transaction?(send_node)
+
+ if has_enable_lock_retries && has_disable_ddl_transaction
+ add_offense(send_node, message: MSG)
+ break
+ end
+ end
+ end
+ end
+ end
+ end
+end
diff --git a/rubocop/cop/migration/schedule_async.rb b/rubocop/cop/migration/schedule_async.rb
index 74bd2baffa9..f31bfa46aa7 100644
--- a/rubocop/cop/migration/schedule_async.rb
+++ b/rubocop/cop/migration/schedule_async.rb
@@ -11,9 +11,8 @@ module RuboCop
ENFORCED_SINCE = 2020_02_12_00_00_00
MSG = <<~MSG
- Don't call the background migration worker directly, use the `#migrate_async`,
- `#migrate_in`, `#bulk_migrate_async` or `#bulk_migrate_in` migration helpers
- instead.
+ Don't call the background migration worker directly, use the `#migrate_in` or
+ `#queue_background_migration_jobs_by_range_at_intervals` migration helpers instead.
MSG
def_node_matcher :calls_background_migration_worker?, <<~PATTERN
@@ -26,28 +25,6 @@ module RuboCop
add_offense(node, location: :expression) if calls_background_migration_worker?(node)
end
-
- def autocorrect(node)
- # This gets rid of the receiver `BackgroundMigrationWorker` and
- # replaces `perform` with `schedule`
- schedule_method = method_name(node).to_s.sub('perform', 'migrate')
- arguments = arguments(node).map(&:source).join(', ')
-
- replacement = "#{schedule_method}(#{arguments})"
- lambda do |corrector|
- corrector.replace(node.source_range, replacement)
- end
- end
-
- private
-
- def method_name(node)
- node.children.second
- end
-
- def arguments(node)
- node.children[2..]
- end
end
end
end