rubocop-migration 0.8.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: '0813a8046835fbb1c8382152971eaada39e144aabb2266cda2115b2e5c2a1398'
4
- data.tar.gz: 38580189a800f01224c4dbfd39d7aed4707509d148ec1deed74e936858a99624
3
+ metadata.gz: '03485734726566b4a1b71b0c077912fa5a79b2abc921397fa7240f322839158e'
4
+ data.tar.gz: 4482090f99d1efc637bc8d9de8f084c2dffee443297d8516491de8794d75c03e
5
5
  SHA512:
6
- metadata.gz: 65e716d2e0cecb340f2e8171d125d901f7eb8e53352227fa472e4489da1c8b97c04000ae6bb59080f2a7d811e9cc2c1dad3248d9624a00f90fba3b2ce55c7e93
7
- data.tar.gz: 4bb8b248e3f0bc57767bc0be615a27d31d663f89e2d8eddd67e30102cbbfe6d49f1bd119db3b6a3fae5593f0751b5862e1af993e90e0f6514e788faadb8d6314
6
+ metadata.gz: 44fa6b05896197e565e6ea884a5b4e07ef507b9612d4783a8f7cdce612a024426580e73a51416f27d3474140b20d6da86533e7c9f1e627ae39567f24e7c6b34d
7
+ data.tar.gz: 7d15afd2bc3e2da78279ab35bfb3cbeaea89497eb829ef87e4e98535655f8ae75ba60eae4b5bd544d29cb5227e65d2870da50bec843aa9feb4c1a69fb94d289b
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- rubocop-migration (0.8.0)
4
+ rubocop-migration (0.9.1)
5
5
  activesupport
6
6
  lint_roller (>= 1.1)
7
7
  rubocop (>= 1.72.1)
@@ -31,9 +31,21 @@ module RuboCop
31
31
  # validate_check_constraint :orders, name: 'orders_price_positive'
32
32
  # end
33
33
  # end
34
+ #
35
+ # # good
36
+ # class CreateOrders < ActiveRecord::Migration[7.0]
37
+ # def change
38
+ # create_table :orders do |t|
39
+ # t.integer :price
40
+ # end
41
+ # add_check_constraint :orders, 'price > 0', name: 'orders_price_positive'
42
+ # end
43
+ # end
34
44
  class AddCheckConstraint < RuboCop::Cop::Base
35
45
  extend AutoCorrector
36
46
 
47
+ include ::RuboCop::Migration::CopConcerns::NewTable
48
+
37
49
  MSG = 'Activate a check constraint in a separate migration in PostgreSQL.'
38
50
 
39
51
  RESTRICT_ON_SEND = %i[
@@ -104,7 +116,8 @@ module RuboCop
104
116
  # @return [Boolean]
105
117
  def bad?(node)
106
118
  add_check_constraint?(node) &&
107
- !add_check_constraint_with_validate_false?(node)
119
+ !add_check_constraint_with_validate_false?(node) &&
120
+ !new_table?(node.first_argument)
108
121
  end
109
122
  end
110
123
  end
@@ -27,11 +27,22 @@ module RuboCop
27
27
  # change_column_default :users, :some_column, 'some value'
28
28
  # end
29
29
  # end
30
+ #
31
+ # # good
32
+ # class CreateUsers < ActiveRecord::Migration[7.0]
33
+ # def change
34
+ # create_table :users do |t|
35
+ # t.string :name
36
+ # end
37
+ # add_column :users, :some_column, :string, default: 'some value'
38
+ # end
39
+ # end
30
40
  class AddColumnWithDefaultValue < RuboCop::Cop::Base
31
41
  extend AutoCorrector
32
42
 
33
43
  include RangeHelp
34
44
  include ::RuboCop::Migration::CopConcerns::ColumnTypeMethod
45
+ include ::RuboCop::Migration::CopConcerns::NewTable
35
46
 
36
47
  MSG = 'Add the column without a default value then change the default.'
37
48
 
@@ -44,6 +55,7 @@ module RuboCop
44
55
  # @return [void]
45
56
  def on_send(node)
46
57
  return unless target_method?(node)
58
+ return if new_table?(find_table_node_from(node))
47
59
 
48
60
  default_option_node = non_nil_default_option_node_from(node)
49
61
  return unless default_option_node
@@ -100,7 +100,7 @@ module RuboCop
100
100
  case node.type
101
101
  when :array
102
102
  node.values.count
103
- when :sym
103
+ when :str, :sym
104
104
  1
105
105
  end
106
106
  end
@@ -26,10 +26,21 @@ module RuboCop
26
26
  # add_index :users, :name, algorithm: :concurrently
27
27
  # end
28
28
  # end
29
+ #
30
+ # # good
31
+ # class CreateUsers < ActiveRecord::Migration[7.0]
32
+ # def change
33
+ # create_table :users do |t|
34
+ # t.string :name
35
+ # end
36
+ # add_index :users, :name
37
+ # end
38
+ # end
29
39
  class AddIndexConcurrently < RuboCop::Cop::Base
30
40
  extend AutoCorrector
31
41
 
32
42
  include ::RuboCop::Migration::CopConcerns::DisableDdlTransaction
43
+ include ::RuboCop::Migration::CopConcerns::NewTable
33
44
  include RangeHelp
34
45
 
35
46
  MSG = 'Use `algorithm: :concurrently` on adding indexes to existing tables in PostgreSQL.'
@@ -44,7 +55,7 @@ module RuboCop
44
55
  # @param node [RuboCop::AST::SendNode]
45
56
  # @return [void]
46
57
  def on_send(node)
47
- if add_index_without_concurrency?(node)
58
+ if add_index_without_concurrency?(node) && !new_table?(find_table_node_from(node))
48
59
  add_offense(node) do |corrector|
49
60
  autocorrect(corrector, node)
50
61
  end
@@ -159,6 +170,17 @@ module RuboCop
159
170
  disable_ddl_transactions_from(node)[1..] || []
160
171
  end
161
172
 
173
+ # @param node [RuboCop::AST::SendNode]
174
+ # @return [RuboCop::AST::Node, nil]
175
+ def find_table_node_from(node)
176
+ case node.method_name
177
+ when :add_index
178
+ node.first_argument
179
+ else
180
+ node.each_ancestor(:block).first.send_node.first_argument
181
+ end
182
+ end
183
+
162
184
  # @param node [RuboCop::AST::SendNode]
163
185
  # @return [Boolean]
164
186
  def in_change_table?(node)
@@ -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
- case indexed_columns_node&.type
111
- when :array
112
- indexed_columns_node.children.map { |child| child.value.to_s }
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
- table_name_value_node = table_name_value_node_from(node)
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]
@@ -28,16 +28,35 @@ module RuboCop
28
28
  #
29
29
  # class ReplaceNotNullConstraintOnUsersName < ActiveRecord::Migration[7.0]
30
30
  # def change
31
- # validate_constraint :users, name: 'users_name_is_not_null'
31
+ # validate_check_constraint :users, name: 'users_name_is_not_null'
32
32
  # change_column_null :users, :name, false
33
33
  # remove_check_constraint :users, name: 'users_name_is_not_null'
34
34
  # end
35
35
  # end
36
+ #
37
+ # # bad
38
+ # class AllowNullOnUsersName < ActiveRecord::Migration[7.0]
39
+ # def change
40
+ # change_column_null :users, :name, true
41
+ # end
42
+ # end
43
+ #
44
+ # # good
45
+ # class AllowNullOnUsersName < ActiveRecord::Migration[7.0]
46
+ # def up
47
+ # change_column_null :users, :name, true
48
+ # end
49
+ # end
36
50
  class ChangeColumnNull < RuboCop::Cop::Base
37
51
  extend AutoCorrector
38
52
 
53
+ include ::RuboCop::Migration::CopConcerns::LiteralName
54
+
39
55
  MSG = 'Avoid simply setting `NOT NULL` constraint on an existing column in PostgreSQL.'
40
56
 
57
+ MSG_FOR_ALLOW_NULL_IN_CHANGE = 'Allow NULL in `up` instead of `change`, ' \
58
+ 'as rolling back `change` simply sets `NOT NULL` constraint again.'
59
+
41
60
  RESTRICT_ON_SEND = %i[
42
61
  change_column_null
43
62
  change_null
@@ -48,6 +67,11 @@ module RuboCop
48
67
  def on_send(node)
49
68
  return if called_with_validate_constraint?(node)
50
69
 
70
+ if allow_null?(node)
71
+ add_offense(node, message: MSG_FOR_ALLOW_NULL_IN_CHANGE) if in_change_method?(node)
72
+ return
73
+ end
74
+
51
75
  add_offense(node) do |corrector|
52
76
  autocorrect(corrector, node)
53
77
  end
@@ -56,11 +80,21 @@ module RuboCop
56
80
 
57
81
  private
58
82
 
83
+ # @!method allow_null?(node)
84
+ # @param node [RuboCop::AST::SendNode]
85
+ # @return [Boolean]
86
+ def_node_matcher :allow_null?, <<~PATTERN
87
+ {
88
+ (send nil? :change_column_null _ _ true ...)
89
+ (call _ :change_null _ true ...)
90
+ }
91
+ PATTERN
92
+
59
93
  # @!method validate_constraint?(node)
60
94
  # @param node [RuboCop::AST::SendNode]
61
95
  # @return [Boolean]
62
96
  def_node_matcher :validate_constraint?, <<~PATTERN
63
- (send nil? :validate_constraint ...)
97
+ (send nil? {:validate_constraint :validate_check_constraint} ...)
64
98
  PATTERN
65
99
 
66
100
  # @param corrector [RuboCop::Cop::Corrector]
@@ -85,11 +119,15 @@ module RuboCop
85
119
  corrector,
86
120
  node
87
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
+
88
126
  corrector.replace(
89
127
  node,
90
128
  format_add_check_constraint(
91
- column_name: find_column_name_from_change_column_null(node),
92
- table_name: find_table_name_from_change_column_null(node)
129
+ column_name: column_name,
130
+ table_name: table_name
93
131
  )
94
132
  )
95
133
  end
@@ -101,13 +139,17 @@ module RuboCop
101
139
  corrector,
102
140
  node
103
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
+
104
146
  corrector.replace(
105
147
  node.location.selector.with(
106
148
  end_pos: node.source_range.end_pos
107
149
  ),
108
150
  format_check_constraint(
109
- column_name: find_column_name_from_change_null(node),
110
- table_name: find_table_name_from_change_null(node)
151
+ column_name: column_name,
152
+ table_name: table_name
111
153
  )
112
154
  )
113
155
  end
@@ -134,27 +176,27 @@ module RuboCop
134
176
  end
135
177
 
136
178
  # @param node [RuboCop::AST::SendNode]
137
- # @return [String]
179
+ # @return [String, nil]
138
180
  def find_column_name_from_change_column_null(node)
139
- node.arguments[1].value.to_s
181
+ literal_name_from(node.arguments[1])
140
182
  end
141
183
 
142
184
  # @param node [RuboCop::AST::SendNode]
143
- # @return [String]
185
+ # @return [String, nil]
144
186
  def find_column_name_from_change_null(node)
145
- node.first_argument.value.to_s
187
+ literal_name_from(node.first_argument)
146
188
  end
147
189
 
148
190
  # @param node [RuboCop::AST::SendNode]
149
- # @return [String]
191
+ # @return [String, nil]
150
192
  def find_table_name_from_change_column_null(node)
151
- node.first_argument.value.to_s
193
+ literal_name_from(node.first_argument)
152
194
  end
153
195
 
154
196
  # @param node [RuboCop::AST::SendNode]
155
- # @return [String]
197
+ # @return [String, nil]
156
198
  def find_table_name_from_change_null(node)
157
- find_ancestor_change_table(node).send_node.first_argument.value.to_s
199
+ literal_name_from(find_ancestor_change_table(node).send_node.first_argument)
158
200
  end
159
201
 
160
202
  # @param column_name [String]
@@ -203,6 +245,12 @@ module RuboCop
203
245
  constraint_name: "#{table_name}_#{column_name}_is_not_null"
204
246
  )
205
247
  end
248
+
249
+ # @param node [RuboCop::AST::SendNode]
250
+ # @return [Boolean]
251
+ def in_change_method?(node)
252
+ node.each_ancestor(:def).first&.method?(:change) || false
253
+ end
206
254
  end
207
255
  end
208
256
  end
@@ -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,16 +116,17 @@ 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].value&.to_s
119
+ literal_name_from(node.arguments[1])
118
120
  end
119
121
 
120
122
  # @param table_name [String]
121
123
  # @return [Array<String>]
122
124
  def find_ignored_column_names_from(table_name)
123
125
  pathname = model_pathname_from(
124
- singularize(table_name)
126
+ directory: ::Pathname.new('app/models'),
127
+ snake_cased_model_name: singularize(table_name)
125
128
  )
126
- return [] unless pathname.exist?
129
+ return [] unless pathname
127
130
 
128
131
  ignored_column_nodes = ignored_column_nodes_from(
129
132
  parse(
@@ -139,26 +142,42 @@ module RuboCop
139
142
  # @param node [RuboCop::AST::SendNode]
140
143
  # @return [String, nil]
141
144
  def find_table_name_from(node)
142
- table_name_node = node.first_argument
143
- table_name_node.value&.to_s if table_name_node.respond_to?(:value)
145
+ literal_name_from(node.first_argument)
144
146
  end
145
147
 
146
- # @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.
147
149
  # @param node [RuboCop::AST::SendNode]
148
150
  # @return [Boolean]
149
151
  def ignored?(node)
150
152
  table_name = find_table_name_from(node)
151
- return true unless table_name
153
+ column_name = find_column_name_from(node)
154
+ return true unless table_name && column_name
152
155
 
153
- find_ignored_column_names_from(table_name).include?(
154
- find_column_name_from(node)
155
- )
156
+ find_ignored_column_names_from(table_name).include?(column_name)
156
157
  end
157
158
 
159
+ # @note Tries `accounts_user.rb` first, then namespaces such as `accounts/user.rb`.
160
+ # @param directory [Pathname]
158
161
  # @param snake_cased_model_name [String]
159
- # @return [Pathname]
160
- def model_pathname_from(snake_cased_model_name)
161
- ::Pathname.new("app/models/#{snake_cased_model_name}.rb")
162
+ # @return [Pathname, nil]
163
+ def model_pathname_from(
164
+ directory:,
165
+ snake_cased_model_name:
166
+ )
167
+ pathname = directory.join("#{snake_cased_model_name}.rb")
168
+ return pathname if pathname.exist?
169
+
170
+ snake_cased_model_name.scan(/(?<=[^_])_(?=[^_])/) do
171
+ namespace = ::Regexp.last_match.pre_match
172
+ next unless directory.join(namespace).directory?
173
+
174
+ pathname = model_pathname_from(
175
+ directory: directory.join(namespace),
176
+ snake_cased_model_name: ::Regexp.last_match.post_match
177
+ )
178
+ return pathname if pathname
179
+ end
180
+ nil
162
181
  end
163
182
 
164
183
  # @param content [String]
@@ -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
@@ -0,0 +1,80 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Migration
5
+ module CopConcerns
6
+ module NewTable
7
+ class << self
8
+ def included(klass)
9
+ super
10
+ klass.class_eval do
11
+ # @!method create_table_if_not_exists?(node)
12
+ # @param node [RuboCop::AST::SendNode]
13
+ # @return [Boolean]
14
+ def_node_matcher :create_table_if_not_exists?, <<~PATTERN
15
+ (send
16
+ nil?
17
+ :create_table
18
+ _
19
+ (hash
20
+ <
21
+ (pair
22
+ (sym :if_not_exists)
23
+ true
24
+ )
25
+ ...
26
+ >
27
+ )
28
+ )
29
+ PATTERN
30
+
31
+ # @!method create_table_name_from(node)
32
+ # @param node [RuboCop::AST::SendNode]
33
+ # @return [String, Symbol, nil]
34
+ def_node_matcher :create_table_name_from, <<~PATTERN
35
+ (send
36
+ nil?
37
+ :create_table
38
+ ({str sym} $_)
39
+ ...
40
+ )
41
+ PATTERN
42
+ end
43
+ end
44
+ end
45
+
46
+ private
47
+
48
+ # @param node [RuboCop::AST::Node]
49
+ # @param table_name [String]
50
+ # @return [Boolean]
51
+ def create_table_of?(
52
+ node,
53
+ table_name
54
+ )
55
+ node = node.send_node if node.block_type?
56
+ node.send_type? &&
57
+ create_table_name_from(node)&.to_s == table_name &&
58
+ !create_table_if_not_exists?(node)
59
+ end
60
+
61
+ # Whether the table is created by a `create_table` written directly in the method body before it,
62
+ # not under a condition, a block, or a rescue.
63
+ # @param table_node [RuboCop::AST::Node, nil]
64
+ # @return [Boolean]
65
+ def new_table?(table_node)
66
+ return false unless table_node&.type?(:str, :sym)
67
+
68
+ body_node = table_node.each_ancestor(:def, :defs).first&.body
69
+ return false unless body_node&.begin_type?
70
+
71
+ table_name = table_node.value.to_s
72
+ body_node.each_child_node.any? do |statement_node|
73
+ statement_node.source_range.end_pos < table_node.source_range.begin_pos &&
74
+ create_table_of?(statement_node, table_name)
75
+ end
76
+ end
77
+ end
78
+ end
79
+ end
80
+ end
@@ -6,6 +6,8 @@ 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'
10
+ autoload :NewTable, 'rubocop/migration/cop_concerns/new_table'
9
11
  end
10
12
  end
11
13
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module RuboCop
4
4
  module Migration
5
- VERSION = '0.8.0'
5
+ VERSION = '0.9.1'
6
6
  end
7
7
  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.8.0
4
+ version: 0.9.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryo Nakamura
@@ -104,6 +104,8 @@ 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
108
+ - lib/rubocop/migration/cop_concerns/new_table.rb
107
109
  - lib/rubocop/migration/plugin.rb
108
110
  - lib/rubocop/migration/version.rb
109
111
  - rubocop-migration.gemspec