Skip to content

Commit

Permalink
Respect options passed to foreign_key when reverting add_reference
Browse files Browse the repository at this point in the history
The code incorrectly assumes that the option was written as
`foreign_key: true`, but that is not always the case. This now mirrors
the behavior of reverting `add_foreign_key`. The code was changed to use
kwargs while I was touching it, as well.

This could really use a refactoring to go through the same code paths as
`add_refernce` in the future, so we don't duplicate default values.

Fixes rails#25169
  • Loading branch information
sgrif authored and prathamesh-sonpatki committed Jul 23, 2016
1 parent 3a5c86c commit d18fd49
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
Expand Up @@ -842,14 +842,19 @@ def add_reference(table_name, *args)
#
# remove_reference(:products, :user, index: true, foreign_key: true)
#
def remove_reference(table_name, ref_name, options = {})
if options[:foreign_key]
def remove_reference(table_name, ref_name, foreign_key: false, polymorphic: false, **options)
if foreign_key
reference_name = Base.pluralize_table_names ? ref_name.to_s.pluralize : ref_name
remove_foreign_key(table_name, reference_name)
if foreign_key.is_a?(Hash)
foreign_key_options = foreign_key
else
foreign_key_options = { to_table: reference_name }
end
remove_foreign_key(table_name, **foreign_key_options)
end

remove_column(table_name, "#{ref_name}_id")
remove_column(table_name, "#{ref_name}_type") if options[:polymorphic]
remove_column(table_name, "#{ref_name}_type") if polymorphic
end
alias :remove_belongs_to :remove_reference

Expand Down
15 changes: 15 additions & 0 deletions activerecord/test/cases/invertible_migration_test.rb
Expand Up @@ -151,6 +151,14 @@ def change
end
end

class RevertCustomForeignKeyTable < SilentMigration
def change
change_table(:horses) do |t|
t.references :owner, foreign_key: { to_table: :developers }
end
end
end

setup do
@verbose_was, ActiveRecord::Migration.verbose = ActiveRecord::Migration.verbose, false
end
Expand Down Expand Up @@ -353,6 +361,13 @@ def test_migrate_down_with_table_name_prefix
ActiveRecord::Base.table_name_prefix = ActiveRecord::Base.table_name_suffix = ''
end

def test_migrations_can_handle_foreign_keys_to_specific_tables
migration = RevertCustomForeignKeyTable.new
InvertibleMigration.migrate(:up)
migration.migrate(:up)
migration.migrate(:down)
end

# MySQL 5.7 and Oracle do not allow to create duplicate indexes on the same columns
unless current_adapter?(:Mysql2Adapter, :OracleAdapter)
def test_migrate_revert_add_index_with_name
Expand Down

0 comments on commit d18fd49

Please sign in to comment.