-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathadd_column_with_default_value.rb
229 lines (209 loc) · 6.45 KB
/
add_column_with_default_value.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
# frozen_string_literal: true
module RuboCop
module Cop
module Migration
# Add the column without a default value then change the default.
#
# In earlier versions of Postgres, MySQL, and MariaDB,
# adding a column with a default value to an existing table causes the entire table to be rewritten.
# During this time, reads and writes are blocked in Postgres, and writes are blocked in MySQL and MariaDB.
#
# @safety
# Only meaningful in earlier versions of Postgres, MySQL, and MariaDB.
#
# @example
# # bad
# class AddSomeColumnToUsers < ActiveRecord::Migration[7.0]
# def change
# add_column :users, :some_column, :string, default: 'some value'
# end
# end
#
# # good
# class AddSomeColumnToUsers < ActiveRecord::Migration[7.0]
# def change
# add_column :users, :some_column, :string
# change_column_default :users, :some_column, 'some value'
# end
# end
class AddColumnWithDefaultValue < RuboCop::Cop::Base
extend AutoCorrector
include RangeHelp
include ::RuboCop::Migration::CopConcerns::ColumnTypeMethod
MSG = 'Add the column without a default value then change the default.'
RESTRICT_ON_SEND = [
:add_column,
*COLUMN_TYPE_METHOD_NAMES
].freeze
# @param node [RuboCop::AST::SendNode]
# @return [void]
def on_send(node)
return unless target_method?(node)
default_option_node = non_nil_default_option_node_from(node)
return unless default_option_node
add_offense(default_option_node) do |corrector|
autocorrect(
corrector,
default_option_node: default_option_node,
send_node: node
)
end
end
private
# @!method add_column?(node)
# @param node [RuboCop::AST::SendNode]
# @return [Boolean]
def_node_matcher :add_column?, <<~PATTERN
(send
nil?
:add_column
...
)
PATTERN
# @!method column_type_method?(node)
# @param node [RuboCop::AST::SendNode]
# @return [Boolean]
def_node_matcher :column_type_method?, <<~PATTERN
(send
lvar
COLUMN_TYPE_METHOD_NAMES
...
)
PATTERN
# @!method non_nil_default_option_node_from(node)
# @param node [RuboCop::AST::SendNode]
# @return [RuboCop::AST::PairNode, nil]
def_node_matcher :non_nil_default_option_node_from, <<~PATTERN
(send
_
_
...
(hash
<
$(pair
(sym :default)
!nil
)
>
...
)
)
PATTERN
# @param corrector [RuboCop::Cop::Corrector]
# @param default_option_node [RuboCop::AST::PairNode]
# @param send_node [RuboCop::AST::SendNode]
# @return [void]
def autocorrect(
corrector,
default_option_node:,
send_node:
)
remove_pair(
corrector,
default_option_node
)
insert_change_column_default(
corrector,
default_option_node: default_option_node,
send_node: send_node
)
end
# @param node [RuboCop::AST::SendNode]
# @return [RuboCop::AST::SymNode]
def find_column_node_from(node)
case node.method_name
when :add_column
node.arguments[1]
else
node.first_argument
end
end
# @param node [RuboCop::AST::SendNode]
# @return [RuboCop::AST::SendNode]
def find_insertion_target_node_from(node)
case node.method_name
when :add_column
node
else
node.each_ancestor(:block).first
end
end
# @param node [RuboCop::AST::SendNode]
# @return [RuboCop::AST::SymNode]
def find_table_node_from(node)
case node.method_name
when :add_column
node.first_argument
else
node.each_ancestor(:block).first.send_node.first_argument
end
end
# @param node [RuboCop::AST::SendNode]
# @return [Boolean]
def in_change_table?(node)
node.each_ancestor(:block).first&.method?(:change_table)
end
# @param corrector [RuboCop::Cop::Corrector]
# @param node [RuboCop::AST::Node]
# @param string [String]
def insert_after_with_same_indentation(
corrector,
node,
string
)
corrector.insert_after(
node,
format(
"\n%<indentation>s%<string>s",
indentation: ' ' * node.location.column,
string: string
)
)
end
# @param corrector [RuboCop::Cop::Corrector]
# @param default_option_node [RuboCop::AST::PairNode]
# @param send_node [RuboCop::AST::SendNode]
# @return [void]
def insert_change_column_default(
corrector,
default_option_node:,
send_node:
)
insert_after_with_same_indentation(
corrector,
find_insertion_target_node_from(send_node),
format(
'change_column_default %<table>s, %<column>s, %<default>s',
column: find_column_node_from(send_node).source,
default: default_option_node.value.source,
table: find_table_node_from(send_node).source
)
)
end
# @param corrector [RuboCop::Cop::Corrector]
# @param node [RuboCop::AST::Node]
# @return [void]
def remove_pair(
corrector,
node
)
corrector.remove(
range_with_surrounding_comma(
range_with_surrounding_space(
node.source_range,
side: :left
),
:left
)
)
end
# @param node [RuboCop::AST::SendNode]
# @return [Boolean]
def target_method?(node)
add_column?(node) ||
(column_type_method?(node) && in_change_table?(node))
end
end
end
end
end