rubocop-migration 0.9.0 → 0.9.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/lib/rubocop/cop/migration/add_index_duplicate.rb +6 -10
- data/lib/rubocop/cop/migration/change_column_null.rb +22 -12
- data/lib/rubocop/cop/migration/remove_column.rb +8 -8
- data/lib/rubocop/migration/cop_concerns/literal_name.rb +18 -0
- data/lib/rubocop/migration/cop_concerns.rb +1 -0
- data/lib/rubocop/migration/version.rb +1 -1
- metadata +2 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz: '
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: '03485734726566b4a1b71b0c077912fa5a79b2abc921397fa7240f322839158e'
|
|
4
|
+
data.tar.gz: 4482090f99d1efc637bc8d9de8f084c2dffee443297d8516491de8794d75c03e
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 44fa6b05896197e565e6ea884a5b4e07ef507b9612d4783a8f7cdce612a024426580e73a51416f27d3474140b20d6da86533e7c9f1e627ae39567f24e7c6b34d
|
|
7
|
+
data.tar.gz: 7d15afd2bc3e2da78279ab35bfb3cbeaea89497eb829ef87e4e98535655f8ae75ba60eae4b5bd544d29cb5227e65d2870da50bec843aa9feb4c1a69fb94d289b
|
data/Gemfile.lock
CHANGED
|
@@ -25,6 +25,8 @@ module RuboCop
|
|
|
25
25
|
# end
|
|
26
26
|
# end
|
|
27
27
|
class AddIndexDuplicate < RuboCop::Cop::Base
|
|
28
|
+
include ::RuboCop::Migration::CopConcerns::LiteralName
|
|
29
|
+
|
|
28
30
|
MSG = 'Avoid adding duplicate indexes.'
|
|
29
31
|
|
|
30
32
|
RESTRICT_ON_SEND = %i[
|
|
@@ -107,12 +109,9 @@ module RuboCop
|
|
|
107
109
|
# @return [Array<String>, nil]
|
|
108
110
|
def indexed_column_names_from(node)
|
|
109
111
|
indexed_columns_node = indexed_columns_node_from(node)
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
when :str, :sym
|
|
114
|
-
[indexed_columns_node.value.to_s]
|
|
115
|
-
end
|
|
112
|
+
column_nodes = indexed_columns_node&.array_type? ? indexed_columns_node.children : [indexed_columns_node]
|
|
113
|
+
column_names = column_nodes.map { |column_node| literal_name_from(column_node) }
|
|
114
|
+
column_names unless column_names.include?(nil)
|
|
116
115
|
end
|
|
117
116
|
|
|
118
117
|
# @param node [RuboCop::AST::SendNode]
|
|
@@ -149,10 +148,7 @@ module RuboCop
|
|
|
149
148
|
# @param node [RuboCop::AST::SendNode]
|
|
150
149
|
# @return [String, nil]
|
|
151
150
|
def table_name_from(node)
|
|
152
|
-
|
|
153
|
-
return unless table_name_value_node.respond_to?(:value)
|
|
154
|
-
|
|
155
|
-
table_name_value_node.value.to_s
|
|
151
|
+
literal_name_from(table_name_value_node_from(node))
|
|
156
152
|
end
|
|
157
153
|
|
|
158
154
|
# @param node [RuboCop::AST::SendNode]
|
|
@@ -50,6 +50,8 @@ module RuboCop
|
|
|
50
50
|
class ChangeColumnNull < RuboCop::Cop::Base
|
|
51
51
|
extend AutoCorrector
|
|
52
52
|
|
|
53
|
+
include ::RuboCop::Migration::CopConcerns::LiteralName
|
|
54
|
+
|
|
53
55
|
MSG = 'Avoid simply setting `NOT NULL` constraint on an existing column in PostgreSQL.'
|
|
54
56
|
|
|
55
57
|
MSG_FOR_ALLOW_NULL_IN_CHANGE = 'Allow NULL in `up` instead of `change`, ' \
|
|
@@ -117,11 +119,15 @@ module RuboCop
|
|
|
117
119
|
corrector,
|
|
118
120
|
node
|
|
119
121
|
)
|
|
122
|
+
column_name = find_column_name_from_change_column_null(node)
|
|
123
|
+
table_name = find_table_name_from_change_column_null(node)
|
|
124
|
+
return unless column_name && table_name
|
|
125
|
+
|
|
120
126
|
corrector.replace(
|
|
121
127
|
node,
|
|
122
128
|
format_add_check_constraint(
|
|
123
|
-
column_name:
|
|
124
|
-
table_name:
|
|
129
|
+
column_name: column_name,
|
|
130
|
+
table_name: table_name
|
|
125
131
|
)
|
|
126
132
|
)
|
|
127
133
|
end
|
|
@@ -133,13 +139,17 @@ module RuboCop
|
|
|
133
139
|
corrector,
|
|
134
140
|
node
|
|
135
141
|
)
|
|
142
|
+
column_name = find_column_name_from_change_null(node)
|
|
143
|
+
table_name = find_table_name_from_change_null(node)
|
|
144
|
+
return unless column_name && table_name
|
|
145
|
+
|
|
136
146
|
corrector.replace(
|
|
137
147
|
node.location.selector.with(
|
|
138
148
|
end_pos: node.source_range.end_pos
|
|
139
149
|
),
|
|
140
150
|
format_check_constraint(
|
|
141
|
-
column_name:
|
|
142
|
-
table_name:
|
|
151
|
+
column_name: column_name,
|
|
152
|
+
table_name: table_name
|
|
143
153
|
)
|
|
144
154
|
)
|
|
145
155
|
end
|
|
@@ -166,27 +176,27 @@ module RuboCop
|
|
|
166
176
|
end
|
|
167
177
|
|
|
168
178
|
# @param node [RuboCop::AST::SendNode]
|
|
169
|
-
# @return [String]
|
|
179
|
+
# @return [String, nil]
|
|
170
180
|
def find_column_name_from_change_column_null(node)
|
|
171
|
-
node.arguments[1]
|
|
181
|
+
literal_name_from(node.arguments[1])
|
|
172
182
|
end
|
|
173
183
|
|
|
174
184
|
# @param node [RuboCop::AST::SendNode]
|
|
175
|
-
# @return [String]
|
|
185
|
+
# @return [String, nil]
|
|
176
186
|
def find_column_name_from_change_null(node)
|
|
177
|
-
node.first_argument
|
|
187
|
+
literal_name_from(node.first_argument)
|
|
178
188
|
end
|
|
179
189
|
|
|
180
190
|
# @param node [RuboCop::AST::SendNode]
|
|
181
|
-
# @return [String]
|
|
191
|
+
# @return [String, nil]
|
|
182
192
|
def find_table_name_from_change_column_null(node)
|
|
183
|
-
node.first_argument
|
|
193
|
+
literal_name_from(node.first_argument)
|
|
184
194
|
end
|
|
185
195
|
|
|
186
196
|
# @param node [RuboCop::AST::SendNode]
|
|
187
|
-
# @return [String]
|
|
197
|
+
# @return [String, nil]
|
|
188
198
|
def find_table_name_from_change_null(node)
|
|
189
|
-
find_ancestor_change_table(node).send_node.first_argument
|
|
199
|
+
literal_name_from(find_ancestor_change_table(node).send_node.first_argument)
|
|
190
200
|
end
|
|
191
201
|
|
|
192
202
|
# @param column_name [String]
|
|
@@ -38,6 +38,8 @@ module RuboCop
|
|
|
38
38
|
# end
|
|
39
39
|
# end
|
|
40
40
|
class RemoveColumn < RuboCop::Cop::Base
|
|
41
|
+
include ::RuboCop::Migration::CopConcerns::LiteralName
|
|
42
|
+
|
|
41
43
|
MSG = 'Make sure the column is already ignored by the running app before removing it.'
|
|
42
44
|
|
|
43
45
|
RESTRICT_ON_SEND = %i[
|
|
@@ -114,7 +116,7 @@ module RuboCop
|
|
|
114
116
|
# @param node [RuboCop::AST::SendNode]
|
|
115
117
|
# @return [String, nil]
|
|
116
118
|
def find_column_name_from(node)
|
|
117
|
-
node.arguments[1]
|
|
119
|
+
literal_name_from(node.arguments[1])
|
|
118
120
|
end
|
|
119
121
|
|
|
120
122
|
# @param table_name [String]
|
|
@@ -140,20 +142,18 @@ module RuboCop
|
|
|
140
142
|
# @param node [RuboCop::AST::SendNode]
|
|
141
143
|
# @return [String, nil]
|
|
142
144
|
def find_table_name_from(node)
|
|
143
|
-
|
|
144
|
-
table_name_node.value&.to_s if table_name_node.respond_to?(:value)
|
|
145
|
+
literal_name_from(node.first_argument)
|
|
145
146
|
end
|
|
146
147
|
|
|
147
|
-
# @note This method returns `true` if the table name cannot be determined.
|
|
148
|
+
# @note This method returns `true` if the table name or the column name cannot be determined.
|
|
148
149
|
# @param node [RuboCop::AST::SendNode]
|
|
149
150
|
# @return [Boolean]
|
|
150
151
|
def ignored?(node)
|
|
151
152
|
table_name = find_table_name_from(node)
|
|
152
|
-
|
|
153
|
+
column_name = find_column_name_from(node)
|
|
154
|
+
return true unless table_name && column_name
|
|
153
155
|
|
|
154
|
-
find_ignored_column_names_from(table_name).include?(
|
|
155
|
-
find_column_name_from(node)
|
|
156
|
-
)
|
|
156
|
+
find_ignored_column_names_from(table_name).include?(column_name)
|
|
157
157
|
end
|
|
158
158
|
|
|
159
159
|
# @note Tries `accounts_user.rb` first, then namespaces such as `accounts/user.rb`.
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module RuboCop
|
|
4
|
+
module Migration
|
|
5
|
+
module CopConcerns
|
|
6
|
+
module LiteralName
|
|
7
|
+
private
|
|
8
|
+
|
|
9
|
+
# Table and column names are only known statically when written as a symbol or string literal.
|
|
10
|
+
# @param node [RuboCop::AST::Node, nil]
|
|
11
|
+
# @return [String, nil] `nil` when the name is given by anything else, such as a variable.
|
|
12
|
+
def literal_name_from(node)
|
|
13
|
+
node.value.to_s if node&.type?(:str, :sym)
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
@@ -6,6 +6,7 @@ module RuboCop
|
|
|
6
6
|
autoload :BatchProcessing, 'rubocop/migration/cop_concerns/batch_processing'
|
|
7
7
|
autoload :ColumnTypeMethod, 'rubocop/migration/cop_concerns/column_type_method'
|
|
8
8
|
autoload :DisableDdlTransaction, 'rubocop/migration/cop_concerns/disable_ddl_transaction'
|
|
9
|
+
autoload :LiteralName, 'rubocop/migration/cop_concerns/literal_name'
|
|
9
10
|
autoload :NewTable, 'rubocop/migration/cop_concerns/new_table'
|
|
10
11
|
end
|
|
11
12
|
end
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: rubocop-migration
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.9.
|
|
4
|
+
version: 0.9.1
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Ryo Nakamura
|
|
@@ -104,6 +104,7 @@ files:
|
|
|
104
104
|
- lib/rubocop/migration/cop_concerns/batch_processing.rb
|
|
105
105
|
- lib/rubocop/migration/cop_concerns/column_type_method.rb
|
|
106
106
|
- lib/rubocop/migration/cop_concerns/disable_ddl_transaction.rb
|
|
107
|
+
- lib/rubocop/migration/cop_concerns/literal_name.rb
|
|
107
108
|
- lib/rubocop/migration/cop_concerns/new_table.rb
|
|
108
109
|
- lib/rubocop/migration/plugin.rb
|
|
109
110
|
- lib/rubocop/migration/version.rb
|