From e43e8e268f0522f9f11f4eacae1d04afca768c9b Mon Sep 17 00:00:00 2001 From: Marc-Andre Lafortune Date: Tue, 18 Dec 2012 23:32:45 -0500 Subject: Make change_table reversible when possible [#8267] --- .../lib/active_record/migration/command_recorder.rb | 6 +++++- .../test/cases/migration/command_recorder_test.rb | 20 ++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/activerecord/lib/active_record/migration/command_recorder.rb b/activerecord/lib/active_record/migration/command_recorder.rb index 4ea709fe69..f738c909bc 100644 --- a/activerecord/lib/active_record/migration/command_recorder.rb +++ b/activerecord/lib/active_record/migration/command_recorder.rb @@ -70,7 +70,7 @@ module ActiveRecord super || delegate.respond_to?(*args) end - [:create_table, :create_join_table, :change_table, :rename_table, :add_column, :remove_column, + [:create_table, :create_join_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, :remove_index, @@ -84,6 +84,10 @@ module ActiveRecord alias :add_belongs_to :add_reference alias :remove_belongs_to :remove_reference + def change_table(table_name, options = {}) + yield ConnectionAdapters::Table.new(table_name, self) + end + private module StraightReversions diff --git a/activerecord/test/cases/migration/command_recorder_test.rb b/activerecord/test/cases/migration/command_recorder_test.rb index 1c5530a132..b406beba06 100644 --- a/activerecord/test/cases/migration/command_recorder_test.rb +++ b/activerecord/test/cases/migration/command_recorder_test.rb @@ -77,6 +77,26 @@ module ActiveRecord [:drop_table, ["figs"], block]], @recorder.commands end + def test_invert_change_table + @recorder.revert do + @recorder.change_table :fruits do |t| + t.string :name + t.rename :kind, :cultivar + end + end + assert_equal [ + [:rename_column, [:fruits, :cultivar, :kind]], + [:remove_column, [:fruits, :name, :string, {}], nil], + ], @recorder.commands + + assert_raises(ActiveRecord::IrreversibleMigration) do + @recorder.revert do + @recorder.change_table :fruits do |t| + t.remove :kind + end + end + end + end def test_invert_create_table @recorder.revert do -- cgit v1.2.3