diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2022-04-29 12:09:48 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2022-04-29 12:09:48 +0300 |
commit | 7510fe06eba02c3cee247f8ceb4ee6f6a4de54f6 (patch) | |
tree | 95025711e64d000172e47209bfdc1d7cd7d6b972 /rubocop | |
parent | 401607eed7e0918553e985e1f12e99dc1ff9e948 (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'rubocop')
-rw-r--r-- | rubocop/cop/database/multiple_databases.rb | 2 | ||||
-rw-r--r-- | rubocop/cop/migration/background_migration_record.rb | 41 | ||||
-rw-r--r-- | rubocop/migration_helpers.rb | 11 |
3 files changed, 52 insertions, 2 deletions
diff --git a/rubocop/cop/database/multiple_databases.rb b/rubocop/cop/database/multiple_databases.rb index f20348d9d1f..470c282f60f 100644 --- a/rubocop/cop/database/multiple_databases.rb +++ b/rubocop/cop/database/multiple_databases.rb @@ -22,7 +22,7 @@ module RuboCop ].freeze def_node_matcher :active_record_base_method_is_used?, <<~PATTERN - (send (const (const nil? :ActiveRecord) :Base) $_) + (send (const (const _ :ActiveRecord) :Base) $_) PATTERN def on_send(node) diff --git a/rubocop/cop/migration/background_migration_record.rb b/rubocop/cop/migration/background_migration_record.rb new file mode 100644 index 00000000000..2ee6b9f7b42 --- /dev/null +++ b/rubocop/cop/migration/background_migration_record.rb @@ -0,0 +1,41 @@ +# frozen_string_literal: true + +require_relative '../../migration_helpers' + +module RuboCop + module Cop + module Migration + class BackgroundMigrationRecord < RuboCop::Cop::Cop + include MigrationHelpers + + MSG = <<~MSG + Don't use or inherit from ActiveRecord::Base. + Use ::ApplicationRecord or ::Ci::ApplicationRecord to ensure the correct database is used. + See https://docs.gitlab.com/ee/development/database/batched_background_migrations.html#accessing-data-for-multiple-databases. + MSG + + def_node_matcher :inherits_from_active_record_base?, <<~PATTERN + (class _ (const (const _ :ActiveRecord) :Base) _) + PATTERN + + def_node_search :class_new_active_record_base?, <<~PATTERN + (send (const _ :Class) :new (const (const _ :ActiveRecord) :Base) ...) + PATTERN + + def on_class(node) + return unless in_background_migration?(node) + return unless inherits_from_active_record_base?(node) + + add_offense(node, location: :expression) + end + + def on_send(node) + return unless in_background_migration?(node) + return unless class_new_active_record_base?(node) + + add_offense(node, location: :expression) + end + end + end + end +end diff --git a/rubocop/migration_helpers.rb b/rubocop/migration_helpers.rb index 63b3766e126..f14f4d33709 100644 --- a/rubocop/migration_helpers.rb +++ b/rubocop/migration_helpers.rb @@ -32,6 +32,11 @@ module RuboCop in_deployment_migration?(node) || in_post_deployment_migration?(node) end + def in_background_migration?(node) + filepath(node).include?('/lib/gitlab/background_migration/') || + filepath(node).include?('/ee/lib/ee/gitlab/background_migration/') + end + def in_deployment_migration?(node) dirname(node).end_with?('db/migrate', 'db/geo/migrate') end @@ -56,8 +61,12 @@ module RuboCop private + def filepath(node) + node.location.expression.source_buffer.name + end + def dirname(node) - File.dirname(node.location.expression.source_buffer.name) + File.dirname(filepath(node)) end def rubocop_migrations_config |