-
-
Notifications
You must be signed in to change notification settings - Fork 266
/
reversible_migration.rb
326 lines (292 loc) · 8.92 KB
/
reversible_migration.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
# frozen_string_literal: true
module RuboCop
module Cop
module Rails
# Checks whether the change method of the migration file is
# reversible.
#
# @example
# # bad
# def change
# change_table :users do |t|
# t.remove :name
# end
# end
#
# # good
# def change
# change_table :users do |t|
# t.remove :name, type: :string
# end
# end
#
# # good
# def change
# create_table :users do |t|
# t.string :name
# end
# end
#
# @example
# # drop_table
#
# # bad
# def change
# drop_table :users
# end
#
# # good
# def change
# drop_table :users do |t|
# t.string :name
# end
# end
#
# @example
# # change_column_default
#
# # bad
# def change
# change_column_default(:suppliers, :qualification, 'new')
# end
#
# # good
# def change
# change_column_default(:posts, :state, from: nil, to: "draft")
# end
#
# @example
# # remove_column
#
# # bad
# def change
# remove_column(:suppliers, :qualification)
# end
#
# # good
# def change
# remove_column(:suppliers, :qualification, :string)
# end
#
# @example
# # remove_foreign_key
#
# # bad
# def change
# remove_foreign_key :accounts, column: :owner_id
# end
#
# # good
# def change
# remove_foreign_key :accounts, :branches
# end
#
# # good
# def change
# remove_foreign_key :accounts, to_table: :branches
# end
#
# @example
# # change_table
#
# # bad
# def change
# change_table :users do |t|
# t.remove :name
# t.change_default :authorized, 1
# t.change :price, :string
# end
# end
#
# # good
# def change
# change_table :users do |t|
# t.string :name
# end
# end
#
# @example
# # remove_columns
#
# # bad
# def change
# remove_columns :users, :name, :email
# end
#
# # good
# def change
# reversible do |dir|
# dir.up do
# remove_columns :users, :name, :email
# end
#
# dir.down do
# add_column :users, :name, :string
# add_column :users, :email, :string
# end
# end
# end
#
# # good (Rails >= 6.1, see https://github.com/rails/rails/pull/36589)
# def change
# remove_columns :users, :name, :email, type: :string
# end
#
# @example
# # remove_index
#
# # bad
# def change
# remove_index :users, name: :index_users_on_email
# end
#
# # good
# def change
# remove_index :users, :email
# end
#
# # good
# def change
# remove_index :users, column: :email
# end
class ReversibleMigration < Base
prepend MigrationsHelper
MSG = '%<action>s is not reversible.'
def_node_matcher :irreversible_schema_statement_call, <<~PATTERN
(send nil? ${:change_column :execute} ...)
PATTERN
def_node_matcher :drop_table_call, <<~PATTERN
(send nil? :drop_table ...)
PATTERN
def_node_matcher :remove_column_call, <<~PATTERN
(send nil? :remove_column $...)
PATTERN
def_node_matcher :remove_foreign_key_call, <<~PATTERN
(send nil? :remove_foreign_key _ $_)
PATTERN
def_node_matcher :change_table_call, <<~PATTERN
(send nil? :change_table $_ ...)
PATTERN
def_node_matcher :remove_columns_call, <<~PATTERN
(send nil? :remove_columns ... $_)
PATTERN
def_node_matcher :remove_index_call, <<~PATTERN
(send nil? :remove_index _ $_)
PATTERN
def on_send(node)
return unless in_migration?(node) && within_change_method?(node)
return if within_reversible_or_up_only_block?(node)
check_irreversible_schema_statement_node(node)
check_drop_table_node(node)
check_reversible_hash_node(node)
check_remove_column_node(node)
check_remove_foreign_key_node(node)
check_remove_columns_node(node)
check_remove_index_node(node)
end
def on_block(node)
return unless in_migration?(node) && within_change_method?(node)
return if within_reversible_or_up_only_block?(node)
return if node.body.nil?
check_change_table_node(node.send_node, node.body)
end
alias on_numblock on_block
private
def check_irreversible_schema_statement_node(node)
irreversible_schema_statement_call(node) do |method_name|
add_offense(node, message: format(MSG, action: method_name))
end
end
def check_drop_table_node(node)
drop_table_call(node) do
unless node.parent.block_type? || node.last_argument.block_pass_type?
add_offense(node, message: format(MSG, action: 'drop_table(without block)'))
end
end
end
def check_reversible_hash_node(node)
return if reversible_change_table_call?(node)
add_offense(node, message: format(MSG, action: "#{node.method_name}(without :from and :to)"))
end
def check_remove_column_node(node)
remove_column_call(node) do |args|
add_offense(node, message: format(MSG, action: 'remove_column(without type)')) if args.to_a.size < 3
end
end
def check_remove_foreign_key_node(node)
remove_foreign_key_call(node) do |arg|
if arg.hash_type? && !all_hash_key?(arg, :to_table)
add_offense(node, message: format(MSG, action: 'remove_foreign_key(without table)'))
end
end
end
def check_change_table_node(node, block)
change_table_call(node) do |arg|
if block.send_type?
check_change_table_offense(arg, block)
else
block.each_child_node(:send) do |child_node|
check_change_table_offense(arg, child_node)
end
end
end
end
def check_remove_columns_node(node)
remove_columns_call(node) do |args|
unless all_hash_key?(args, :type) && target_rails_version >= 6.1
action = target_rails_version >= 6.1 ? 'remove_columns(without type)' : 'remove_columns'
add_offense(node, message: format(MSG, action: action))
end
end
end
def check_remove_index_node(node)
remove_index_call(node) do |args|
if args.hash_type? && !all_hash_key?(args, :column)
add_offense(node, message: format(MSG, action: 'remove_index(without column)'))
end
end
end
def check_change_table_offense(receiver, node)
method_name = node.method_name
return if receiver != node.receiver && reversible_change_table_call?(node)
action = if method_name == :remove
target_rails_version >= 6.1 ? 't.remove (without type)' : 't.remove'
else
"change_table(with #{method_name})"
end
add_offense(node, message: format(MSG, action: action))
end
def reversible_change_table_call?(node)
case node.method_name
when :change
false
when :remove
target_rails_version >= 6.1 && all_hash_key?(node.last_argument, :type)
when :change_default, :change_column_default, :change_table_comment,
:change_column_comment
all_hash_key?(node.last_argument, :from, :to)
else
true
end
end
def within_change_method?(node)
node.each_ancestor(:def).any? do |ancestor|
ancestor.method?(:change)
end
end
def within_reversible_or_up_only_block?(node)
node.each_ancestor(:block).any? do |ancestor|
(ancestor.block_type? && ancestor.method?(:reversible)) || ancestor.method?(:up_only)
end
end
def all_hash_key?(args, *keys)
return false unless args&.hash_type?
hash_keys = args.keys.map do |key|
key.children.first.to_sym
end
(hash_keys & keys).sort == keys
end
end
end
end
end