master_slave_adapter

An ActiveRecord database adapter that allows you to setup a "master/slave" environment

MIT License

Stars
99

master_slave_adapter - maurício DOT linhares AT gmail DOT com

This simple plugin acts as a common ActiveRecord adapter and allows you to setup a master-slave environment using any database you like (and is supported by ActiveRecord).

This plugin works by handling two connections, one to a master database, that will receive all non-"SELECT" statements, and another to a slave database that that is going to receive all SELECT statements. It also tries to do as little black magic as possible, it works just like any other ActiveRecord database adapter and performs no monkeypatching at all, so it's easy and simple to use and understand.

The master database connection will also receive SELECT calls if a transaction is active at the moment or if a command is executed inside a "with_master" block:

ActiveRecord::Base.with_master do # :with_master instructs the adapter @users = User.all # to use the master connection inside the block end

To use this adapter you just have to install the plugin:

ruby script/plugin install git://github.com/mauricio/master_slave_adapter.git

And then configure it at your database.yml file:

development: database: sample_development username: root adapter: master_slave # the adapter must be set to "master_slave" host: 10.21.34.80 master_slave_adapter: mysql # here's where you'll place the real database adapter name disable_connection_test: true # this will disable the connection test before use, # can possibly improve the performance but you could also # hit stale connections, default is false eager_load_connections: true # connections are lazy loaded by default, you can load gem eagerly setting this to true master: # and here's where you'll add the master database configuration database: talkies_development # you shouldn't specify an "adapter" here, the username: root # value at "master_slave_adapter" is going to be used host: 10.21.34.82 adapter: postgresql # you can use another adapter for the master connection if needed # if you don't set it the "master_slave_adapter" property will be used

Related Projects