Skip to content

Commit

Permalink
allow some more flexibility on when exactly the proxy is setup
Browse files Browse the repository at this point in the history
git-svn-id: http://ar-code.svn.engineyard.com/plugins/masochism@31 d98f8484-0aa7-43af-80ea-302c351499d6
  • Loading branch information
rick committed Oct 6, 2007
1 parent 3942270 commit 720142f
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 5 deletions.
18 changes: 18 additions & 0 deletions LICENSE
@@ -0,0 +1,18 @@
Copyright (c) 2007 Rick Olson

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 changes: 28 additions & 0 deletions README
@@ -0,0 +1,28 @@
masochism
==============

Connection proxy sends some queries (thoes 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 in database.yml to serve as the master database.

To setup:

Whether you want this in production only, or maybe just your deployment server,
is up to you. Just call this method somewhere:

ActiveReload::ConnectionProxy.setup!

Some suggestions:

* in a config/initializer
* config.after_initialize block

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
2 changes: 0 additions & 2 deletions init.rb

This file was deleted.

15 changes: 12 additions & 3 deletions lib/active_reload/connection_proxy.rb
Expand Up @@ -5,14 +5,23 @@ class MasterDatabase < ActiveRecord::Base
end

class ConnectionProxy
def initialize
@slave = ActiveRecord::Base.connection
@master = ActiveReload::MasterDatabase.connection
def initialize(master, slave)
@slave = slave.connection
@master = master.connection
@current = @slave
end

attr_accessor :slave, :master

def self.setup!
setup_for ActiveReload::MasterDatabase
end

def self.setup_for(master, slave = nil)
slave ||= ActiveRecord::Base
ActiveRecord::Base.active_connections[slave.name] = new(master, slave)
end

def with_master
set_to_master!
yield
Expand Down

0 comments on commit 720142f

Please sign in to comment.