1

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

Paritosh Singh
  • 6,288
  • 5
  • 37
  • 56
  • Did you get this working? Am having problems this way too. – simonmorley Aug 31 '14 at 17:24
  • @simonmorley No, I haven't got this fixed till now. – Paritosh Singh Sep 01 '14 at 10:33
  • Doesn't seem to work at all and I can't see whether it's supported either. Will raise an issue on Github and paste link here. – simonmorley Sep 01 '14 at 10:48
  • @simonmorley: Yeah, that is the last choice. – Paritosh Singh Sep 02 '14 at 07:45
  • Take a look at what I had done in my previous app. http://imnithin.weebly.com/multiple-dbs-in-rails.html – Nithin Oct 27 '14 at 07:26
  • @Nithin Here we are not taking about connecting multiple databases in rails. The problem is that we are using a gem octopus, through which all read operations from our models are send to slave database servers (on which only read operations are performed), only write operations are sent to master database. Through this gem we are not able to replicate slave servers for different database. – Paritosh Singh Nov 10 '14 at 06:11

0 Answers0