diff options
author | Yves Senn <yves.senn@gmail.com> | 2015-06-12 15:45:19 +0200 |
---|---|---|
committer | Yves Senn <yves.senn@gmail.com> | 2015-06-12 15:45:19 +0200 |
commit | 98cacae101e73b24cc4fd11fb8e1daf3fdc6de45 (patch) | |
tree | 39991d69bf13a75bec1709c618b8f573a555494f /activerecord/lib | |
parent | 0158f9261f0f3d7be5bb5974ceef96bcf862766a (diff) | |
parent | a186663b3d274b7aa648683f3fdf7a816fe90763 (diff) | |
download | rails-98cacae101e73b24cc4fd11fb8e1daf3fdc6de45.tar.gz rails-98cacae101e73b24cc4fd11fb8e1daf3fdc6de45.tar.bz2 rails-98cacae101e73b24cc4fd11fb8e1daf3fdc6de45.zip |
Merge pull request #20226 from EpicH0liday/reversible-remove-foreign-key
Make remove_foreign_key reversible
Conflicts:
activerecord/CHANGELOG.md
Diffstat (limited to 'activerecord/lib')
-rw-r--r-- | activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb | 6 | ||||
-rw-r--r-- | activerecord/lib/active_record/migration/command_recorder.rb | 10 |
2 files changed, 15 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..c8be038d76 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 option parameters provided + # will be used to re-add the foreign key in case of a migration rollback. + # 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,7 @@ module ActiveRecord # # remove_foreign_key :accounts, name: :special_fk_name # + # The +options+ hash accepts the same keys as SchemaStatements#add_foreign_key. 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) |