Skip to content

Commit 798b39e

Browse files
committed
Add remove_reference check to Rails/ReversibleMigration
1 parent 26d508b commit 798b39e

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* Add `remove_reference` check to `Rails/ReversibleMigration`. ([@mattmccormick][])

lib/rubocop/cop/rails/reversible_migration.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ class ReversibleMigration < Base
179179
MSG = '%<action>s is not reversible.'
180180

181181
def_node_matcher :irreversible_schema_statement_call, <<~PATTERN
182-
(send nil? ${:change_column :execute :remove_belongs_to} ...)
182+
(send nil? ${:change_column :execute :remove_belongs_to :remove_reference} ...)
183183
PATTERN
184184

185185
def_node_matcher :drop_table_call, <<~PATTERN

spec/rubocop/cop/rails/reversible_migration_spec.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,20 @@ def change
171171
RUBY
172172
end
173173

174+
context 'remove_belongs_to' do
175+
it_behaves_like 'accepts', 'up_only', <<~RUBY
176+
up_only { remove_belongs_to(:products, :user, index: false) }
177+
RUBY
178+
179+
it_behaves_like 'offense', 'remove_belongs_to', <<~RUBY
180+
remove_belongs_to(:products, :user, index: false)
181+
RUBY
182+
183+
it_behaves_like 'offense', 'remove_belongs_to', <<~RUBY
184+
remove_belongs_to(:products, :supplier, polymorphic: true)
185+
RUBY
186+
end
187+
174188
context 'remove_column' do
175189
it_behaves_like 'accepts', 'remove_column(with type)', <<~RUBY
176190
remove_column(:suppliers, :qualification, :string)
@@ -199,6 +213,20 @@ def change
199213
RUBY
200214
end
201215

216+
context 'remove_reference' do
217+
it_behaves_like 'accepts', 'up_only', <<~RUBY
218+
up_only { remove_reference(:products, :user, index: false) }
219+
RUBY
220+
221+
it_behaves_like 'offense', 'remove_reference', <<~RUBY
222+
remove_reference(:products, :user, index: false)
223+
RUBY
224+
225+
it_behaves_like 'offense', 'remove_reference', <<~RUBY
226+
remove_reference(:products, :supplier, polymorphic: true)
227+
RUBY
228+
end
229+
202230
context 'change_table' do
203231
it_behaves_like 'accepts', 'change_table(with reversible calls)', <<~RUBY
204232
change_table :users do |t|

0 commit comments

Comments
 (0)