Skip to content

Commit 7506a31

Browse files
authored
Merge pull request #205 from joshpencheon/remove_foreign_key_to_table
# Address false positive when reversing remove_foreign_key
2 parents 652bb56 + f851ed9 commit 7506a31

File tree

4 files changed

+17
-1
lines changed

4 files changed

+17
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
* [#180](https://github.com/rubocop-hq/rubocop-rails/issues/180): Fix a false positive for `HttpPositionalArguments` when using `get` method with `:to` option. ([@koic][])
1212
* [#193](https://github.com/rubocop-hq/rubocop-rails/issues/193): Make `Rails/EnvironmentComparison` aware of `Rails.env` is used in RHS or when `!=` is used for comparison. ([@koic][])
13+
* [#205](https://github.com/rubocop-hq/rubocop-rails/pull/205): Make `Rails/ReversibleMigration` aware of `:to_table` option of `remove_foreign_key`. ([@joshpencheon][])
1314

1415
## 2.4.2 (2020-01-26)
1516

@@ -141,3 +142,4 @@
141142
[@mvz]: https://github.com/mvz
142143
[@fidalgo]: https://github.com/fidalgo
143144
[@hanachin]: https://github.com/hanachin
145+
[@joshpencheon]: https://github.com/joshpencheon

lib/rubocop/cop/rails/reversible_migration.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,11 @@ module Rails
9090
# remove_foreign_key :accounts, :branches
9191
# end
9292
#
93+
# # good
94+
# def change
95+
# remove_foreign_key :accounts, to_table: :branches
96+
# end
97+
#
9398
# @example
9499
# # change_table
95100
#
@@ -210,7 +215,7 @@ def check_remove_column_node(node)
210215

211216
def check_remove_foreign_key_node(node)
212217
remove_foreign_key_call(node) do |arg|
213-
if arg.hash_type?
218+
if arg.hash_type? && !all_hash_key?(arg, :to_table)
214219
add_offense(
215220
node,
216221
message: format(MSG,

manual/cops_rails.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2053,6 +2053,11 @@ end
20532053
def change
20542054
remove_foreign_key :accounts, :branches
20552055
end
2056+
2057+
# good
2058+
def change
2059+
remove_foreign_key :accounts, to_table: :branches
2060+
end
20562061
```
20572062
```ruby
20582063
# change_table

spec/rubocop/cop/rails/reversible_migration_spec.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,10 @@ def change
169169
remove_foreign_key :accounts, :branches
170170
RUBY
171171

172+
it_behaves_like 'accepts', 'remove_foreign_key(with :to_table)', <<~RUBY
173+
remove_foreign_key :accounts, to_table: :branches
174+
RUBY
175+
172176
it_behaves_like 'offense', 'remove_foreign_key(without table)', <<~RUBY
173177
remove_foreign_key :accounts, column: :owner_id
174178
RUBY

0 commit comments

Comments
 (0)