Commit fba372a3 authored by Maxime Orefice's avatar Maxime Orefice

Add cop to prevent index creation on forbidden table

This commit adds a new custom cop allowing us to prevent
index creation on a given table. The first table forbidden
is ci_builds.
parent 4c8d720f
......@@ -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'
......
# 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
# 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
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment