Skip to content

Commit

Permalink
migrate(:down) method with table_name_prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
kennyj committed Mar 21, 2012
1 parent 275ee0d commit 565bfb9
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
28 changes: 22 additions & 6 deletions activerecord/lib/active_record/migration.rb
Expand Up @@ -346,12 +346,24 @@ def initialize
@name = self.class.name
@version = nil
@connection = nil
@reverting = false
end

# instantiate the delegate object after initialize is defined
self.verbose = true
self.delegate = new

def revert
@reverting = true
yield
ensure
@reverting = false
end

def reverting?
@reverting
end

def up
self.class.delegate = self
return unless self.class.respond_to?(:up)
Expand Down Expand Up @@ -385,9 +397,11 @@ def migrate(direction)
end
@connection = conn
time = Benchmark.measure {
recorder.inverse.each do |cmd, args|
send(cmd, *args)
end
self.revert {
recorder.inverse.each do |cmd, args|
send(cmd, *args)
end
}
}
else
time = Benchmark.measure { change }
Expand Down Expand Up @@ -442,9 +456,11 @@ def method_missing(method, *arguments, &block)
arg_list = arguments.map{ |a| a.inspect } * ', '

say_with_time "#{method}(#{arg_list})" do
unless arguments.empty? || method == :execute
arguments[0] = Migrator.proper_table_name(arguments.first)
arguments[1] = Migrator.proper_table_name(arguments.second) if method == :rename_table
unless reverting?
unless arguments.empty? || method == :execute
arguments[0] = Migrator.proper_table_name(arguments.first)
arguments[1] = Migrator.proper_table_name(arguments.second) if method == :rename_table
end
end
return super unless connection.respond_to?(method)
connection.send(method, *arguments, &block)
Expand Down
12 changes: 12 additions & 0 deletions activerecord/test/cases/invertible_migration_test.rb
Expand Up @@ -88,5 +88,17 @@ def test_down
LegacyMigration.down
assert !ActiveRecord::Base.connection.table_exists?("horses"), "horses should not exist"
end

def test_migrate_down_with_table_name_prefix
ActiveRecord::Base.table_name_prefix = 'p_'
ActiveRecord::Base.table_name_suffix = '_s'
migration = InvertibleMigration.new
migration.migrate(:up)
assert_nothing_raised { migration.migrate(:down) }
assert !ActiveRecord::Base.connection.table_exists?("p_horses_s"), "p_horses_s should not exist"
ensure
ActiveRecord::Base.table_name_prefix = ActiveRecord::Base.table_name_suffix = ''
end

end
end

0 comments on commit 565bfb9

Please sign in to comment.