ActiveRecord 3.1.0 multiple databases

后端 未结 2 1524
我在风中等你
我在风中等你 2020-12-31 15:01

I\'m trying to upgrade the ActiveRecord gem to the latest 3.1.0 release and seeing a lot of exceptions being raised, I think it\'s due to how we handle multiple databases.

相关标签:
2条回答
  • 2020-12-31 15:36

    I ran into the same issue yesterday while upgrading to ActiveRecord 3.1.0. I can't speak to whether there is a new recommended way of handling multiple database connections in ActiveRecord 3.1, but I did find a way to unblock myself.

    It appears a connection must now be established on ActiveRecord::Base in order for it to determine the table name lengths/rules of the adapter. Along with the rest of my connections established in my database initializer, I now also have an ActiveRecord::Base connection established to one of my DBs (it doesn't matter which one).

    I'd like to think there's a better solution to be found, but I'm happy to be unblocked for now.

    0 讨论(0)
  • 2020-12-31 15:36

    I am using this solution - What I was seeing was that when establish_connection was called in each of the OtherDb classes - there seemed to be alot of overhead reloading table definitions and I would randomly see issues every time the class def was reloaded.

    # The idea here is to specify that a given model should use another
    # database without having to change the entire inheritance hierarchy
    
    # declare model for table in primary connection
    class Bar < ActiveRecord::Base
      # assume we have logic here that we don't want to refactor into a module
      # but we do want to inherit in OtherDb::Bar
    end
    
    module Foo
    
      # base model in Foo namespace - uses another db
      class BaseConnection < ActiveRecord::Base
        # OtherDb::Title.database contains a db config hash
        # This would probably go in the initializers
        establish_connection OtherDb::Title.database 
      end
    
      # module used to override db connection
      module OtherDb::Base
        def retrieve_connection
          # connection_handler.retrieve_connection(self) #  normal behavior
          connection_handler.retrieve_connection(Foo::BaseConnection) # use db from Foo::BaseConnection
        end
      end
    
      # Foo::Bar is identical to ::Bar but is in another db
      class Bar < ::Bar
        extend OtherDb::Base
      end
    end
    
    0 讨论(0)
提交回复
热议问题