mysql_replication_adapter 0.1.1 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/License.txt +1 -1
- data/README.txt +6 -2
- data/lib/mysql_replication_adapter.rb +15 -4
- data/lib/mysql_replication_adapter/version.rb +2 -2
- data/website/index.html +1 -1
- metadata +2 -2
data/License.txt
CHANGED
data/README.txt
CHANGED
@@ -23,7 +23,7 @@ development:
|
|
23
23
|
username: writeuser
|
24
24
|
password: yourwritepassword
|
25
25
|
database: yourapp_development
|
26
|
-
|
26
|
+
slaves:
|
27
27
|
- host: slavedb1
|
28
28
|
port: 3306
|
29
29
|
username: user
|
@@ -41,7 +41,7 @@ And that's it. It's configured now.
|
|
41
41
|
|
42
42
|
Usage
|
43
43
|
================
|
44
|
-
There are
|
44
|
+
There are a number of ways to make use of the MysqlReplicationAdapter's slave-balancing capabilities. The simplest way is to pass a new option to ActiveRecord::Base#find. The option is called :use_slave, and it should => true when you want to send the query to a slave. For instance:
|
45
45
|
|
46
46
|
class Author < ActiveRecord::Base; end;
|
47
47
|
|
@@ -57,6 +57,10 @@ end
|
|
57
57
|
|
58
58
|
Note: if you use the block syntax and cause a write query to be generated somehow, then you will receive an exception. The adapter explicitly stops you from writing to any database but the master.
|
59
59
|
|
60
|
+
Another set of methods that can take advantage of the slave balancing is the calculations. For instance:
|
61
|
+
|
62
|
+
Author.count(:age, :use_slave => true)
|
63
|
+
|
60
64
|
Finally, I'm sure there are those of you saying, "But I use find_by_sql and that doesn't take an options hash!" Well, good news! There is now an optional second parameter to find_by_sql. If you pass true as that second parameter, it will select a random database and load balance that individual query. Snazzy! Example:
|
61
65
|
|
62
66
|
Author.find_by_sql("SELECT * FROM authors WHERE name = 'bryan';", true) # will be load balanced
|
@@ -110,8 +110,6 @@ module ActiveRecord
|
|
110
110
|
result
|
111
111
|
end
|
112
112
|
|
113
|
-
|
114
|
-
|
115
113
|
alias_method :old_find_by_sql, :find_by_sql
|
116
114
|
# Override find_by_sql so that you can tell it to selectively use a slave machine
|
117
115
|
def find_by_sql(sql, use_slave = false)
|
@@ -121,6 +119,17 @@ module ActiveRecord
|
|
121
119
|
old_find_by_sql sql
|
122
120
|
end
|
123
121
|
end
|
122
|
+
|
123
|
+
alias_method :old_calculate, :calculate
|
124
|
+
|
125
|
+
def calculate(operation, column_name, options ={})
|
126
|
+
use_slave = options.delete(:use_slave)
|
127
|
+
if use_slave && connection.is_a?(ConnectionAdapters::MysqlReplicationAdapter)
|
128
|
+
connection.load_balance_query {old_calculate(operation, column_name, options)}
|
129
|
+
else
|
130
|
+
old_calculate(operation, column_name, options)
|
131
|
+
end
|
132
|
+
end
|
124
133
|
end
|
125
134
|
|
126
135
|
end
|
@@ -483,8 +492,10 @@ module ActiveRecord
|
|
483
492
|
|
484
493
|
# connect to all the clone machines
|
485
494
|
@clones = []
|
486
|
-
|
487
|
-
|
495
|
+
clone_config = @config[:clones] || @config[:slaves]
|
496
|
+
|
497
|
+
if clone_config
|
498
|
+
clone_config.each do |clone|
|
488
499
|
conn = Mysql.init
|
489
500
|
if encoding
|
490
501
|
conn.options(Mysql::SET_CHARSET_NAME, encoding) rescue nil
|
data/website/index.html
CHANGED
@@ -33,7 +33,7 @@
|
|
33
33
|
<h1>mysql_replication_adapter</h1>
|
34
34
|
<div id="version" class="clickable" onclick='document.location = "http://rubyforge.org/projects/mysql_replication_adapter"; return false'>
|
35
35
|
<p>Get Version</p>
|
36
|
-
<a href="http://rubyforge.org/projects/mysql_replication_adapter" class="numbers">0.
|
36
|
+
<a href="http://rubyforge.org/projects/mysql_replication_adapter" class="numbers">0.2.0</a>
|
37
37
|
</div>
|
38
38
|
<h1>→ ‘mysql_replication_adapter’</h1>
|
39
39
|
|
metadata
CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.4
|
|
3
3
|
specification_version: 1
|
4
4
|
name: mysql_replication_adapter
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version: 0.
|
7
|
-
date: 2007-
|
6
|
+
version: 0.2.0
|
7
|
+
date: 2007-08-01 00:00:00 -07:00
|
8
8
|
summary: An ActiveRecord database adapter that allows you to specify a single write master and multiple read-only slaves.
|
9
9
|
require_paths:
|
10
10
|
- lib
|