This project is archived and is in readonly mode.

#3550 ✓stale
johnf

Rollback of database transactions should support multiple databases.

Reported by johnf | December 8th, 2009 @ 12:33 PM

I have an application with two databases.

I setup models as so

class VqdataBase < ActiveRecord::Base
  establish_connection "otherdb_#{RAILS_ENV}"
  self.abstract_class = true
end 

class Video < VqdataBase
end

I do the following for migrations

class InitialSetup < ActiveRecord::Migration
  def self.connection  
    VqdataBase.connection
  end 
end

This works fine except in testing. The second database doesn't have any changes made to it rolled back.

The following patch ensures that all DBs have a rolled back connection

=== modified file 'vendor/rails/activerecord/lib/active_record/fixtures.rb'
--- vendor/rails/activerecord/lib/active_record/fixtures.rb     2009-12-07 23:56:25 +0000
+++ vendor/rails/activerecord/lib/active_record/fixtures.rb     2009-12-08 12:32:39 +0000
@@ -928,9 +928,11 @@
           load_fixtures
           @@already_loaded_fixtures[self.class] = @loaded_fixtures
         end
-        ActiveRecord::Base.connection.increment_open_transactions
-        ActiveRecord::Base.connection.transaction_joinable = false
-        ActiveRecord::Base.connection.begin_db_transaction
+        ActiveRecord::Base.connection_handler.connection_pools.values.map {|pool| pool.connection}.each do |c|
+          c.increment_open_transactions
+          c.transaction_joinable = false
+          c.begin_db_transaction
+        end
       # Load fixtures for every test.
       else
         Fixtures.reset_cache
@@ -950,9 +952,11 @@
       end
 
       # Rollback changes if a transaction is active.
-      if run_in_transaction? && ActiveRecord::Base.connection.open_transactions != 0
-        ActiveRecord::Base.connection.rollback_db_transaction
-        ActiveRecord::Base.connection.decrement_open_transactions
+      ActiveRecord::Base.connection_handler.connection_pools.values.map {|pool| pool.connection}.each do |c|
+        if run_in_transaction? && c.open_transactions != 0
+          c.rollback_db_transaction
+          c.decrement_open_transactions
+        end
       end
       ActiveRecord::Base.clear_active_connections!
     end

Comments and changes to this ticket

  • Matt Kern

    Matt Kern February 24th, 2010 @ 11:41 PM

    I've got this same need. I'll test out this patch, too. But I was thinking that it'd be really nice if the ActiveRecord::Base.configurations array held multiple databases. That would help with the rake tasks (db:test:*) since they seem to only look at the ActiveRecord::Base.configurations['test'] elements. So you could either nest the multiple databases in the database.yml "test" section, -OR- add anything with a "_test" appended it to it as a top level yaml section.

    Thoughts?

  • Matt Kern

    Matt Kern February 26th, 2010 @ 06:53 AM

    I'm getting lock timeout issues with this patch on 2.3.5.

  • Santiago Pastorino

    Santiago Pastorino February 2nd, 2011 @ 04:54 PM

    • State changed from “new” to “open”

    This issue has been automatically marked as stale because it has not been commented on for at least three months.

    The resources of the Rails core team are limited, and so we are asking for your help. If you can still reproduce this error on the 3-0-stable branch or on master, please reply with all of the information you have about it and add "[state:open]" to your comment. This will reopen the ticket for review. Likewise, if you feel that this is a very important feature for Rails to include, please reply with your explanation so we can consider it.

    Thank you for all your contributions, and we hope you will understand this step to focus our efforts where they are most helpful.

  • Santiago Pastorino

    Santiago Pastorino February 2nd, 2011 @ 04:54 PM

    • State changed from “open” to “stale”

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

People watching this ticket

Pages