Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2020-02-19 21:09:10 +0300
committerGitLab Bot <gitlab-bot@gitlab.com>2020-02-19 21:09:10 +0300
commit33795139ea8e72756bee3675b4e16387425e6ab1 (patch)
tree3ca568fca61482e57810ee30ad5ce4b964a82c4e /rubocop/cop/migration
parentc7e385e282bcb8505589bce526e692b7bb819ffa (diff)
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'rubocop/cop/migration')
-rw-r--r--rubocop/cop/migration/schedule_async.rb54
1 files changed, 54 insertions, 0 deletions
diff --git a/rubocop/cop/migration/schedule_async.rb b/rubocop/cop/migration/schedule_async.rb
new file mode 100644
index 00000000000..f296628c3d6
--- /dev/null
+++ b/rubocop/cop/migration/schedule_async.rb
@@ -0,0 +1,54 @@
+# frozen_string_literal: true
+
+require_relative '../../migration_helpers'
+
+module RuboCop
+ module Cop
+ module Migration
+ class ScheduleAsync < RuboCop::Cop::Cop
+ include MigrationHelpers
+
+ 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.
+ MSG
+
+ def_node_matcher :calls_background_migration_worker?, <<~PATTERN
+ (send (const nil? :BackgroundMigrationWorker) {:perform_async :perform_in :bulk_perform_async :bulk_perform_in} ... )
+ PATTERN
+
+ def on_send(node)
+ return unless in_migration?(node)
+ return if version(node) < ENFORCED_SINCE
+
+ 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..-1]
+ end
+ end
+ end
+ end
+end