diff options
author | Yves Senn <yves.senn@gmail.com> | 2014-06-10 15:00:59 +0200 |
---|---|---|
committer | Yves Senn <yves.senn@gmail.com> | 2014-06-26 22:03:48 +0200 |
commit | 6073d7c683b19fc7394baa9a93bc44e71e071129 (patch) | |
tree | a53d01cc85457f47308aa0eb3a843206604d2a7d /activerecord/lib | |
parent | 402f303f1d938cf2c7781d7734c4ff8e6b874f35 (diff) | |
download | rails-6073d7c683b19fc7394baa9a93bc44e71e071129.tar.gz rails-6073d7c683b19fc7394baa9a93bc44e71e071129.tar.bz2 rails-6073d7c683b19fc7394baa9a93bc44e71e071129.zip |
fk: make `add_foreign_key` reversible.
Diffstat (limited to 'activerecord/lib')
-rw-r--r-- | activerecord/lib/active_record/migration/command_recorder.rb | 17 |
1 files changed, 16 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/migration/command_recorder.rb b/activerecord/lib/active_record/migration/command_recorder.rb index c44d8c1665..bd66c941a2 100644 --- a/activerecord/lib/active_record/migration/command_recorder.rb +++ b/activerecord/lib/active_record/migration/command_recorder.rb @@ -74,7 +74,9 @@ module ActiveRecord :rename_index, :rename_column, :add_index, :remove_index, :add_timestamps, :remove_timestamps, :change_column_default, :add_reference, :remove_reference, :transaction, :drop_join_table, :drop_table, :execute_block, :enable_extension, - :change_column, :execute, :remove_columns, :change_column_null # irreversible methods need to be here too + :change_column, :execute, :remove_columns, :change_column_null, + :add_foreign_key, :remove_foreign_key + # irreversible methods need to be here too ].each do |method| class_eval <<-EOV, __FILE__, __LINE__ + 1 def #{method}(*args, &block) # def create_table(*args, &block) @@ -167,6 +169,19 @@ module ActiveRecord [:change_column_null, args] end + def invert_add_foreign_key(args) + from_table, _to_table, add_options = *args + add_options ||= {} + + if add_options[:name] + options = {name: add_options[:name]} + elsif add_options[:column] + options = {column: add_options[:column]} + end + + [:remove_foreign_key, [from_table, options]] + end + # Forwards any missing method call to the \target. def method_missing(method, *args, &block) if @delegate.respond_to?(method) |