Skip to content
This repository
Browse code

Merge pull request #5537 from kennyj/fix_4399-32

[3-2-stable] migrate(:down) method with table_name_prefix
  • Loading branch information...
commit 0382e440800740c53f43e4fcb378b38d0150b853 2 parents ef48cea + 565bfb9
Aaron Patterson authored March 23, 2012
28  activerecord/lib/active_record/migration.rb
@@ -346,12 +346,24 @@ def initialize
346 346
       @name       = self.class.name
347 347
       @version    = nil
348 348
       @connection = nil
  349
+      @reverting  = false
349 350
     end
350 351
 
351 352
     # instantiate the delegate object after initialize is defined
352 353
     self.verbose  = true
353 354
     self.delegate = new
354 355
 
  356
+    def revert
  357
+      @reverting = true
  358
+      yield
  359
+    ensure
  360
+      @reverting = false
  361
+    end
  362
+
  363
+    def reverting?
  364
+      @reverting
  365
+    end
  366
+
355 367
     def up
356 368
       self.class.delegate = self
357 369
       return unless self.class.respond_to?(:up)
@@ -385,9 +397,11 @@ def migrate(direction)
385 397
             end
386 398
             @connection = conn
387 399
             time = Benchmark.measure {
388  
-              recorder.inverse.each do |cmd, args|
389  
-                send(cmd, *args)
390  
-              end
  400
+              self.revert {
  401
+                recorder.inverse.each do |cmd, args|
  402
+                  send(cmd, *args)
  403
+                end
  404
+              }
391 405
             }
392 406
           else
393 407
             time = Benchmark.measure { change }
@@ -442,9 +456,11 @@ def method_missing(method, *arguments, &block)
442 456
       arg_list = arguments.map{ |a| a.inspect } * ', '
443 457
 
444 458
       say_with_time "#{method}(#{arg_list})" do
445  
-        unless arguments.empty? || method == :execute
446  
-          arguments[0] = Migrator.proper_table_name(arguments.first)
447  
-          arguments[1] = Migrator.proper_table_name(arguments.second) if method == :rename_table
  459
+        unless reverting?
  460
+          unless arguments.empty? || method == :execute
  461
+            arguments[0] = Migrator.proper_table_name(arguments.first)
  462
+            arguments[1] = Migrator.proper_table_name(arguments.second) if method == :rename_table
  463
+          end
448 464
         end
449 465
         return super unless connection.respond_to?(method)
450 466
         connection.send(method, *arguments, &block)
12  activerecord/test/cases/invertible_migration_test.rb
@@ -88,5 +88,17 @@ def test_down
88 88
       LegacyMigration.down
89 89
       assert !ActiveRecord::Base.connection.table_exists?("horses"), "horses should not exist"
90 90
     end
  91
+
  92
+    def test_migrate_down_with_table_name_prefix
  93
+      ActiveRecord::Base.table_name_prefix = 'p_'
  94
+      ActiveRecord::Base.table_name_suffix = '_s'
  95
+      migration = InvertibleMigration.new
  96
+      migration.migrate(:up)
  97
+      assert_nothing_raised { migration.migrate(:down) }
  98
+      assert !ActiveRecord::Base.connection.table_exists?("p_horses_s"), "p_horses_s should not exist"
  99
+    ensure
  100
+      ActiveRecord::Base.table_name_prefix = ActiveRecord::Base.table_name_suffix = ''
  101
+    end
  102
+
91 103
   end
92 104
 end

0 notes on commit 0382e44

Please sign in to comment.
Something went wrong with that request. Please try again.