aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib
diff options
context:
space:
mode:
authorAster Ryan <astersamazing@gmail.com>2015-05-20 10:05:51 -0400
committerAster Ryan <astersamazing@gmail.com>2015-06-11 19:24:46 -0400
commita186663b3d274b7aa648683f3fdf7a816fe90763 (patch)
tree0312dff8c6a3f0563809cfb57004511c09af4114 /activerecord/lib
parente2dfa54db881fceae92bdf793d5545c3f0e22898 (diff)
downloadrails-a186663b3d274b7aa648683f3fdf7a816fe90763.tar.gz
rails-a186663b3d274b7aa648683f3fdf7a816fe90763.tar.bz2
rails-a186663b3d274b7aa648683f3fdf7a816fe90763.zip
Add an invert method for remove_foreign_key
Diffstat (limited to 'activerecord/lib')
-rw-r--r--activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb7
-rw-r--r--activerecord/lib/active_record/migration/command_recorder.rb10
2 files changed, 16 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb b/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb
index dd7dcb9cdd..a89886721a 100644
--- a/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb
+++ b/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb
@@ -777,7 +777,10 @@ module ActiveRecord
execute schema_creation.accept(at)
end
- # Removes the given foreign key from the table.
+ # Removes the given foreign key from the table. Any options parameters provided
+ # will be used to re-add the foreign key in case of a migration reversion.
+ # It is recommended that you provide any options used when creating the foreign
+ # key so that the migration can be reverted properly.
#
# Removes the foreign key on +accounts.branch_id+.
#
@@ -791,6 +794,8 @@ module ActiveRecord
#
# remove_foreign_key :accounts, name: :special_fk_name
#
+ # The +options+ hash can include all keys that can be used in add_foreign_key.
+ # Please refer to the add_foreign_key documentation for a full list
def remove_foreign_key(from_table, options_or_to_table = {})
return unless supports_foreign_keys?
diff --git a/activerecord/lib/active_record/migration/command_recorder.rb b/activerecord/lib/active_record/migration/command_recorder.rb
index 36256415df..ee4545ed71 100644
--- a/activerecord/lib/active_record/migration/command_recorder.rb
+++ b/activerecord/lib/active_record/migration/command_recorder.rb
@@ -184,6 +184,16 @@ module ActiveRecord
[:remove_foreign_key, [from_table, options]]
end
+ def invert_remove_foreign_key(args)
+ from_table, to_table, remove_options = args
+ raise ActiveRecord::IrreversibleMigration, "remove_foreign_key is only reversible if given a second table" if to_table.nil? || to_table.is_a?(Hash)
+
+ reversed_args = [from_table, to_table]
+ reversed_args << remove_options if remove_options
+
+ [:add_foreign_key, reversed_args]
+ end
+
# Forwards any missing method call to the \target.
def method_missing(method, *args, &block)
if @delegate.respond_to?(method)