diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2022-01-20 12:16:11 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2022-01-20 12:16:11 +0300 |
commit | edaa33dee2ff2f7ea3fac488d41558eb5f86d68c (patch) | |
tree | 11f143effbfeba52329fb7afbd05e6e2a3790241 /rubocop/cop | |
parent | d8a5691316400a0f7ec4f83832698f1988eb27c1 (diff) |
Add latest changes from gitlab-org/gitlab@14-7-stable-eev14.7.0-rc42
Diffstat (limited to 'rubocop/cop')
3 files changed, 63 insertions, 25 deletions
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 |