Skip to content

Instantly share code, notes, and snippets.

@QQism
Created August 8, 2012 07:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save QQism/3293261 to your computer and use it in GitHub Desktop.
Save QQism/3293261 to your computer and use it in GitHub Desktop.
TPL Rails 3.2 multiple database db-charmer QuickStart

Rails 3.2 multiple databases with DB-Charmer, Quickstart

Currently, to support rails 3.1 til 3.2.3, we need to install latest version (1.7.1)

gem 'db-charmer', '>= 1.7.1', :require => 'db_charmer'

Assume that we have two database server at 192.168.1.14 and 192.168.1.15

common: &common
  adapter: postgresql
  database: my_db
  username: postgres
  password: postgres
  port: 5432
  pool: 50
  timeout: 5000

test:
  <<: *common
  database: bet_test
  host: localhost

production:
  <<: *common
  host:  192.168.1.14 # *this trick avoids broken startup, and migration*
  sql1:
    <<: *common
    host: 192.168.1.14 # master
  sql2:
    <<: *common
    host: 192.168.1.15 # slave

Model

class Post < ActiveRecord::Base
  # add this method to models that needs multiple database queries
  db_magic :connection => :sql1, :slave => :sql2
  		
  attr_accessible :body, :title
end

By default, SELECT queries will access slave databases.

For more detail, visit http://kovyrin.github.com/db-charmer/index.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment