rubocop-migration 0.3.2 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +2 -2
- data/README.md +6 -7
- data/lib/rubocop/cop/migration/change_column_null.rb +117 -37
- data/lib/rubocop/migration/version.rb +1 -1
- metadata +2 -3
- data/CHANGELOG.md +0 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 819776e7d688a0d112d1ad7ecc2994125de7bfa7e4b8fe27105f21d2a8061565
|
4
|
+
data.tar.gz: fa2c6bfce21e4dd4ad206e6fb049162ee25b016bc2be01c22782d3dc0871e255
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b9fecf517eab16cd9f6c16e302d7a392c0b263a75c164b53a99337b017d4416894fde4eac40874ca53e4fa61c77783f4ef5cbde64007b5388dc6ceefd79d644d
|
7
|
+
data.tar.gz: da06bac2b33c7018964708c0aec62808262e14478b09ff2af0303f6ce00034a57d2331a64ed9fd973ec34012525ec7051b362c71843cbf23f27e17d6f30d5248
|
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
rubocop-migration (0.
|
4
|
+
rubocop-migration (0.4.0)
|
5
5
|
activesupport
|
6
6
|
rubocop (>= 1.34)
|
7
7
|
rubocop-rails
|
@@ -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.
|
60
|
+
rubocop-rails (2.17.0)
|
61
61
|
activesupport (>= 4.2.0)
|
62
62
|
rack (>= 1.1)
|
63
63
|
rubocop (>= 1.33.0, < 2.0)
|
data/README.md
CHANGED
@@ -6,14 +6,11 @@ RuboCop extension focused on ActiveRecord migration.
|
|
6
6
|
|
7
7
|
## Usage
|
8
8
|
|
9
|
-
|
10
|
-
|
11
|
-
This gem is not yet published to rubygems.org.
|
12
|
-
See [#1](https://github.com/r7kamura/rubocop-migration/issues/1) for more details.
|
9
|
+
Install `rubocop-migration` gem:
|
13
10
|
|
14
11
|
```ruby
|
15
12
|
# Gemfile
|
16
|
-
gem 'rubocop-migration', require: false
|
13
|
+
gem 'rubocop-migration', require: false
|
17
14
|
```
|
18
15
|
|
19
16
|
then require `rubocop-migration` and enable the cops you want to use in your .rubocop.yml:
|
@@ -53,6 +50,8 @@ Please read the comments of the respective cop classes for more information.
|
|
53
50
|
|
54
51
|
## Acknowledgements
|
55
52
|
|
56
|
-
This gem was heavily inspired by
|
53
|
+
This gem was heavily inspired by [ankane/strong_migrations](https://github.com/ankane/strong_migrations).
|
54
|
+
|
55
|
+
The gem `rubocop-migration` was originally developed at [wealthsimple/rubocop-migration](https://github.com/wealthsimple/rubocop-migration), and later the gem name was transferred to this repository.
|
57
56
|
|
58
|
-
|
57
|
+
Some cops were originally created at [r7kamura/sevencop](https://github.com/r7kamura/sevencop) then moved to this repository.
|
@@ -40,12 +40,13 @@ module RuboCop
|
|
40
40
|
|
41
41
|
RESTRICT_ON_SEND = %i[
|
42
42
|
change_column_null
|
43
|
+
change_null
|
43
44
|
].freeze
|
44
45
|
|
45
46
|
# @param node [RuboCop::AST::SendNode]
|
46
47
|
# @return [void]
|
47
48
|
def on_send(node)
|
48
|
-
return if
|
49
|
+
return if called_with_validate_constraint?(node)
|
49
50
|
|
50
51
|
add_offense(node) do |corrector|
|
51
52
|
autocorrect(corrector, node)
|
@@ -54,26 +55,6 @@ module RuboCop
|
|
54
55
|
|
55
56
|
private
|
56
57
|
|
57
|
-
# @!method parse_table_name_and_column_name(node)
|
58
|
-
# @param node [RuboCop::AST::SendNode]
|
59
|
-
# @return [Array<Symbol>, nil]
|
60
|
-
def_node_matcher :parse_table_name_and_column_name, <<~PATTERN
|
61
|
-
(send
|
62
|
-
nil?
|
63
|
-
_
|
64
|
-
({str sym} $_)
|
65
|
-
({str sym} $_)
|
66
|
-
...
|
67
|
-
)
|
68
|
-
PATTERN
|
69
|
-
|
70
|
-
# @!method remove_check_constraint?(node)
|
71
|
-
# @param node [RuboCop::AST::SendNode]
|
72
|
-
# @return [Boolean]
|
73
|
-
def_node_matcher :remove_check_constraint?, <<~PATTERN
|
74
|
-
(send nil? :remove_check_constraint ...)
|
75
|
-
PATTERN
|
76
|
-
|
77
58
|
# @!method validate_constraint?(node)
|
78
59
|
# @param node [RuboCop::AST::SendNode]
|
79
60
|
# @return [Boolean]
|
@@ -88,39 +69,138 @@ module RuboCop
|
|
88
69
|
corrector,
|
89
70
|
node
|
90
71
|
)
|
91
|
-
|
72
|
+
case node.method_name
|
73
|
+
when :change_column_null
|
74
|
+
autocorrect_change_column_null(corrector, node)
|
75
|
+
when :change_null
|
76
|
+
autocorrect_change_null(corrector, node)
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
# @param corrector [RuboCop::Cop::Corrector]
|
81
|
+
# @param node [RuboCop::AST::SendNode]
|
82
|
+
# @return [void]
|
83
|
+
def autocorrect_change_column_null(
|
84
|
+
corrector,
|
85
|
+
node
|
86
|
+
)
|
92
87
|
corrector.replace(
|
93
88
|
node,
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
89
|
+
format_add_check_constraint(
|
90
|
+
column_name: find_column_name_from_change_column_null(node),
|
91
|
+
table_name: find_table_name_from_change_column_null(node)
|
92
|
+
)
|
93
|
+
)
|
94
|
+
end
|
95
|
+
|
96
|
+
# @param corrector [RuboCop::Cop::Corrector]
|
97
|
+
# @param node [RuboCop::AST::SendNode]
|
98
|
+
# @return [void]
|
99
|
+
def autocorrect_change_null(
|
100
|
+
corrector,
|
101
|
+
node
|
102
|
+
)
|
103
|
+
corrector.replace(
|
104
|
+
node.location.selector.with(
|
105
|
+
end_pos: node.location.expression.end_pos
|
106
|
+
),
|
107
|
+
format_check_constraint(
|
108
|
+
column_name: find_column_name_from_change_null(node),
|
109
|
+
table_name: find_table_name_from_change_null(node)
|
99
110
|
)
|
100
111
|
)
|
101
112
|
end
|
102
113
|
|
103
114
|
# @param node [RuboCop::AST::SendNode]
|
104
115
|
# @return [Boolean]
|
105
|
-
def
|
106
|
-
node.
|
116
|
+
def called_with_validate_constraint?(node)
|
117
|
+
case node.method_name
|
118
|
+
when :change_column_null
|
119
|
+
node
|
120
|
+
when :change_null
|
121
|
+
find_ancestor_change_table(node)
|
122
|
+
end.left_siblings.any? do |sibling|
|
107
123
|
validate_constraint?(sibling)
|
108
124
|
end
|
109
125
|
end
|
110
126
|
|
111
127
|
# @param node [RuboCop::AST::SendNode]
|
112
|
-
# @return [
|
113
|
-
def
|
114
|
-
node.
|
115
|
-
|
128
|
+
# @return [RuboCop::AST::BlockNode]
|
129
|
+
def find_ancestor_change_table(node)
|
130
|
+
node.each_ancestor(:block).find do |ancestor|
|
131
|
+
ancestor.method?(:change_table)
|
116
132
|
end
|
117
133
|
end
|
118
134
|
|
119
135
|
# @param node [RuboCop::AST::SendNode]
|
120
|
-
# @return [
|
121
|
-
def
|
122
|
-
|
123
|
-
|
136
|
+
# @return [String]
|
137
|
+
def find_column_name_from_change_column_null(node)
|
138
|
+
node.arguments[1].value.to_s
|
139
|
+
end
|
140
|
+
|
141
|
+
# @param node [RuboCop::AST::SendNode]
|
142
|
+
# @return [String]
|
143
|
+
def find_column_name_from_change_null(node)
|
144
|
+
node.arguments[0].value.to_s
|
145
|
+
end
|
146
|
+
|
147
|
+
# @parm node [RuboCop::AST::SendNode]
|
148
|
+
# @return [String]
|
149
|
+
def find_table_name_from_change_column_null(node)
|
150
|
+
node.arguments[0].value.to_s
|
151
|
+
end
|
152
|
+
|
153
|
+
# @param node [RuboCop::AST::SendNode]
|
154
|
+
# @return [String]
|
155
|
+
def find_table_name_from_change_null(node)
|
156
|
+
find_ancestor_change_table(node).send_node.arguments[0].value.to_s
|
157
|
+
end
|
158
|
+
|
159
|
+
# @param column_name [String]
|
160
|
+
# @param table_name [String]
|
161
|
+
# @return [String]
|
162
|
+
def format_add_check_constraint(
|
163
|
+
column_name:,
|
164
|
+
table_name:
|
165
|
+
)
|
166
|
+
format(
|
167
|
+
'add_check_constraint :%<table_name>s, %<arguments>s',
|
168
|
+
arguments: format_check_constraint_arguments(
|
169
|
+
column_name: column_name,
|
170
|
+
table_name: table_name
|
171
|
+
),
|
172
|
+
table_name: table_name
|
173
|
+
)
|
174
|
+
end
|
175
|
+
|
176
|
+
# @param column_name [String]
|
177
|
+
# @param table_name [String]
|
178
|
+
# @return [String]
|
179
|
+
def format_check_constraint(
|
180
|
+
column_name:,
|
181
|
+
table_name:
|
182
|
+
)
|
183
|
+
format(
|
184
|
+
'check_constraint %<arguments>s',
|
185
|
+
arguments: format_check_constraint_arguments(
|
186
|
+
column_name: column_name,
|
187
|
+
table_name: table_name
|
188
|
+
)
|
189
|
+
)
|
190
|
+
end
|
191
|
+
|
192
|
+
# @param coumn_name [String]
|
193
|
+
# @param table_name [String]
|
194
|
+
# @return [String]
|
195
|
+
def format_check_constraint_arguments(
|
196
|
+
column_name:,
|
197
|
+
table_name:
|
198
|
+
)
|
199
|
+
format(
|
200
|
+
"'%<column_name>s IS NOT NULL', name: '%<constraint_name>s', validate: false",
|
201
|
+
column_name: column_name,
|
202
|
+
constraint_name: "#{table_name}_#{column_name}_is_not_null"
|
203
|
+
)
|
124
204
|
end
|
125
205
|
end
|
126
206
|
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
|
+
version: 0.4.0
|
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-
|
11
|
+
date: 2022-10-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -61,7 +61,6 @@ extra_rdoc_files: []
|
|
61
61
|
files:
|
62
62
|
- ".rspec"
|
63
63
|
- ".rubocop.yml"
|
64
|
-
- CHANGELOG.md
|
65
64
|
- CODE_OF_CONDUCT.md
|
66
65
|
- Gemfile
|
67
66
|
- Gemfile.lock
|