public
Description: ActiveRecord connection proxy for master/slave connections
Clone URL: git://github.com/technoweenie/masochism.git
technoweenie (author)
Tue Jul 01 13:38:26 -0700 2008
commit  fec38cfbe77dbe569672c568328fe7b8db3f0b87
tree    d2315e83f8c061ccd0bd4ced8fc357f5541f3b58
parent  8de9fda345c30d7119dda3b639822cd594682d41 parent  0b58f4511dbc2747912935b24725892cae403ec9
name age message
file LICENSE Fri Oct 05 22:59:57 -0700 2007 allow some more flexibility on when exactly the... [rick]
file README Tue Jul 01 13:38:26 -0700 2008 Merge commit 'jsmecham/master' [technoweenie]
directory lib/ Tue Jul 01 12:54:07 -0700 2008 Added support for declaring your master databas... [jsmecham]
README
masochism
=========

The masochism plugin provides an easy solution for Ruby on Rails applications to
work in a replicated database environment. Connection proxy sends some database
queries (those in a transaction, update statements, and ActiveRecord::Base#reload) 
to a master database, and the rest to the slave database. 

The ActiveReload::MasterDatabase model uses a 'master_database' setting that
can either be defined for all of your environments, or for each environment as
a nested declaration:

  # config/database.yml
  login: &login
    adapter: postgresql
    host: localhost
    port: 5432

  production:
    database: production_slave_database_name
    <<: *login

  master_database:
    database: production_master_database_name
    <<: *login

  staging:
    database: staging_database_name
    host: slave-db-pool.local
    <<: *login
    master_database: 
      database: staging_database_name
      host: master-db-server.local
      <<: *login

  development: # Does not use masochism
    database: development_database_name
    <<: *login

To setup:

Whether you want this in production only, or maybe just your deployment server, 
is up to you. Just call the following method in your desired environment file.

  ActiveReload::ConnectionProxy.setup!

Some suggestions:

  * in a config/initializer
  * config.after_initialize block

If you are using the Litespeed web server, child processes are initialized on 
creation, which means any setup done in an environment file will be effectively 
ignored. A brief discussion of the problem is posted here:
http://litespeedtech.com/support/wiki/doku.php?id=litespeed_wiki:rails:memcache

One solution for Litespeed users is to check the connection at your first request
and do the setup! call if your connection hasn't been initialized, like:

class ApplicationController < ActionController::Base
  prepend_before_filter do |controller|
    ActiveReload::ConnectionProxy.setup! unless ActiveRecord::Base.connection.is_a? ActiveReload::ConnectionProxy
  end
  ...
end


If you want a model to always use the Master database, you can inherit 
ActiveRelaod::MasterDatabase.  Any models with their own database connection
will not be affected.

Setting up your own proxies:

  # Sets up MyMaster's connection as the master database connection for User.
  ActiveReload::ConnectionProxy.setup_for MyMaster, User