debian-mirror-gitlab/spec/rubocop/cop/migration/prevent_index_creation_spec.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

111 lines
3.7 KiB
Ruby
Raw Normal View History

2021-09-30 23:02:18 +05:30
# frozen_string_literal: true
2022-10-11 01:57:18 +05:30
require 'rubocop_spec_helper'
2021-09-30 23:02:18 +05:30
require_relative '../../../../rubocop/cop/migration/prevent_index_creation'
RSpec.describe RuboCop::Cop::Migration::PreventIndexCreation do
2021-11-11 11:23:49 +05:30
let(:forbidden_tables) { %w(ci_builds) }
let(:forbidden_tables_list) { forbidden_tables.join(', ') }
2021-09-30 23:02:18 +05:30
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
2021-11-11 11:23:49 +05:30
context 'when table_name is a symbol' do
it "registers an offense when add_index is used", :aggregate_failures do
forbidden_tables.each do |table_name|
expect_offense(<<~RUBY)
def change
add_index :#{table_name}, :protected
^^^^^^^^^ Adding new index to #{forbidden_tables_list} is forbidden, see https://gitlab.com/gitlab-org/gitlab/-/issues/332886
end
RUBY
2021-09-30 23:02:18 +05:30
end
2021-11-11 11:23:49 +05:30
end
it "registers an offense when add_concurrent_index is used", :aggregate_failures do
forbidden_tables.each do |table_name|
expect_offense(<<~RUBY)
def change
add_concurrent_index :#{table_name}, :protected
^^^^^^^^^^^^^^^^^^^^ Adding new index to #{forbidden_tables_list} is forbidden, see https://gitlab.com/gitlab-org/gitlab/-/issues/332886
end
RUBY
end
end
2021-09-30 23:02:18 +05:30
end
2021-11-11 11:23:49 +05:30
context 'when table_name is a string' do
it "registers an offense when add_index is used", :aggregate_failures do
forbidden_tables.each do |table_name|
expect_offense(<<~RUBY)
def change
add_index "#{table_name}", :protected
^^^^^^^^^ Adding new index to #{forbidden_tables_list} is forbidden, see https://gitlab.com/gitlab-org/gitlab/-/issues/332886
end
RUBY
2021-09-30 23:02:18 +05:30
end
2021-11-11 11:23:49 +05:30
end
it "registers an offense when add_concurrent_index is used", :aggregate_failures do
forbidden_tables.each do |table_name|
expect_offense(<<~RUBY)
def change
add_concurrent_index "#{table_name}", :protected
^^^^^^^^^^^^^^^^^^^^ Adding new index to #{forbidden_tables_list} is forbidden, see https://gitlab.com/gitlab-org/gitlab/-/issues/332886
end
RUBY
end
end
end
context 'when table_name is a constant' do
it "registers an offense when add_concurrent_index is used", :aggregate_failures do
expect_offense(<<~RUBY)
INDEX_NAME = "index_name"
TABLE_NAME = :ci_builds
disable_ddl_transaction!
def change
add_concurrent_index TABLE_NAME, :protected
^^^^^^^^^^^^^^^^^^^^ Adding new index to #{forbidden_tables_list} is forbidden, see https://gitlab.com/gitlab-org/gitlab/-/issues/332886
end
RUBY
end
2021-09-30 23:02:18 +05:30
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
2021-11-11 11:23:49 +05:30
context 'when using a constant' do
it 'does not register an offense' do
expect_no_offenses(<<~RUBY)
disable_ddl_transaction!
TABLE_NAME = "not_forbidden"
def up
add_concurrent_index TABLE_NAME, :protected
end
RUBY
end
end
2021-09-30 23:02:18 +05:30
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