rubocop-migration 0.4.0 → 0.4.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 819776e7d688a0d112d1ad7ecc2994125de7bfa7e4b8fe27105f21d2a8061565
4
- data.tar.gz: fa2c6bfce21e4dd4ad206e6fb049162ee25b016bc2be01c22782d3dc0871e255
3
+ metadata.gz: 18f0c1b4fa8be61ca159b11acf5593b9343c4696156171acb9ba5774c422dfab
4
+ data.tar.gz: 7fd0e0b48fc9d98d1ba4a307a1bb41fbd8068240710954c0bf69e3aedb0c5d9c
5
5
  SHA512:
6
- metadata.gz: b9fecf517eab16cd9f6c16e302d7a392c0b263a75c164b53a99337b017d4416894fde4eac40874ca53e4fa61c77783f4ef5cbde64007b5388dc6ceefd79d644d
7
- data.tar.gz: da06bac2b33c7018964708c0aec62808262e14478b09ff2af0303f6ce00034a57d2331a64ed9fd973ec34012525ec7051b362c71843cbf23f27e17d6f30d5248
6
+ metadata.gz: '090d29334bcef597b4b9a13f63b9b79494ef0190f458814ea01abeb8d0d48a374b58a60619f06b85737d258ebda0065647f91e96cb98601e0dd53a4847e494ea'
7
+ data.tar.gz: f7992c9b8468c881508f5aaa091187c75f432368ba0f2e7c4db014fe81f233524c1536b184c067efdab12933ad0a3e0278f5ff3425120d29dc713d8b1478e6ad
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- rubocop-migration (0.4.0)
4
+ rubocop-migration (0.4.2)
5
5
  activesupport
6
6
  rubocop (>= 1.34)
7
7
  rubocop-rails
@@ -9,22 +9,22 @@ PATH
9
9
  GEM
10
10
  remote: https://rubygems.org/
11
11
  specs:
12
- activesupport (7.0.4)
12
+ activesupport (7.0.6)
13
13
  concurrent-ruby (~> 1.0, >= 1.0.2)
14
14
  i18n (>= 1.6, < 2)
15
15
  minitest (>= 5.1)
16
16
  tzinfo (~> 2.0)
17
17
  ast (2.4.2)
18
- concurrent-ruby (1.1.10)
18
+ concurrent-ruby (1.2.2)
19
19
  diff-lcs (1.5.0)
20
- i18n (1.12.0)
20
+ i18n (1.14.1)
21
21
  concurrent-ruby (~> 1.0)
22
22
  json (2.6.2)
23
- minitest (5.16.3)
23
+ minitest (5.18.1)
24
24
  parallel (1.22.1)
25
25
  parser (3.1.2.1)
26
26
  ast (~> 2.4.1)
27
- rack (3.0.0)
27
+ rack (3.0.8)
28
28
  rainbow (3.1.1)
29
29
  rake (13.0.6)
30
30
  regexp_parser (2.6.0)
@@ -57,7 +57,7 @@ GEM
57
57
  rubocop-performance (1.15.0)
58
58
  rubocop (>= 1.7.0, < 2.0)
59
59
  rubocop-ast (>= 0.4.0)
60
- rubocop-rails (2.17.0)
60
+ rubocop-rails (2.20.2)
61
61
  activesupport (>= 4.2.0)
62
62
  rack (>= 1.1)
63
63
  rubocop (>= 1.33.0, < 2.0)
@@ -68,7 +68,7 @@ GEM
68
68
  ruby-progressbar (1.11.0)
69
69
  sevencop (0.21.0)
70
70
  rubocop
71
- tzinfo (2.0.5)
71
+ tzinfo (2.0.6)
72
72
  concurrent-ruby (~> 1.0)
73
73
  unicode-display_width (2.3.0)
74
74
 
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # rubocop-migration
2
2
 
3
- [![test](https://github.com/r7kamura/rubocop-migration/actions/workflows/test.yml/badge.svg)](https://github.com/r7kamura/rubocop-migration/actions/workflows/test.yml)
3
+ [![ci](https://github.com/r7kamura/rubocop-migration/actions/workflows/ci.yml/badge.svg)](https://github.com/r7kamura/rubocop-migration/actions/workflows/ci.yml)
4
4
 
5
5
  RuboCop extension focused on ActiveRecord migration.
6
6
 
@@ -21,7 +21,7 @@ require:
21
21
  - rubocop-migration
22
22
 
23
23
  Migration/AddCheckConstraint:
24
- Enabled: false
24
+ Enabled: true
25
25
  ```
26
26
 
27
27
  Note that all cops are `Enabled: false` by default.
@@ -30,9 +30,12 @@ module RuboCop
30
30
  extend AutoCorrector
31
31
 
32
32
  include ::RuboCop::Migration::CopConcerns::DisableDdlTransaction
33
+ include RangeHelp
33
34
 
34
35
  MSG = 'Use `algorithm: :concurrently` on adding indexes to existing tables in PostgreSQL.'
35
36
 
37
+ MESSAGE_FOR_DUPLICATED_DISABLE_DDL_TRANSACTION = 'Remove duplicated `disable_ddl_transaction!`.'
38
+
36
39
  RESTRICT_ON_SEND = %i[
37
40
  add_index
38
41
  index
@@ -41,10 +44,21 @@ module RuboCop
41
44
  # @param node [RuboCop::AST::SendNode]
42
45
  # @return [void]
43
46
  def on_send(node)
44
- return unless bad?(node)
47
+ if add_index_without_concurrency?(node)
48
+ add_offense(node) do |corrector|
49
+ autocorrect(corrector, node)
50
+ end
51
+ end
45
52
 
46
- add_offense(node) do |corrector|
47
- autocorrect(corrector, node)
53
+ duplicated_disable_ddl_transactions_from(node).each do |disable_ddl_transactions_node|
54
+ add_offense(node, message: MESSAGE_FOR_DUPLICATED_DISABLE_DDL_TRANSACTION) do |corrector|
55
+ corrector.remove(
56
+ range_with_surrounding_space(
57
+ disable_ddl_transactions_node.source_range,
58
+ side: :left
59
+ )
60
+ )
61
+ end
48
62
  end
49
63
  end
50
64
 
@@ -116,6 +130,17 @@ module RuboCop
116
130
  )
117
131
  PATTERN
118
132
 
133
+ # @param node [RuboCop::AST::SendNode]
134
+ # @return [Boolean]
135
+ def add_index_without_concurrency?(node)
136
+ case node.method_name
137
+ when :add_index
138
+ add_index?(node) && !add_index_concurrently?(node)
139
+ when :index
140
+ index?(node) && in_change_table?(node) && !index_concurrently?(node)
141
+ end
142
+ end
143
+
119
144
  # @param corrector [RuboCop::Cop::Corrector]
120
145
  # @param node [RuboCop::AST::SendNode]
121
146
  # @return [void]
@@ -128,14 +153,9 @@ module RuboCop
128
153
  end
129
154
 
130
155
  # @param node [RuboCop::AST::SendNode]
131
- # @return [Boolean]
132
- def bad?(node)
133
- case node.method_name
134
- when :add_index
135
- add_index?(node) && !add_index_concurrently?(node)
136
- when :index
137
- index?(node) && in_change_table?(node) && !index_concurrently?(node)
138
- end
156
+ # @return [Array<RuboCop::AST::SendNode>]
157
+ def duplicated_disable_ddl_transactions_from(node)
158
+ disable_ddl_transactions_from(node)[1..] || []
139
159
  end
140
160
 
141
161
  # @param node [RuboCop::AST::SendNode]
@@ -135,15 +135,18 @@ module RuboCop
135
135
  # @param node [RuboCop::AST::SendNode]
136
136
  # @return [String, nil]
137
137
  def find_table_name_from(node)
138
- node.arguments[0].value&.to_s
138
+ table_name_node = node.arguments[0]
139
+ table_name_node.value&.to_s if table_name_node.respond_to?(:value)
139
140
  end
140
141
 
142
+ # @note This method returns `true` if the table name cannot be determined.
141
143
  # @param node [RuboCop::AST::SendNode]
142
144
  # @return [Boolean]
143
145
  def ignored?(node)
144
- find_ignored_column_names_from(
145
- find_table_name_from(node)
146
- ).include?(
146
+ table_name = find_table_name_from(node)
147
+ return true unless table_name
148
+
149
+ find_ignored_column_names_from(table_name).include?(
147
150
  find_column_name_from(node)
148
151
  )
149
152
  end
@@ -24,6 +24,15 @@ module RuboCop
24
24
 
25
25
  private
26
26
 
27
+ # @param node [RuboCop::AST::SendNode]
28
+ # @return [Array<RuboCop::AST::SendNode>]
29
+ def disable_ddl_transactions_from(node)
30
+ node.each_ancestor(:def).first&.left_siblings&.select do |sibling|
31
+ sibling.is_a?(::RuboCop::AST::SendNode) &&
32
+ disable_ddl_transaction?(sibling)
33
+ end || []
34
+ end
35
+
27
36
  # @param corrector [RuboCop::Cop::Corrector]
28
37
  # @param node [RuboCop::AST::SendNode]
29
38
  # @return [void]
@@ -40,10 +49,7 @@ module RuboCop
40
49
  # @param node [RuboCop::AST::SendNode]
41
50
  # @return [Boolean]
42
51
  def within_disable_ddl_transaction?(node)
43
- node.each_ancestor(:def).first&.left_siblings&.any? do |sibling|
44
- sibling.is_a?(::RuboCop::AST::SendNode) &&
45
- disable_ddl_transaction?(sibling)
46
- end
52
+ !disable_ddl_transactions_from(node).empty?
47
53
  end
48
54
  end
49
55
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Rubocop
4
4
  module Migration
5
- VERSION = '0.4.0'
5
+ VERSION = '0.4.2'
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubocop-migration
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.4.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryo Nakamura
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-10-22 00:00:00.000000000 Z
11
+ date: 2023-07-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -117,7 +117,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
117
117
  - !ruby/object:Gem::Version
118
118
  version: '0'
119
119
  requirements: []
120
- rubygems_version: 3.3.7
120
+ rubygems_version: 3.3.26
121
121
  signing_key:
122
122
  specification_version: 4
123
123
  summary: RuboCop extension focused on ActiveRecord migration.