-
-
Notifications
You must be signed in to change notification settings - Fork 248
/
reversible_migration_spec.rb
256 lines (211 loc) · 7.05 KB
/
reversible_migration_spec.rb
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# frozen_string_literal: true
RSpec.describe RuboCop::Cop::Rails::ReversibleMigration, :config do
subject(:cop) { described_class.new(config) }
let(:source) do
<<~RUBY
class ExampleMigration < ActiveRecord::Migration
def change
#{code}
end
end
RUBY
end
shared_examples 'accepts' do |name, code|
let(:code) { code }
it "accepts usages of #{name}" do
expect_no_offenses(source)
end
end
shared_examples 'offense' do |name, code|
let(:code) { code }
it "registers an offense for #{name}" do
inspect_source(source)
expect(cop.messages).to eq(["#{name} is not reversible."])
end
end
it_behaves_like 'accepts', 'create_table', <<~RUBY
create_table :users do |t|
t.string :name
end
RUBY
it_behaves_like 'offense', 'execute', <<~RUBY
execute "ALTER TABLE `pages_linked_pages` ADD UNIQUE `page_id_linked_page_id` (`page_id`,`linked_page_id`)"
RUBY
it_behaves_like 'accepts', 'up_only', <<~RUBY
up_only { execute "UPDATE posts SET published = 'true'" }
RUBY
context 'within block' do
it_behaves_like 'accepts', 'create_table', <<~RUBY
[:users, :articles].each do |table|
create_table table do |t|
t.string :name
end
end
RUBY
it_behaves_like 'offense', 'execute', <<~RUBY
[:pages_linked_pages, :pages_unlinked_pages].each do |table|
execute "ALTER TABLE `table` ADD UNIQUE `page_id_linked_page_id` (`page_id`,`linked_page_id`)"
end
RUBY
end
context 'when using variable assignment' do
it_behaves_like 'accepts', 'create_table', <<~RUBY
def change
change_table :invoices do |t|
decimals_params = {precision: 10, scale: 2}
t.decimal :total_discount, decimals_params
end
end
RUBY
end
context 'when block argument is empty' do
it_behaves_like 'accepts', 'create_table', <<~RUBY
def change
change_table :invoices do |t|
end
end
RUBY
end
context 'within #reversible' do
it_behaves_like 'accepts', 'execute', <<~RUBY
reversible do |dir|
dir.up do
execute "ALTER TABLE `pages_linked_pages` ADD UNIQUE `page_id_linked_page_id` (`page_id`,`linked_page_id`)"
end
dir.down do
execute "ALTER TABLE `pages_linked_pages` DROP INDEX `page_id_linked_page_id`"
end
end
RUBY
end
context 'drop_table' do
it_behaves_like 'accepts', 'drop_table(with block)', <<~RUBY
drop_table :users do |t|
t.string :name
end
RUBY
it_behaves_like 'offense', 'drop_table(without block)', <<~RUBY
drop_table :users
RUBY
end
context 'change_column_default' do
it_behaves_like 'accepts',
'change_column_default(with :from and :to)', <<-RUBY
change_column_default(:posts, :state, from: nil, to: "draft")
RUBY
it_behaves_like 'accepts',
'change_column_default(with :to and :from)', <<-RUBY
change_column_default(:posts, :state, to: "draft", from: nil)
RUBY
it_behaves_like 'accepts',
'change_column_default(*column :from and :to)', <<-RUBY
columns = [:foo, :bar]
change_column_default(*columns, from: nil, to: "draft")
RUBY
it_behaves_like 'offense',
'change_column_default(without :from and :to)', <<-RUBY
change_column_default(:suppliers, :qualification, 'new')
RUBY
end
context 'change_table_comment' do
it_behaves_like 'accepts',
'change_table_comment(with :from and :to)', <<-RUBY
change_table_comment(:posts, from: nil, to: "draft")
RUBY
it_behaves_like 'offense',
'change_table_comment(without :from and :to)', <<-RUBY
change_table_comment(:suppliers, 'new')
RUBY
end
context 'change_column_comment' do
it_behaves_like 'accepts',
'change_column_comment(with :from and :to)', <<-RUBY
change_column_comment(:posts, :state, from: nil, to: "draft")
RUBY
it_behaves_like 'offense',
'change_column_comment(without :from and :to)', <<-RUBY
change_column_comment(:suppliers, :qualification, 'new')
RUBY
end
context 'remove_column' do
it_behaves_like 'accepts', 'remove_column(with type)', <<~RUBY
remove_column(:suppliers, :qualification, :string)
RUBY
it_behaves_like 'accepts', 'remove_column(with type and options)', <<~RUBY
remove_column(:suppliers, :qualification, :string, null: false)
RUBY
it_behaves_like 'offense', 'remove_column(without type)', <<~RUBY
remove_column(:suppliers, :qualification)
RUBY
end
context 'remove_foreign_key' do
it_behaves_like 'accepts', 'remove_foreign_key(with table)', <<~RUBY
remove_foreign_key :accounts, :branches
RUBY
it_behaves_like 'accepts', 'remove_foreign_key(with :to_table)', <<~RUBY
remove_foreign_key :accounts, to_table: :branches
RUBY
it_behaves_like 'offense', 'remove_foreign_key(without table)', <<~RUBY
remove_foreign_key :accounts, column: :owner_id
RUBY
end
context 'change_table' do
it_behaves_like 'accepts', 'change_table(with reversible calls)', <<~RUBY
change_table :users do |t|
t.column :name, :string
t.text :description
t.boolean :authorized
end
RUBY
it_behaves_like 'offense', 'change_table(with change)', <<~RUBY
change_table :users do |t|
t.change :description, :text
end
RUBY
it_behaves_like 'accepts',
'change_table(with reversible change_default)', <<-RUBY
change_table :users do |t|
t.change_default :authorized, from: nil, to: 1
end
RUBY
it_behaves_like 'offense', 'change_table(with change_default)', <<~RUBY
change_table :users do |t|
t.change_default :authorized, 1
end
RUBY
it_behaves_like 'offense', 'change_table(with remove)', <<~RUBY
change_table :users do |t|
t.remove :qualification
end
RUBY
end
context 'remove_columns' do
context 'Rails >= 6.1', :rails61 do
it_behaves_like 'accepts', 'remove_columns(with type)', <<~RUBY
remove_columns(:posts, :title, :body, type: :text)
RUBY
it_behaves_like 'offense', 'remove_columns(without type)', <<~RUBY
remove_columns(:posts, :title, :body)
RUBY
end
context 'Rails < 6.1', :rails60 do
it_behaves_like 'offense', 'remove_columns', <<~RUBY
remove_columns(:posts, :title, :body, type: :text)
RUBY
end
it_behaves_like 'offense', 'remove_columns', <<~RUBY
remove_columns(:posts, :title, :body)
RUBY
end
context 'remove_index' do
it_behaves_like 'accepts', 'remove_index (with column)', <<~RUBY
remove_index(:posts, column: :body)
RUBY
it_behaves_like 'accepts', 'remove_index (with two args)', <<~RUBY
remove_index(:posts, :body)
RUBY
it_behaves_like 'offense', 'remove_index(without column)', <<~RUBY
remove_index(:posts, name: :index_columns_on_body)
RUBY
end
end