I have a rails app using 2 databases (say DB1, DB2). Also the DB1 is replicated to DB3, DB2 to DB4. I am using octopus gem for replication i.e. all write operations are on DB1 and read operations from DB3.
Following is my setting for database.yml
production:
adapter: mysql2
database: DB1
username: root
host: localhost
password: root
pool: 5
timeout: 5000
reconnect: true
DB2_production:
adapter: mysql2
database: DB2
username: root
host: localhost
password: root
pool: 5
timeout: 5000
reconnect: true
Now I have file shards.yml as following
octopus:
replicated: true
environments:
- production
- DB2_production
production:
slave1:
adapter: mysql2
database: DB3
username: root
host: localhost
password: root
pool: 5
timeout: 5000
reconnect: true
DB2_production:
slave2:
adapter: mysql2
database: DB4
username: root
host: localhost
password: root
pool: 5
timeout: 5000
reconnect: true
Also I am using 'octopus_establish_connection' to connect to DB2 in my model
class MyModel1 < ActiveRecord::Base
octopus_establish_connection "DB2_#{Rails.env.to_s}"
end
class MyModel2 < ActiveRecord::Base
octopus_establish_connection "DB2_#{Rails.env.to_s}"
end
So here if there is a read query from MyModel1 the DB3 is queried and for write query DB1 is queried. But in MyModel2 for both cases DB2 is queried, while DB4 should be queried.
Thanks