From 47017bd1697d6b4d6780356a403f91536eacd689 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Fri, 19 Nov 2010 10:31:03 -0800 Subject: invertable migrations are working --- activerecord/lib/active_record/migration.rb | 20 +++++++++++++++++++- .../lib/active_record/migration/command_recorder.rb | 2 +- activerecord/test/cases/invertable_migration_test.rb | 5 ----- 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/activerecord/lib/active_record/migration.rb b/activerecord/lib/active_record/migration.rb index e7063bca11..99dd50ccb1 100644 --- a/activerecord/lib/active_record/migration.rb +++ b/activerecord/lib/active_record/migration.rb @@ -332,7 +332,25 @@ module ActiveRecord time = nil ActiveRecord::Base.connection_pool.with_connection do |conn| @connection = conn - time = Benchmark.measure { send(direction) } + if respond_to?(:change) + if direction == :down + recorder = CommandRecorder.new(@connection) + suppress_messages do + @connection = recorder + change + end + @connection = conn + time = Benchmark.measure { + recorder.inverse.each do |cmd, args| + send(cmd, *args) + end + } + else + time = Benchmark.measure { change } + end + else + time = Benchmark.measure { send(direction) } + end @connection = nil end diff --git a/activerecord/lib/active_record/migration/command_recorder.rb b/activerecord/lib/active_record/migration/command_recorder.rb index fa189b8009..fc669d2e89 100644 --- a/activerecord/lib/active_record/migration/command_recorder.rb +++ b/activerecord/lib/active_record/migration/command_recorder.rb @@ -3,7 +3,7 @@ module ActiveRecord # ActiveRecord::Migration::CommandRecorder records commands done during # a migration and knows how to reverse those commands. class CommandRecorder - attr_reader :commands, :delegate + attr_accessor :commands, :delegate def initialize(delegate = nil) @commands = [] diff --git a/activerecord/test/cases/invertable_migration_test.rb b/activerecord/test/cases/invertable_migration_test.rb index 2477048954..ab08cf6fe2 100644 --- a/activerecord/test/cases/invertable_migration_test.rb +++ b/activerecord/test/cases/invertable_migration_test.rb @@ -21,11 +21,6 @@ module ActiveRecord end end - def test_invertable? - migration = InvertableMigration.new - assert migration.invertable?, 'should be invertable' - end - def test_up migration = InvertableMigration.new migration.migrate(:up) -- cgit v1.2.3