Skip to content

Commit 1038d95

Browse files
committed
Fix a false positive for Rails/ReversibleMigration
Follow up to #110 (comment). This PR fixes a false positive for `Rails/ReversibleMigration` when using `t.remove` with `type` option in Rails 6.1.
1 parent 7570fb6 commit 1038d95

File tree

3 files changed

+39
-6
lines changed

3 files changed

+39
-6
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#553](https://github.com/rubocop/rubocop-rails/pull/553): Fix a false positive for `Rails/ReversibleMigration` when using `t.remove` with `type` option in Rails 6.1. ([@koic][])

lib/rubocop/cop/rails/reversible_migration.rb

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -317,16 +317,24 @@ def check_change_table_offense(receiver, node)
317317
return if receiver != node.receiver &&
318318
reversible_change_table_call?(node)
319319

320+
action = if method_name == :remove
321+
target_rails_version >= 6.1 ? 't.remove (without type)' : 't.remove'
322+
else
323+
"change_table(with #{method_name})"
324+
end
325+
320326
add_offense(
321327
node,
322-
message: format(MSG, action: "change_table(with #{method_name})")
328+
message: format(MSG, action: action)
323329
)
324330
end
325331

326332
def reversible_change_table_call?(node)
327333
case node.method_name
328-
when :change, :remove
334+
when :change
329335
false
336+
when :remove
337+
target_rails_version >= 6.1 && all_hash_key?(node.arguments.last, :type)
330338
when :change_default, :change_column_default, :change_table_comment,
331339
:change_column_comment
332340
all_hash_key?(node.arguments.last, :from, :to)

spec/rubocop/cop/rails/reversible_migration_spec.rb

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -213,11 +213,35 @@ def change
213213
end
214214
RUBY
215215

216-
it_behaves_like 'offense', 'change_table(with remove)', <<~RUBY
217-
change_table :users do |t|
218-
t.remove :qualification
216+
context 'remove' do
217+
context 'Rails >= 6.1', :rails61 do
218+
it_behaves_like 'accepts', 't.remove (with type)', <<~RUBY
219+
change_table :users do |t|
220+
t.remove(:posts, type: :text)
221+
end
222+
RUBY
223+
224+
it_behaves_like 'offense', 't.remove (without type)', <<~RUBY
225+
change_table :users do |t|
226+
t.remove(:posts)
227+
end
228+
RUBY
219229
end
220-
RUBY
230+
231+
context 'Rails < 6.1', :rails60 do
232+
it_behaves_like 'offense', 't.remove', <<~RUBY
233+
change_table :users do |t|
234+
t.remove(:posts, type: :text)
235+
end
236+
RUBY
237+
238+
it_behaves_like 'offense', 't.remove', <<~RUBY
239+
change_table :users do |t|
240+
t.remove(:posts)
241+
end
242+
RUBY
243+
end
244+
end
221245
end
222246

223247
context 'remove_columns' do

0 commit comments

Comments
 (0)