diff --git a/.rubocop.yml b/.rubocop.yml index a26e9ab986b21fff614911603fee5507d8f9cd50..981b71e582ff3c59918a5ee2930206e23286f846 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -607,6 +607,12 @@ Migration/CreateTableWithForeignKeys: Exclude: - !ruby/regexp /\Adb\/(?:post_)?migrate\/(?:201[0-9]\d+|20200[0-8][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9])_.+\.rb\z/ +Migration/PreventIndexCreation: + Exclude: + - !ruby/regexp /\Adb\/(post_)?migrate\/201.*\.rb\z/ + - !ruby/regexp /\Adb\/(post_)?migrate\/2020.*\.rb\z/ + - !ruby/regexp /\Adb\/(post_)?migrate\/20210[1-6].*\.rb\z/ + Gitlab/RailsLogger: Exclude: - 'spec/**/*.rb' diff --git a/rubocop/cop/migration/prevent_index_creation.rb b/rubocop/cop/migration/prevent_index_creation.rb new file mode 100644 index 0000000000000000000000000000000000000000..c90f911d24e55e3987cf8a160ed1f571320ff390 --- /dev/null +++ b/rubocop/cop/migration/prevent_index_creation.rb @@ -0,0 +1,41 @@ +# frozen_string_literal: true +require_relative '../../migration_helpers' + +module RuboCop + module Cop + module Migration + # Cop that checks if new indexes are introduced to forbidden tables. + class PreventIndexCreation < RuboCop::Cop::Cop + include MigrationHelpers + + FORBIDDEN_TABLES = %i[ci_builds].freeze + + MSG = "Adding new index to #{FORBIDDEN_TABLES.join(", ")} is forbidden, see https://gitlab.com/gitlab-org/gitlab/-/issues/332886" + + def_node_matcher :add_index?, <<~PATTERN + (send nil? :add_index (sym #forbidden_tables?) ...) + PATTERN + + def_node_matcher :add_concurrent_index?, <<~PATTERN + (send nil? :add_concurrent_index (sym #forbidden_tables?) ...) + PATTERN + + def forbidden_tables?(node) + FORBIDDEN_TABLES.include?(node) + end + + def on_def(node) + return unless in_migration?(node) + + node.each_descendant(:send) do |send_node| + add_offense(send_node, location: :selector) if offense?(send_node) + end + end + + def offense?(node) + add_index?(node) || add_concurrent_index?(node) + end + end + end + end +end diff --git a/spec/rubocop/cop/migration/prevent_index_creation_spec.rb b/spec/rubocop/cop/migration/prevent_index_creation_spec.rb new file mode 100644 index 0000000000000000000000000000000000000000..a3965f54bbd5ffd57e0db4e76007aa08670a8b89 --- /dev/null +++ b/spec/rubocop/cop/migration/prevent_index_creation_spec.rb @@ -0,0 +1,50 @@ +# frozen_string_literal: true + +require 'fast_spec_helper' +require_relative '../../../../rubocop/cop/migration/prevent_index_creation' + +RSpec.describe RuboCop::Cop::Migration::PreventIndexCreation do + subject(:cop) { described_class.new } + + context 'when in migration' do + before do + allow(cop).to receive(:in_migration?).and_return(true) + end + + context 'when adding an index to a forbidden table' do + it 'registers an offense when add_index is used' do + expect_offense(<<~RUBY) + def change + add_index :ci_builds, :protected + ^^^^^^^^^ Adding new index to ci_builds is forbidden, see https://gitlab.com/gitlab-org/gitlab/-/issues/332886 + end + RUBY + end + + it 'registers an offense when add_concurrent_index is used' do + expect_offense(<<~RUBY) + def change + add_concurrent_index :ci_builds, :protected + ^^^^^^^^^^^^^^^^^^^^ Adding new index to ci_builds is forbidden, see https://gitlab.com/gitlab-org/gitlab/-/issues/332886 + end + RUBY + end + end + + context 'when adding an index to a regular table' do + it 'does not register an offense' do + expect_no_offenses(<<~RUBY) + def change + add_index :ci_pipelines, :locked + end + RUBY + end + end + end + + context 'when outside of migration' do + it 'does not register an offense' do + expect_no_offenses('def change; add_index :table, :column; end') + end + end +end