schoefmann / multi_db

Connection proxy for ActiveRecord for single master / multiple slave database deployments
http://github.com/schoefmax/multi_db
MIT License
144 stars 57 forks source link

Using multi_db with establish_connection #4

Open clayton opened 14 years ago

clayton commented 14 years ago

Can multi_db support the following scenario?

In this example multi_db works just fine for the user model. However, multi_db either ignores or overrides the establish_connection call made in the Team model that is trying to use a connection to a different database. This results in a MySQL error similar to "Mysql::Error: Table 'production.teams' doesn't exist" because teams is not defined in the database named 'production' but rather in the database named 'third_party_db'.

# initiliazers/connection_proxy.rb

MultiDb::ConnectionProxy.setup!

# app/models/user.rb
class User < ActiveRecord::Base
  # typical user model
end

# app/models/team.rb
class Team < ThirdPartyDataProvider  
end

# app/models/third_party_data_provider.rb
class ThirdPartyDataProvider < ActiveRecord::Base
  establish_connection "third_party_db"
end

# config/database.yml
production:
  adapter: mysql
  database: production
  username: root
  password:
  host: 127.0.0.1

production_slave:
  adapter: mysql
  database: production
  username: root
  password:
  host: 127.0.0.2

third_party_db:
  adapter: mysql
  database: third_party
  username: root
  password: 
  host: 127.0.0.1
clayton commented 14 years ago

I found that this does work if you setup your database.yml entries for the third party like so:

third_party_db_production:
  adapter: mysql
  database: third_party
  username: root
  password: 
  host: 127.0.0.1

third_party_db_production_slave_database:
  adapter: mysql
  database: third_party
  username: root
  password: 
  host: 127.0.0.1