1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
# frozen_string_literal: true
require 'rubocop_spec_helper'
require_relative '../../../../rubocop/cop/migration/prevent_index_creation'
RSpec.describe RuboCop::Cop::Migration::PreventIndexCreation do
let(:forbidden_tables) { %w[ci_builds namespaces projects users] }
let(:forbidden_tables_list) { forbidden_tables.join(', ') }
context 'when in migration' do
before do
allow(cop).to receive(:in_migration?).and_return(true)
end
let(:offense) { "Adding new index to #{forbidden_tables_list} is forbidden. [...]" }
context 'when adding an index to a forbidden table' do
it 'does not register an offense when direction is down' do
forbidden_tables.each do |table_name|
expect_no_offenses(<<~RUBY)
def down
add_concurrent_index :#{table_name}, :runners_token, unique: true, name: INDEX_NAME
end
RUBY
end
end
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
^^^^^^^^^ #{offense}
end
RUBY
end
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
^^^^^^^^^^^^^^^^^^^^ #{offense}
end
RUBY
end
end
end
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
^^^^^^^^^ #{offense}
end
RUBY
end
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
^^^^^^^^^^^^^^^^^^^^ #{offense}
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
^^^^^^^^^^^^^^^^^^^^ #{offense}
end
RUBY
end
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
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
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
|