-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathadd_index_concurrently.rb
184 lines (167 loc) · 4.99 KB
/
add_index_concurrently.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
# frozen_string_literal: true
module RuboCop
module Cop
module Migration
# Use `algorithm: :concurrently` on adding indexes to existing tables in PostgreSQL.
#
# To avoid blocking writes.
#
# @safety
# Only meaningful in PostgreSQL.
#
# @example
# # bad
# class AddIndexToUsersName < ActiveRecord::Migration[7.0]
# def change
# add_index :users, :name
# end
# end
#
# # good
# class AddIndexToUsersNameConcurrently < ActiveRecord::Migration[7.0]
# disable_ddl_transaction!
#
# def change
# add_index :users, :name, algorithm: :concurrently
# end
# end
class AddIndexConcurrently < RuboCop::Cop::Base
extend AutoCorrector
include ::RuboCop::Migration::CopConcerns::DisableDdlTransaction
include RangeHelp
MSG = 'Use `algorithm: :concurrently` on adding indexes to existing tables in PostgreSQL.'
MESSAGE_FOR_DUPLICATED_DISABLE_DDL_TRANSACTION = 'Remove duplicated `disable_ddl_transaction!`.'
RESTRICT_ON_SEND = %i[
add_index
index
].freeze
# @param node [RuboCop::AST::SendNode]
# @return [void]
def on_send(node)
if add_index_without_concurrency?(node)
add_offense(node) do |corrector|
autocorrect(corrector, node)
end
end
duplicated_disable_ddl_transactions_from(node).each do |disable_ddl_transactions_node|
add_offense(node, message: MESSAGE_FOR_DUPLICATED_DISABLE_DDL_TRANSACTION) do |corrector|
corrector.remove(
range_with_surrounding_space(
disable_ddl_transactions_node.source_range,
side: :left
)
)
end
end
end
private
# @!method add_index?(node)
# @param node [RuboCop::AST::SendNode]
# @return [Boolean]
def_node_matcher :add_index?, <<~PATTERN
(send
nil?
:add_index
_
_
...
)
PATTERN
# @!method add_index_concurrently?(node)
# @param node [RuboCop::AST::SendNode]
# @return [Boolean]
def_node_matcher :add_index_concurrently?, <<~PATTERN
(send
nil?
:add_index
_
_
(hash
<
(pair
(sym :algorithm)
(sym :concurrently)
)
...
>
)
)
PATTERN
# @!method index?(node)
# @param node [RuboCop::AST::SendNode]
# @return [Boolean]
def_node_matcher :index?, <<~PATTERN
(send
lvar
:index
_
...
)
PATTERN
# @!method index_concurrently?(node)
# @param node [RuboCop::AST::SendNode]
# @return [Boolean]
def_node_matcher :index_concurrently?, <<~PATTERN
(send
lvar
:index
_
(hash
<
(pair
(sym :algorithm)
(sym :concurrently)
)
...
>
)
)
PATTERN
# @param node [RuboCop::AST::SendNode]
# @return [Boolean]
def add_index_without_concurrency?(node)
case node.method_name
when :add_index
add_index?(node) && !add_index_concurrently?(node)
when :index
index?(node) && in_change_table?(node) && !index_concurrently?(node)
end
end
# @param corrector [RuboCop::Cop::Corrector]
# @param node [RuboCop::AST::SendNode]
# @return [void]
def autocorrect(
corrector,
node
)
insert_disable_ddl_transaction(corrector, node) unless within_disable_ddl_transaction?(node)
insert_algorithm_option(corrector, node)
end
# @param node [RuboCop::AST::SendNode]
# @return [Array<RuboCop::AST::SendNode>]
def duplicated_disable_ddl_transactions_from(node)
disable_ddl_transactions_from(node)[1..] || []
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::SendNode]
# @return [void]
def insert_algorithm_option(
corrector,
node
)
target_node = node.last_argument
target_node = target_node.pairs.last if target_node.hash_type?
corrector.insert_after(
target_node,
', algorithm: :concurrently'
)
end
end
end
end
end