diff --git a/lib/gitlab/background_migration/backfill_approval_merge_request_rule_sources_project_id.rb b/lib/gitlab/background_migration/backfill_approval_merge_request_rule_sources_project_id.rb index 3595021fb6cceb19a0b78a7a71020f581a47a491..aad01a8d1a7cab6abf43b2a543e612791d868570 100644 --- a/lib/gitlab/background_migration/backfill_approval_merge_request_rule_sources_project_id.rb +++ b/lib/gitlab/background_migration/backfill_approval_merge_request_rule_sources_project_id.rb @@ -2,11 +2,9 @@ module Gitlab module BackgroundMigration - # rubocop: disable Migration/BackgroundMigrationBaseClass -- BackfillDesiredShardingKeyJob inherits from BatchedMigrationJob. class BackfillApprovalMergeRequestRuleSourcesProjectId < BackfillDesiredShardingKeyJob operation_name :backfill_approval_merge_request_rule_sources_project_id feature_category :code_review_workflow end - # rubocop: enable Migration/BackgroundMigrationBaseClass end end diff --git a/lib/gitlab/background_migration/backfill_approval_merge_request_rules_project_id.rb b/lib/gitlab/background_migration/backfill_approval_merge_request_rules_project_id.rb index de30d23f6983acc8ce25c3023850f6a4604c32c1..5c0f8b3307364a67d2bff5cd88af3d1c5f966964 100644 --- a/lib/gitlab/background_migration/backfill_approval_merge_request_rules_project_id.rb +++ b/lib/gitlab/background_migration/backfill_approval_merge_request_rules_project_id.rb @@ -2,11 +2,9 @@ module Gitlab module BackgroundMigration - # rubocop: disable Migration/BackgroundMigrationBaseClass -- BackfillDesiredShardingKeyJob inherits from BatchedMigrationJob. class BackfillApprovalMergeRequestRulesProjectId < BackfillDesiredShardingKeyJob operation_name :backfill_approval_merge_request_rules_project_id feature_category :code_review_workflow end - # rubocop: enable Migration/BackgroundMigrationBaseClass end end diff --git a/lib/gitlab/background_migration/backfill_approval_project_rules_users_project_id.rb b/lib/gitlab/background_migration/backfill_approval_project_rules_users_project_id.rb index e2fedec6b5d983c84a82d00071948868b0a22432..b5fbdaefee50ed419c56baa134353ab2f0818c99 100644 --- a/lib/gitlab/background_migration/backfill_approval_project_rules_users_project_id.rb +++ b/lib/gitlab/background_migration/backfill_approval_project_rules_users_project_id.rb @@ -2,11 +2,9 @@ module Gitlab module BackgroundMigration - # rubocop: disable Migration/BackgroundMigrationBaseClass -- BackfillDesiredShardingKeyJob inherits from BatchedMigrationJob. class BackfillApprovalProjectRulesUsersProjectId < BackfillDesiredShardingKeyJob operation_name :backfill_approval_project_rules_users_project_id feature_category :source_code_management end - # rubocop: enable Migration/BackgroundMigrationBaseClass end end diff --git a/lib/gitlab/background_migration/backfill_audit_events_streaming_event_type_filters_group_id.rb b/lib/gitlab/background_migration/backfill_audit_events_streaming_event_type_filters_group_id.rb index 20c18a8a8e26546e436ee08e9a4872761f000eb8..77b336ece2b09fc7862a12caec29329c233883af 100644 --- a/lib/gitlab/background_migration/backfill_audit_events_streaming_event_type_filters_group_id.rb +++ b/lib/gitlab/background_migration/backfill_audit_events_streaming_event_type_filters_group_id.rb @@ -2,11 +2,9 @@ module Gitlab module BackgroundMigration - # rubocop: disable Migration/BackgroundMigrationBaseClass -- BackfillDesiredShardingKeyJob inherits from BatchedMigrationJob. class BackfillAuditEventsStreamingEventTypeFiltersGroupId < BackfillDesiredShardingKeyJob operation_name :backfill_audit_events_streaming_event_type_filters_group_id feature_category :audit_events end - # rubocop: enable Migration/BackgroundMigrationBaseClass end end diff --git a/lib/gitlab/background_migration/backfill_audit_events_streaming_headers_group_id.rb b/lib/gitlab/background_migration/backfill_audit_events_streaming_headers_group_id.rb index 3d78afe874817d0c8315f6f53915b23a8d960b9f..f47cd9d226576ad940db067a0b56775cc2668937 100644 --- a/lib/gitlab/background_migration/backfill_audit_events_streaming_headers_group_id.rb +++ b/lib/gitlab/background_migration/backfill_audit_events_streaming_headers_group_id.rb @@ -2,11 +2,9 @@ module Gitlab module BackgroundMigration - # rubocop: disable Migration/BackgroundMigrationBaseClass -- BackfillDesiredShardingKeyJob inherits from BatchedMigrationJob. class BackfillAuditEventsStreamingHeadersGroupId < BackfillDesiredShardingKeyJob operation_name :backfill_audit_events_streaming_headers_group_id feature_category :audit_events end - # rubocop: enable Migration/BackgroundMigrationBaseClass end end diff --git a/lib/gitlab/background_migration/backfill_boards_epic_board_labels_group_id.rb b/lib/gitlab/background_migration/backfill_boards_epic_board_labels_group_id.rb index 3e462e31c62ad4239746dcbe6690fb022eabd05b..3dace12f9b51983ff9ff439c6a0ec24a30ee5233 100644 --- a/lib/gitlab/background_migration/backfill_boards_epic_board_labels_group_id.rb +++ b/lib/gitlab/background_migration/backfill_boards_epic_board_labels_group_id.rb @@ -2,11 +2,9 @@ module Gitlab module BackgroundMigration - # rubocop: disable Migration/BackgroundMigrationBaseClass -- BackfillDesiredShardingKeyJob inherits from BatchedMigrationJob. class BackfillBoardsEpicBoardLabelsGroupId < BackfillDesiredShardingKeyJob operation_name :backfill_boards_epic_board_labels_group_id feature_category :portfolio_management end - # rubocop: enable Migration/BackgroundMigrationBaseClass end end diff --git a/lib/gitlab/background_migration/backfill_boards_epic_board_positions_group_id.rb b/lib/gitlab/background_migration/backfill_boards_epic_board_positions_group_id.rb index 1046579654fde065838bf312867822a3abe14e41..6c5c93bb488bb772658bd64907938eded0cf432e 100644 --- a/lib/gitlab/background_migration/backfill_boards_epic_board_positions_group_id.rb +++ b/lib/gitlab/background_migration/backfill_boards_epic_board_positions_group_id.rb @@ -2,11 +2,9 @@ module Gitlab module BackgroundMigration - # rubocop: disable Migration/BackgroundMigrationBaseClass -- BackfillDesiredShardingKeyJob inherits from BatchedMigrationJob. class BackfillBoardsEpicBoardPositionsGroupId < BackfillDesiredShardingKeyJob operation_name :backfill_boards_epic_board_positions_group_id feature_category :portfolio_management end - # rubocop: enable Migration/BackgroundMigrationBaseClass end end diff --git a/lib/gitlab/background_migration/backfill_boards_epic_lists_group_id.rb b/lib/gitlab/background_migration/backfill_boards_epic_lists_group_id.rb index a632ff5ac406931d44c33e6867948f1256b7d081..071060a1cae74de041d40bde4281910fdd315384 100644 --- a/lib/gitlab/background_migration/backfill_boards_epic_lists_group_id.rb +++ b/lib/gitlab/background_migration/backfill_boards_epic_lists_group_id.rb @@ -2,11 +2,9 @@ module Gitlab module BackgroundMigration - # rubocop: disable Migration/BackgroundMigrationBaseClass -- BackfillDesiredShardingKeyJob inherits from BatchedMigrationJob. class BackfillBoardsEpicListsGroupId < BackfillDesiredShardingKeyJob operation_name :backfill_boards_epic_lists_group_id feature_category :portfolio_management end - # rubocop: enable Migration/BackgroundMigrationBaseClass end end diff --git a/lib/gitlab/background_migration/backfill_cluster_agent_tokens_project_id.rb b/lib/gitlab/background_migration/backfill_cluster_agent_tokens_project_id.rb index bf4c9082a50185697615234f367380c78b2293de..fe17e48d95f5c3cd4dee1a9d6da85a2e02d8490d 100644 --- a/lib/gitlab/background_migration/backfill_cluster_agent_tokens_project_id.rb +++ b/lib/gitlab/background_migration/backfill_cluster_agent_tokens_project_id.rb @@ -2,11 +2,9 @@ module Gitlab module BackgroundMigration - # rubocop: disable Migration/BackgroundMigrationBaseClass -- BackfillDesiredShardingKeyJob inherits from BatchedMigrationJob. class BackfillClusterAgentTokensProjectId < BackfillDesiredShardingKeyJob operation_name :backfill_cluster_agent_tokens_project_id feature_category :deployment_management end - # rubocop: enable Migration/BackgroundMigrationBaseClass end end diff --git a/lib/gitlab/background_migration/backfill_deployment_approvals_project_id.rb b/lib/gitlab/background_migration/backfill_deployment_approvals_project_id.rb index fac2d8bae46338f9b0a9c0c481945c2b5b6b8f56..e1a92fe0f96314d98d0c97d616852ac5bd9868fe 100644 --- a/lib/gitlab/background_migration/backfill_deployment_approvals_project_id.rb +++ b/lib/gitlab/background_migration/backfill_deployment_approvals_project_id.rb @@ -2,11 +2,9 @@ module Gitlab module BackgroundMigration - # rubocop: disable Migration/BackgroundMigrationBaseClass -- BackfillDesiredShardingKeyJob inherits from BatchedMigrationJob. class BackfillDeploymentApprovalsProjectId < BackfillDesiredShardingKeyJob operation_name :backfill_deployment_approvals_project_id feature_category :continuous_delivery end - # rubocop: enable Migration/BackgroundMigrationBaseClass end end diff --git a/lib/gitlab/background_migration/backfill_design_management_designs_namespace_id.rb b/lib/gitlab/background_migration/backfill_design_management_designs_namespace_id.rb index eae37e5e160df219aad5373638ca9a9da458ead1..5afc4d4ba900f6049f6f6b04c3c48ff4cc03ccf2 100644 --- a/lib/gitlab/background_migration/backfill_design_management_designs_namespace_id.rb +++ b/lib/gitlab/background_migration/backfill_design_management_designs_namespace_id.rb @@ -2,11 +2,9 @@ module Gitlab module BackgroundMigration - # rubocop: disable Migration/BackgroundMigrationBaseClass -- BackfillDesiredShardingKeyJob inherits from BatchedMigrationJob. class BackfillDesignManagementDesignsNamespaceId < BackfillDesiredShardingKeyJob operation_name :backfill_design_management_designs_namespace_id feature_category :design_management end - # rubocop: enable Migration/BackgroundMigrationBaseClass end end diff --git a/lib/gitlab/background_migration/backfill_design_management_repositories_namespace_id.rb b/lib/gitlab/background_migration/backfill_design_management_repositories_namespace_id.rb index 7a1435b55b0cd4ef673fe8d5f4b9c225c3cc29d9..342b684fa71e526f8adce941524933728833d1e7 100644 --- a/lib/gitlab/background_migration/backfill_design_management_repositories_namespace_id.rb +++ b/lib/gitlab/background_migration/backfill_design_management_repositories_namespace_id.rb @@ -2,11 +2,9 @@ module Gitlab module BackgroundMigration - # rubocop: disable Migration/BackgroundMigrationBaseClass -- BackfillDesiredShardingKeyJob inherits from BatchedMigrationJob. class BackfillDesignManagementRepositoriesNamespaceId < BackfillDesiredShardingKeyJob operation_name :backfill_design_management_repositories_namespace_id feature_category :design_management end - # rubocop: enable Migration/BackgroundMigrationBaseClass end end diff --git a/lib/gitlab/background_migration/backfill_wiki_repository_states_project_id.rb b/lib/gitlab/background_migration/backfill_wiki_repository_states_project_id.rb index ef4d2ae5fbfbd900a9848ed88d26f66d6ee6cd31..e4c8507f6d1b5ed49c7af28c541bad845ee5ba5f 100644 --- a/lib/gitlab/background_migration/backfill_wiki_repository_states_project_id.rb +++ b/lib/gitlab/background_migration/backfill_wiki_repository_states_project_id.rb @@ -2,11 +2,9 @@ module Gitlab module BackgroundMigration - # rubocop: disable Migration/BackgroundMigrationBaseClass -- BackfillDesiredShardingKeyJob inherits from BatchedMigrationJob. class BackfillWikiRepositoryStatesProjectId < BackfillDesiredShardingKeyJob operation_name :backfill_wiki_repository_states_project_id feature_category :geo_replication end - # rubocop: enable Migration/BackgroundMigrationBaseClass end end diff --git a/lib/gitlab/background_migration/backfill_work_item_parent_links_namespace_id.rb b/lib/gitlab/background_migration/backfill_work_item_parent_links_namespace_id.rb index e1c6bdaebbaa9920bb5b1cec045a875f6c0b59c5..332e12d17cf2b73d293248b08c40d0de0bf2905a 100644 --- a/lib/gitlab/background_migration/backfill_work_item_parent_links_namespace_id.rb +++ b/lib/gitlab/background_migration/backfill_work_item_parent_links_namespace_id.rb @@ -2,11 +2,9 @@ module Gitlab module BackgroundMigration - # rubocop: disable Migration/BackgroundMigrationBaseClass -- BackfillDesiredShardingKeyJob inherits from BatchedMigrationJob. class BackfillWorkItemParentLinksNamespaceId < BackfillDesiredShardingKeyJob operation_name :backfill_work_item_parent_links_namespace_id feature_category :team_planning end - # rubocop: enable Migration/BackgroundMigrationBaseClass end end diff --git a/lib/gitlab/background_migration/backfill_workspace_variables_project_id.rb b/lib/gitlab/background_migration/backfill_workspace_variables_project_id.rb index 63e2cbf1d330c375a7d75fceb0bea9290425b717..6848fe28e7a2dc05e6930d6d4c1f401ed21daf64 100644 --- a/lib/gitlab/background_migration/backfill_workspace_variables_project_id.rb +++ b/lib/gitlab/background_migration/backfill_workspace_variables_project_id.rb @@ -2,11 +2,9 @@ module Gitlab module BackgroundMigration - # rubocop: disable Migration/BackgroundMigrationBaseClass -- BackfillDesiredShardingKeyJob inherits from BatchedMigrationJob. class BackfillWorkspaceVariablesProjectId < BackfillDesiredShardingKeyJob operation_name :backfill_workspace_variables_project_id feature_category :remote_development end - # rubocop: enable Migration/BackgroundMigrationBaseClass end end diff --git a/rubocop/cop/migration/background_migration_base_class.rb b/rubocop/cop/migration/background_migration_base_class.rb index 9ec8403a607f66872b68b634f4a889f4bcd6d8e6..1a3a40cb66bdbe2cb63888cbb9186fda9d06a4a3 100644 --- a/rubocop/cop/migration/background_migration_base_class.rb +++ b/rubocop/cop/migration/background_migration_base_class.rb @@ -4,7 +4,14 @@ module RuboCop module Cop module Migration class BackgroundMigrationBaseClass < RuboCop::Cop::Base - MSG = 'Batched background migration jobs should inherit from Gitlab::BackgroundMigration::BatchedMigrationJob' + MSG = 'Batched background migration jobs should inherit from either ' \ + 'Gitlab::BackgroundMigration::BatchedMigrationJob ' \ + 'or one of its subclasses.' + + BASE_CLASSES = %i[ + BatchedMigrationJob + BackfillDesiredShardingKeyJob + ].to_set.freeze def_node_search :top_level_module?, <<~PATTERN (module (const nil? :Gitlab) (module (const nil? :BackgroundMigration) ...)) @@ -15,7 +22,7 @@ class BackgroundMigrationBaseClass < RuboCop::Cop::Base PATTERN def_node_search :inherits_batched_migration_job?, <<~PATTERN - (class _ (const #matching_parent_namespace? :BatchedMigrationJob) ...) + (class _ (const #matching_parent_namespace? %BASE_CLASSES) ...) PATTERN def on_module(module_node) diff --git a/spec/rubocop/cop/migration/background_migration_base_class_spec.rb b/spec/rubocop/cop/migration/background_migration_base_class_spec.rb index 8cc85ac692ca7c36a88a55f9fd0cea365cf8110c..fdd5f8d963ea0d2d053c346a7aaaf62617589817 100644 --- a/spec/rubocop/cop/migration/background_migration_base_class_spec.rb +++ b/spec/rubocop/cop/migration/background_migration_base_class_spec.rb @@ -3,75 +3,80 @@ require 'rubocop_spec_helper' require_relative '../../../../rubocop/cop/migration/background_migration_base_class' -RSpec.describe RuboCop::Cop::Migration::BackgroundMigrationBaseClass do - context 'when the migration class inherits from BatchedMigrationJob' do - it 'does not register any offenses' do - expect_no_offenses(<<~RUBY) - module Gitlab - module BackgroundMigration - class MyJob < BatchedMigrationJob - def perform - connection.execute("select 1") +RSpec.describe RuboCop::Cop::Migration::BackgroundMigrationBaseClass, feature_category: :database do + shared_examples_for 'inheritance from the right base class does not register an offense' do |base_class| + context "when the migration class inherits from #{base_class}" do + it 'does not register any offenses' do + expect_no_offenses(<<~RUBY) + module Gitlab + module BackgroundMigration + class MyJob < #{base_class} + def perform + connection.execute("select 1") + end end end end - end - RUBY + RUBY + end end - end - context 'when the migration class inherits from the namespaced BatchedMigrationJob' do - it 'does not register any offenses' do - expect_no_offenses(<<~RUBY) - module Gitlab - module BackgroundMigration - class MyJob < Gitlab::BackgroundMigration::BatchedMigrationJob - def perform - connection.execute("select 1") + context "when the migration class inherits from the namespaced #{base_class}" do + it 'does not register any offenses' do + expect_no_offenses(<<~RUBY) + module Gitlab + module BackgroundMigration + class MyJob < Gitlab::BackgroundMigration::#{base_class} + def perform + connection.execute("select 1") + end end end end - end - RUBY + RUBY + end end - end - context 'when the migration class inherits from the top-level namespaced BatchedMigrationJob' do - it 'does not register any offenses' do - expect_no_offenses(<<~RUBY) - module Gitlab - module BackgroundMigration - class MyJob < ::Gitlab::BackgroundMigration::BatchedMigrationJob - def perform - connection.execute("select 1") + context "when the migration class inherits from the top-level namespaced #{base_class}" do + it 'does not register any offenses' do + expect_no_offenses(<<~RUBY) + module Gitlab + module BackgroundMigration + class MyJob < ::Gitlab::BackgroundMigration::#{base_class} + def perform + connection.execute("select 1") + end end end end - end - RUBY + RUBY + end end - end - context 'when a nested class is used inside the job class' do - it 'does not register any offenses' do - expect_no_offenses(<<~RUBY) - module Gitlab - module BackgroundMigration - class MyJob < BatchedMigrationJob - class Project < ApplicationRecord - self.table_name = 'projects' - end + context 'when a nested class is used inside the job class' do + it 'does not register any offenses' do + expect_no_offenses(<<~RUBY) + module Gitlab + module BackgroundMigration + class MyJob < #{base_class} + class Project < ApplicationRecord + self.table_name = 'projects' + end - def perform - Project.update!(name: 'hi') + def perform + Project.update!(name: 'hi') + end end end end - end - RUBY + RUBY + end end end + it_behaves_like 'inheritance from the right base class does not register an offense', 'BatchedMigrationJob' + it_behaves_like 'inheritance from the right base class does not register an offense', 'BackfillDesiredShardingKeyJob' + context 'when the migration class inherits from another class' do it 'registers an offense' do expect_offense(<<~RUBY)