SwitchConnection

Gem Version Build Status Coverage Status Code Climate

Switching database connection between multiple slave and writable one. Fork from switch_point gem. Original Version: https://github.com/eagletmt/switch_point.

Installation

Add this line to your application's Gemfile:

gem 'switch_connection'

And then execute:

$ bundle

Or install it yourself as:

$ gem install switch_connection

Usage

Suppose you have 4 databases: db-blog-master, db-blog-slave, db-comment-master and db-comment-slave. Article model and Category model are stored in db-blog-master,slave and Comment model is stored in db-comment-master,slave.

Configuration

In database.yml:

production_blog_master:
  adapter: mysql2
  username: blog_writable
  host: db-blog-master
production_blog_slave:
  adapter: mysql2
  username: blog_slave
  host: db-blog-slave
production_comment_master:
    ...

In initializer:

SwitchConnection.configure do |config|
  config.define_switch_point :blog,
    slaves: [:"#{Rails.env}_blog_slave1",:"#{Rails.env}_blog_slave2"]
    master: :"#{Rails.env}_blog_master"
  config.define_switch_point :comment,
    slaves: [:"#{Rails.env}_comment_slave"]
    master: :"#{Rails.env}_comment_master"
end

In models:

class Article < ActiveRecord::Base
  use_switch_point :blog
end

class Category < ActiveRecord::Base
  use_switch_point :blog
end

class Comment < ActiveRecord::Base
  use_switch_point :comment
end

Switching connections

  • Write query automatically go master database, read query automatically go to slave database.

    article = Article.find(1) # read query go to slave
    article.name = "hoge"
    article.save # write query go to master
    
  • Use with_master to force query go to master database.

    Article.with_master do
    article.save! # Write to master db
    Article.first # Read from master db
    end
    
  • Force query to master database.

    Article.with_master { Article.all }
    Article.with_master { Article.find(1) }
    Article.with_master { Article.where(name: "foobar").to_a }
    
  • with_switch_point

    Book.with_switch_point(:main) { Book.count  }
    

Note that Article and Category shares their connections.

Special case: ActiveRecord::Base.connection

Basically, each connection managed by a proxy isn't shared between proxies. But there's one exception: ActiveRecord::Base.

Contributing

  1. Fork it ( https://github.com/phamvanmhung2e123/switch_point/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request