public
Description: ActiveRecord connection proxy for master/slave connections
Clone URL: git://github.com/technoweenie/masochism.git
abloom (author)
Wed Oct 01 12:20:09 -0700 2008
commit  4aaa0298207ec5042f66afcc68ccb69ae4e5b48f
tree    aab35248344d4692104aba0e6d6b99cabd9a401f
parent  bc014a64730de22e2bde0fab96a667bbdb5b507c
masochism / README
100644 84 lines (62 sloc) 2.653 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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
  
Using the MasterFilter class is quite simple. If you have any actions you know require
the Master DB for both reads and writes simply do the following:
 
class RandomController < ApplicationController
  around_filter MasterFilter, :only => [:show, :edit, :update]
  
  ...
end