From 6073d7c683b19fc7394baa9a93bc44e71e071129 Mon Sep 17 00:00:00 2001 From: Yves Senn Date: Tue, 10 Jun 2014 15:00:59 +0200 Subject: fk: make `add_foreign_key` reversible. --- .../lib/active_record/migration/command_recorder.rb | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) (limited to 'activerecord/lib/active_record/migration/command_recorder.rb') 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) -- cgit v1.2.3 From d074b821489b6d58101d1474dd514990f4bdf0fa Mon Sep 17 00:00:00 2001 From: Yves Senn Date: Tue, 10 Jun 2014 15:29:19 +0200 Subject: fk: infere column name from table names. This allows to create and remove foreign keys without specifying a column. --- activerecord/lib/active_record/migration/command_recorder.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'activerecord/lib/active_record/migration/command_recorder.rb') diff --git a/activerecord/lib/active_record/migration/command_recorder.rb b/activerecord/lib/active_record/migration/command_recorder.rb index bd66c941a2..ad726a3c02 100644 --- a/activerecord/lib/active_record/migration/command_recorder.rb +++ b/activerecord/lib/active_record/migration/command_recorder.rb @@ -170,13 +170,15 @@ module ActiveRecord end def invert_add_foreign_key(args) - from_table, _to_table, add_options = *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]} + else + options = to_table end [:remove_foreign_key, [from_table, options]] -- cgit v1.2.3 From 24e1aefb4b2d7b2b4babfd4bae1e9e613283b003 Mon Sep 17 00:00:00 2001 From: Yves Senn Date: Thu, 26 Jun 2014 13:09:45 +0200 Subject: fk: review corrections: indent, visibility, syntax, wording. --- activerecord/lib/active_record/migration/command_recorder.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'activerecord/lib/active_record/migration/command_recorder.rb') diff --git a/activerecord/lib/active_record/migration/command_recorder.rb b/activerecord/lib/active_record/migration/command_recorder.rb index ad726a3c02..f833caaab6 100644 --- a/activerecord/lib/active_record/migration/command_recorder.rb +++ b/activerecord/lib/active_record/migration/command_recorder.rb @@ -170,13 +170,13 @@ module ActiveRecord end def invert_add_foreign_key(args) - from_table, to_table, add_options = *args + from_table, to_table, add_options = args add_options ||= {} if add_options[:name] - options = {name: add_options[:name]} + options = { name: add_options[:name] } elsif add_options[:column] - options = {column: add_options[:column]} + options = { column: add_options[:column] } else options = to_table end -- cgit v1.2.3