-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
change_column_null.rb
208 lines (189 loc) · 6.23 KB
/
change_column_null.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
# frozen_string_literal: true
module RuboCop
module Cop
module Migration
# Avoid simply setting `NOT NULL` constraint on an existing column in PostgreSQL.
#
# It blocks reads and writes while every row is checked.
# In PostgreSQL 12+, you can safely set `NOT NULL` constraint if corresponding check constraint exists.
#
# @safety
# Only meaningful in PostgreSQL 12+.
#
# @example
# # bad
# class SetNotNullColumnConstraintToUsersName < ActiveRecord::Migration[7.0]
# def change
# change_column_null :users, :name, false
# end
# end
#
# # good
# class SetNotNullCheckConstraintToUsersName < ActiveRecord::Migration[7.0]
# def change
# add_check_constraint :users, 'name IS NOT NULL', name: 'users_name_is_not_null', validate: false
# end
# end
#
# class ReplaceNotNullConstraintOnUsersName < ActiveRecord::Migration[7.0]
# def change
# validate_constraint :users, name: 'users_name_is_not_null'
# change_column_null :users, :name, false
# remove_check_constraint :users, name: 'users_name_is_not_null'
# end
# end
class ChangeColumnNull < RuboCop::Cop::Base
extend AutoCorrector
MSG = 'Avoid simply setting `NOT NULL` constraint on an existing column in PostgreSQL.'
RESTRICT_ON_SEND = %i[
change_column_null
change_null
].freeze
# @param node [RuboCop::AST::SendNode]
# @return [void]
def on_send(node)
return if called_with_validate_constraint?(node)
add_offense(node) do |corrector|
autocorrect(corrector, node)
end
end
private
# @!method validate_constraint?(node)
# @param node [RuboCop::AST::SendNode]
# @return [Boolean]
def_node_matcher :validate_constraint?, <<~PATTERN
(send nil? :validate_constraint ...)
PATTERN
# @param corrector [RuboCop::Cop::Corrector]
# @param node [RuboCop::AST::SendNode]
# @return [void]
def autocorrect(
corrector,
node
)
case node.method_name
when :change_column_null
autocorrect_change_column_null(corrector, node)
when :change_null
autocorrect_change_null(corrector, node)
end
end
# @param corrector [RuboCop::Cop::Corrector]
# @param node [RuboCop::AST::SendNode]
# @return [void]
def autocorrect_change_column_null(
corrector,
node
)
corrector.replace(
node,
format_add_check_constraint(
column_name: find_column_name_from_change_column_null(node),
table_name: find_table_name_from_change_column_null(node)
)
)
end
# @param corrector [RuboCop::Cop::Corrector]
# @param node [RuboCop::AST::SendNode]
# @return [void]
def autocorrect_change_null(
corrector,
node
)
corrector.replace(
node.location.selector.with(
end_pos: node.source_range.end_pos
),
format_check_constraint(
column_name: find_column_name_from_change_null(node),
table_name: find_table_name_from_change_null(node)
)
)
end
# @param node [RuboCop::AST::SendNode]
# @return [Boolean]
def called_with_validate_constraint?(node)
case node.method_name
when :change_column_null
node
when :change_null
find_ancestor_change_table(node)
end.left_siblings.any? do |sibling|
validate_constraint?(sibling)
end
end
# @param node [RuboCop::AST::SendNode]
# @return [RuboCop::AST::BlockNode]
def find_ancestor_change_table(node)
node.each_ancestor(:block).find do |ancestor|
ancestor.method?(:change_table)
end
end
# @param node [RuboCop::AST::SendNode]
# @return [String]
def find_column_name_from_change_column_null(node)
node.arguments[1].value.to_s
end
# @param node [RuboCop::AST::SendNode]
# @return [String]
def find_column_name_from_change_null(node)
node.first_argument.value.to_s
end
# @parm node [RuboCop::AST::SendNode]
# @return [String]
def find_table_name_from_change_column_null(node)
node.first_argument.value.to_s
end
# @param node [RuboCop::AST::SendNode]
# @return [String]
def find_table_name_from_change_null(node)
find_ancestor_change_table(node).send_node.first_argument.value.to_s
end
# @param column_name [String]
# @param table_name [String]
# @return [String]
def format_add_check_constraint(
column_name:,
table_name:
)
format(
'add_check_constraint :%<table_name>s, %<arguments>s',
arguments: format_check_constraint_arguments(
column_name: column_name,
table_name: table_name
),
table_name: table_name
)
end
# @param column_name [String]
# @param table_name [String]
# @return [String]
def format_check_constraint(
column_name:,
table_name:
)
format(
'check_constraint %<arguments>s',
arguments: format_check_constraint_arguments(
column_name: column_name,
table_name: table_name
)
)
end
# @param coumn_name [String]
# @param table_name [String]
# @return [String]
def format_check_constraint_arguments(
column_name:,
table_name:
)
format(
"'%<column_name>s IS NOT NULL', name: '%<constraint_name>s', validate: false",
column_name: column_name,
constraint_name: "#{table_name}_#{column_name}_is_not_null"
)
end
end
end
end
end