forked from rubocop/rubocop-rails
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbulk_change_table.rb
293 lines (255 loc) · 8.37 KB
/
bulk_change_table.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
# frozen_string_literal: true
module RuboCop
module Cop
module Rails
# This Cop checks whether alter queries are combinable.
# If combinable queries are detected, it suggests to you
# to use `change_table` with `bulk: true` instead.
# This option causes the migration to generate a single
# ALTER TABLE statement combining multiple column alterations.
#
# The `bulk` option is only supported on the MySQL and
# the PostgreSQL (5.2 later) adapter; thus it will
# automatically detect an adapter from `development` environment
# in `config/database.yml` when the `Database` option is not set.
# If the adapter is not `mysql2` or `postgresql`,
# this Cop ignores offenses.
#
# @example
# # bad
# def change
# add_column :users, :name, :string, null: false
# add_column :users, :nickname, :string
#
# # ALTER TABLE `users` ADD `name` varchar(255) NOT NULL
# # ALTER TABLE `users` ADD `nickname` varchar(255)
# end
#
# # good
# def change
# change_table :users, bulk: true do |t|
# t.string :name, null: false
# t.string :nickname
# end
#
# # ALTER TABLE `users` ADD `name` varchar(255) NOT NULL,
# # ADD `nickname` varchar(255)
# end
#
# @example
# # bad
# def change
# change_table :users do |t|
# t.string :name, null: false
# t.string :nickname
# end
# end
#
# # good
# def change
# change_table :users, bulk: true do |t|
# t.string :name, null: false
# t.string :nickname
# end
# end
#
# # good
# # When you don't want to combine alter queries.
# def change
# change_table :users, bulk: false do |t|
# t.string :name, null: false
# t.string :nickname
# end
# end
#
# @see https://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/SchemaStatements.html#method-i-change_table
# @see https://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/Table.html
class BulkChangeTable < Base
MSG_FOR_CHANGE_TABLE = <<~MSG.chomp
You can combine alter queries using `bulk: true` options.
MSG
MSG_FOR_ALTER_METHODS = <<~MSG.chomp
You can use `change_table :%<table>s, bulk: true` to combine alter queries.
MSG
MYSQL = 'mysql'
POSTGRESQL = 'postgresql'
MIGRATION_METHODS = %i[change up down].freeze
COMBINABLE_TRANSFORMATIONS = %i[
primary_key
column
string
text
integer
bigint
float
decimal
numeric
datetime
timestamp
time
date
binary
boolean
json
virtual
remove
change
timestamps
remove_timestamps
].freeze
COMBINABLE_ALTER_METHODS = %i[
add_column
remove_column
remove_columns
change_column
add_timestamps
remove_timestamps
].freeze
MYSQL_COMBINABLE_TRANSFORMATIONS = %i[
rename
index
remove_index
].freeze
MYSQL_COMBINABLE_ALTER_METHODS = %i[
rename_column
add_index
remove_index
].freeze
POSTGRESQL_COMBINABLE_TRANSFORMATIONS = %i[
change_default
].freeze
POSTGRESQL_COMBINABLE_ALTER_METHODS = %i[
change_column_default
].freeze
def on_def(node)
return unless support_bulk_alter?
return unless MIGRATION_METHODS.include?(node.method_name)
return unless node.body
recorder = AlterMethodsRecorder.new
node.body.child_nodes.each do |child_node|
if call_to_combinable_alter_method? child_node
recorder.process(child_node)
else
recorder.flush
end
end
recorder.offensive_nodes.each { |n| add_offense_for_alter_methods(n) }
end
def on_send(node)
return unless support_bulk_alter?
return unless node.command?(:change_table)
return if include_bulk_options?(node)
return unless node.block_node
send_nodes = node.block_node.body.each_child_node(:send).to_a
transformations = send_nodes.select do |send_node|
combinable_transformations.include?(send_node.method_name)
end
add_offense_for_change_table(node) if transformations.size > 1
end
private
# @param node [RuboCop::AST::SendNode] (send nil? :change_table ...)
def include_bulk_options?(node)
# arguments: [{(sym :table)(str "table")} (hash (pair (sym :bulk) _))]
options = node.arguments[1]
return false unless options
options.hash_type? &&
options.keys.any? { |key| key.sym_type? && key.value == :bulk }
end
def database
cop_config['Database'] || database_from_yaml
end
def database_from_yaml
return nil unless database_yaml
case database_yaml['adapter']
when 'mysql2'
MYSQL
when 'postgresql'
POSTGRESQL
end
end
def database_yaml
return nil unless File.exist?('config/database.yml')
yaml = YAML.load_file('config/database.yml')
return nil unless yaml.is_a? Hash
config = yaml['development']
return nil unless config.is_a?(Hash)
config
rescue Psych::SyntaxError
nil
end
def support_bulk_alter?
case database
when MYSQL
true
when POSTGRESQL
# Add bulk alter support for PostgreSQL in 5.2.0
# @see https://github.com/rails/rails/pull/31331
target_rails_version >= 5.2
else
false
end
end
def call_to_combinable_alter_method?(child_node)
child_node.send_type? &&
combinable_alter_methods.include?(child_node.method_name)
end
def combinable_alter_methods
case database
when MYSQL
COMBINABLE_ALTER_METHODS + MYSQL_COMBINABLE_ALTER_METHODS
when POSTGRESQL
COMBINABLE_ALTER_METHODS + POSTGRESQL_COMBINABLE_ALTER_METHODS
end
end
def combinable_transformations
case database
when MYSQL
COMBINABLE_TRANSFORMATIONS + MYSQL_COMBINABLE_TRANSFORMATIONS
when POSTGRESQL
COMBINABLE_TRANSFORMATIONS + POSTGRESQL_COMBINABLE_TRANSFORMATIONS
end
end
# @param node [RuboCop::AST::SendNode]
def add_offense_for_alter_methods(node)
# arguments: [{(sym :table)(str "table")} ...]
table_node = node.arguments[0]
return unless table_node.is_a? RuboCop::AST::BasicLiteralNode
message = format(MSG_FOR_ALTER_METHODS, table: table_node.value)
add_offense(node, message: message)
end
# @param node [RuboCop::AST::SendNode]
def add_offense_for_change_table(node)
add_offense(node, message: MSG_FOR_CHANGE_TABLE)
end
# Record combinable alter methods and register offensive nodes.
class AlterMethodsRecorder
def initialize
@nodes = []
@offensive_nodes = []
end
# @param new_node [RuboCop::AST::SendNode]
def process(new_node)
# arguments: [{(sym :table)(str "table")} ...]
table_node = new_node.arguments[0]
if table_node.is_a? RuboCop::AST::BasicLiteralNode
flush unless @nodes.all? do |node|
node.arguments[0].value.to_s == table_node.value.to_s
end
@nodes << new_node
else
flush
end
end
def flush
@offensive_nodes << @nodes.first if @nodes.size > 1
@nodes = []
end
def offensive_nodes
flush
@offensive_nodes
end
end
end
end
end
end