From 313232463d2a378cb3d14a34f5334cc4bd7a07b8 Mon Sep 17 00:00:00 2001 From: Marc-Andre Lafortune Date: Mon, 19 Nov 2012 02:12:40 -0500 Subject: Make remove_index reversible [#8267] --- .../active_record/migration/command_recorder.rb | 14 +++++++---- .../test/cases/migration/command_recorder_test.rb | 27 +++++++++++++++++++--- 2 files changed, 34 insertions(+), 7 deletions(-) (limited to 'activerecord') diff --git a/activerecord/lib/active_record/migration/command_recorder.rb b/activerecord/lib/active_record/migration/command_recorder.rb index 0fca1444f9..d61de6bc47 100644 --- a/activerecord/lib/active_record/migration/command_recorder.rb +++ b/activerecord/lib/active_record/migration/command_recorder.rb @@ -73,7 +73,7 @@ module ActiveRecord [:create_table, :create_join_table, :change_table, :rename_table, :add_column, :remove_column, :rename_index, :rename_column, :add_index, :remove_index, :add_timestamps, :remove_timestamps, :change_column, :change_column_default, :add_reference, :remove_reference, :transaction, - :drop_join_table, :drop_table, :remove_columns, + :drop_join_table, :drop_table, :remove_columns, :remove_index, ].each do |method| class_eval <<-EOV, __FILE__, __LINE__ + 1 def #{method}(*args, &block) # def create_table(*args, &block) @@ -132,9 +132,15 @@ module ActiveRecord def invert_add_index(args) table, columns, options = *args - index_name = options.try(:[], :name) - options_hash = index_name ? {:name => index_name} : {:column => columns} - [:remove_index, [table, options_hash]] + [:remove_index, [table, (options || {}).merge(column: columns)]] + end + + def invert_remove_index(args) + table, options = *args + raise ActiveRecord::IrreversibleMigration, "remove_index is only reversible if given a :column option." unless options && options[:column] + + options = options.dup + [:add_index, [table, options.delete(:column), options]] end def invert_remove_timestamps(args) diff --git a/activerecord/test/cases/migration/command_recorder_test.rb b/activerecord/test/cases/migration/command_recorder_test.rb index 8ebc1adaa6..c12ffb0b4c 100644 --- a/activerecord/test/cases/migration/command_recorder_test.rb +++ b/activerecord/test/cases/migration/command_recorder_test.rb @@ -148,17 +148,38 @@ module ActiveRecord def test_invert_add_index remove = @recorder.inverse_of :add_index, [:table, [:one, :two], options: true] - assert_equal [:remove_index, [:table, {:column => [:one, :two]}]], remove + assert_equal [:remove_index, [:table, {column: [:one, :two], options: true}]], remove end def test_invert_add_index_with_name remove = @recorder.inverse_of :add_index, [:table, [:one, :two], name: "new_index"] - assert_equal [:remove_index, [:table, {:name => "new_index"}]], remove + assert_equal [:remove_index, [:table, {column: [:one, :two], name: "new_index"}]], remove end def test_invert_add_index_with_no_options remove = @recorder.inverse_of :add_index, [:table, [:one, :two]] - assert_equal [:remove_index, [:table, {:column => [:one, :two]}]], remove + assert_equal [:remove_index, [:table, {column: [:one, :two]}]], remove + end + + def test_invert_remove_index + add = @recorder.inverse_of :remove_index, [:table, {column: [:one, :two], options: true}] + assert_equal [:add_index, [:table, [:one, :two], options: true]], add + end + + def test_invert_remove_index_with_name + add = @recorder.inverse_of :remove_index, [:table, {column: [:one, :two], name: "new_index"}] + assert_equal [:add_index, [:table, [:one, :two], name: "new_index"]], add + end + + def test_invert_remove_index_with_no_special_options + add = @recorder.inverse_of :remove_index, [:table, {column: [:one, :two]}] + assert_equal [:add_index, [:table, [:one, :two], {}]], add + end + + def test_invert_remove_index_with_no_column + assert_raises(ActiveRecord::IrreversibleMigration) do + @recorder.inverse_of :remove_index, [:table, name: "new_index"] + end end def test_invert_rename_index -- cgit v1.2.3