active_record_shards 2.7.5 → 2.8.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/active_record_shards/default_slave_patches.rb +35 -46
- data/lib/active_record_shards.rb +4 -0
- data/test/connection_switching_test.rb +20 -1
- data/test/models.rb +2 -0
- data/test/schema.rb +5 -0
- metadata +4 -4
@@ -1,23 +1,32 @@
|
|
1
1
|
module ActiveRecordShards
|
2
2
|
module DefaultSlavePatches
|
3
|
-
|
3
|
+
def self.wrap_method_in_on_slave(class_method, base, method)
|
4
4
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
class << self
|
11
|
-
def #{slave_method}_with_default_slave#{punctuation}(*args, &block)
|
12
|
-
on_slave_unless_tx do
|
13
|
-
#{slave_method}_without_default_slave#{punctuation}(*args, &block)
|
14
|
-
end
|
15
|
-
end
|
5
|
+
if class_method
|
6
|
+
base_methods = (base.methods | base.private_methods).map(&:to_sym)
|
7
|
+
else
|
8
|
+
base_methods = (base.instance_methods | base.private_instance_methods).map(&:to_sym)
|
9
|
+
end
|
16
10
|
|
17
|
-
|
11
|
+
return unless base_methods.include?(method)
|
12
|
+
_, method, punctuation = method.to_s.match(/^(.*?)([\?\!]?)$/).to_a
|
13
|
+
base.class_eval <<-RUBY, __FILE__, __LINE__ + 1
|
14
|
+
#{class_method ? "class << self" : ""}
|
15
|
+
def #{method}_with_default_slave#{punctuation}(*args, &block)
|
16
|
+
on_slave_unless_tx do
|
17
|
+
#{method}_without_default_slave#{punctuation}(*args, &block)
|
18
|
+
end
|
18
19
|
end
|
19
|
-
|
20
|
-
|
20
|
+
|
21
|
+
alias_method_chain :#{method}#{punctuation}, :default_slave
|
22
|
+
#{class_method ? "end" : ""}
|
23
|
+
RUBY
|
24
|
+
end
|
25
|
+
|
26
|
+
CLASS_SLAVE_METHODS = [ :find_by_sql, :count_by_sql, :calculate, :find_one, :find_some, :find_every, :quote_value, :sanitize_sql_hash_for_conditions, :exists?, :table_exists? ]
|
27
|
+
|
28
|
+
def self.extended(base)
|
29
|
+
CLASS_SLAVE_METHODS.each { |m| ActiveRecordShards::DefaultSlavePatches.wrap_method_in_on_slave(true, base, m) }
|
21
30
|
|
22
31
|
base.class_eval do
|
23
32
|
# fix ActiveRecord to do the right thing, and use our aliased quote_value
|
@@ -58,33 +67,10 @@ module ActiveRecordShards
|
|
58
67
|
end
|
59
68
|
|
60
69
|
alias_method_chain :transaction, :slave_off
|
61
|
-
|
62
|
-
|
63
|
-
def table_exists_with_default_slave?(*args)
|
64
|
-
on_slave_unless_tx(*args) { table_exists_without_default_slave?(*args) }
|
65
|
-
end
|
66
|
-
|
67
|
-
alias_method_chain :table_exists?, :default_slave
|
68
70
|
end
|
69
71
|
end
|
70
|
-
|
71
|
-
|
72
|
-
ActiveRecord::Associations::HasAndBelongsToManyAssociation.class_eval do
|
73
|
-
def construct_sql_with_default_slave(*args, &block)
|
74
|
-
on_slave_unless_tx do
|
75
|
-
construct_sql_without_default_slave(*args, &block)
|
76
|
-
end
|
77
|
-
end
|
78
|
-
|
79
|
-
def construct_find_options_with_default_slave!(*args, &block)
|
80
|
-
on_slave_unless_tx do
|
81
|
-
construct_find_options_without_default_slave!(*args, &block)
|
82
|
-
end
|
83
|
-
end
|
84
|
-
|
85
|
-
alias_method_chain :construct_sql, :default_slave if respond_to?(:construct_sql)
|
86
|
-
alias_method_chain :construct_find_options!, :default_slave if respond_to?(:construct_find_options!)
|
87
|
-
end
|
72
|
+
ActiveRecordShards::DefaultSlavePatches.wrap_method_in_on_slave(false, ActiveRecord::Associations::HasAndBelongsToManyAssociation, :construct_sql)
|
73
|
+
ActiveRecordShards::DefaultSlavePatches.wrap_method_in_on_slave(false, ActiveRecord::Associations::HasAndBelongsToManyAssociation, :construct_find_options!)
|
88
74
|
end
|
89
75
|
|
90
76
|
def on_slave_unless_tx(&block)
|
@@ -97,20 +83,23 @@ module ActiveRecordShards
|
|
97
83
|
|
98
84
|
module ActiveRelationPatches
|
99
85
|
def self.included(base)
|
100
|
-
|
101
|
-
|
86
|
+
ActiveRecordShards::DefaultSlavePatches.wrap_method_in_on_slave(false, base, :calculate)
|
87
|
+
ActiveRecordShards::DefaultSlavePatches.wrap_method_in_on_slave(false, base, :exists?)
|
88
|
+
ActiveRecordShards::DefaultSlavePatches.wrap_method_in_on_slave(false, base, :pluck)
|
102
89
|
end
|
103
90
|
|
104
91
|
def on_slave_unless_tx
|
105
92
|
@klass.on_slave_unless_tx { yield }
|
106
93
|
end
|
94
|
+
end
|
107
95
|
|
108
|
-
|
109
|
-
|
96
|
+
module HasAndBelongsToManyPreloaderPatches
|
97
|
+
def self.included(base)
|
98
|
+
ActiveRecordShards::DefaultSlavePatches.wrap_method_in_on_slave(false, base, :records_for) rescue nil
|
110
99
|
end
|
111
100
|
|
112
|
-
def
|
113
|
-
on_slave_unless_tx {
|
101
|
+
def on_slave_unless_tx
|
102
|
+
klass.on_slave_unless_tx { yield }
|
114
103
|
end
|
115
104
|
end
|
116
105
|
end
|
data/lib/active_record_shards.rb
CHANGED
@@ -22,6 +22,10 @@ if ActiveRecord.const_defined?(:Relation)
|
|
22
22
|
ActiveRecord::Relation.send(:include, ActiveRecordShards::DefaultSlavePatches::ActiveRelationPatches)
|
23
23
|
end
|
24
24
|
|
25
|
+
if ActiveRecord::Associations.const_defined?(:Preloader)
|
26
|
+
ActiveRecord::Associations::Preloader::HasAndBelongsToMany.send(:include, ActiveRecordShards::DefaultSlavePatches::HasAndBelongsToManyPreloaderPatches)
|
27
|
+
end
|
28
|
+
|
25
29
|
if ActiveRecord::VERSION::STRING >= "3.1.0"
|
26
30
|
ActiveRecord::Associations::CollectionProxy.send(:include, ActiveRecordShards::AssociationCollectionConnectionSelection)
|
27
31
|
else
|
@@ -380,13 +380,19 @@ describe "connection switching" do
|
|
380
380
|
end
|
381
381
|
end
|
382
382
|
|
383
|
-
# TODO: make all this stuff rails 3 compatible.
|
384
383
|
describe "with finds routed to the slave by default" do
|
385
384
|
before do
|
386
385
|
Account.on_slave_by_default = true
|
386
|
+
Person.on_slave_by_default = true
|
387
387
|
Account.connection.execute("INSERT INTO accounts (id, name, created_at, updated_at) VALUES(1000, 'master_name', '2009-12-04 20:18:48', '2009-12-04 20:18:48')")
|
388
388
|
Account.on_slave.connection.execute("INSERT INTO accounts (id, name, created_at, updated_at) VALUES(1000, 'slave_name', '2009-12-04 20:18:48', '2009-12-04 20:18:48')")
|
389
389
|
Account.on_slave.connection.execute("INSERT INTO accounts (id, name, created_at, updated_at) VALUES(1001, 'slave_name2', '2009-12-04 20:18:48', '2009-12-04 20:18:48')")
|
390
|
+
|
391
|
+
Person.connection.execute("REPLACE INTO people(id, name) VALUES(10, 'master person')")
|
392
|
+
Person.on_slave.connection.execute("REPLACE INTO people(id, name) VALUES(20, 'slave person')")
|
393
|
+
|
394
|
+
Account.connection.execute("INSERT INTO account_people(account_id, person_id) VALUES(1000, 10)")
|
395
|
+
Account.on_slave.connection.execute("INSERT INTO account_people(account_id, person_id) VALUES(1001, 20)")
|
390
396
|
end
|
391
397
|
|
392
398
|
it "find() by default on the slave" do
|
@@ -445,8 +451,21 @@ describe "connection switching" do
|
|
445
451
|
assert AccountInherited.on_slave_by_default?
|
446
452
|
end
|
447
453
|
|
454
|
+
it "will :include things via has_and_belongs associations correctly" do
|
455
|
+
a = Account.first(:conditions => "id = 1001", :include => :people)
|
456
|
+
assert a.people.size > 0
|
457
|
+
assert_equal 'slave person', a.people.first.name
|
458
|
+
end
|
459
|
+
|
460
|
+
if ActiveRecord::VERSION::MAJOR >= 3 && ActiveRecord::VERSION::MINOR >= 2
|
461
|
+
it "supports .pluck" do
|
462
|
+
assert_equal ["slave_name", "slave_name2"], Account.pluck(:name)
|
463
|
+
end
|
464
|
+
end
|
465
|
+
|
448
466
|
after do
|
449
467
|
Account.on_slave_by_default = false
|
468
|
+
Person.on_slave_by_default = false
|
450
469
|
end
|
451
470
|
end
|
452
471
|
end
|
data/test/models.rb
CHANGED
@@ -4,6 +4,7 @@ class Account < ActiveRecord::Base
|
|
4
4
|
|
5
5
|
has_many :tickets
|
6
6
|
has_many :account_things
|
7
|
+
has_and_belongs_to_many :people, :join_table => 'account_people'
|
7
8
|
end
|
8
9
|
|
9
10
|
class AccountThing < ActiveRecord::Base
|
@@ -36,3 +37,4 @@ end
|
|
36
37
|
class User < Person
|
37
38
|
end
|
38
39
|
|
40
|
+
|
data/test/schema.rb
CHANGED
@@ -12,6 +12,11 @@ ActiveRecord::Schema.define(:version => 1) do
|
|
12
12
|
t.boolean "enabled", :default => true
|
13
13
|
end
|
14
14
|
|
15
|
+
create_table "account_people", :force => true, :id => false do |t|
|
16
|
+
t.integer "account_id"
|
17
|
+
t.integer "person_id"
|
18
|
+
end
|
19
|
+
|
15
20
|
create_table "emails", :force => true do |t|
|
16
21
|
t.string "from"
|
17
22
|
t.string "to"
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_record_shards
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.8.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -11,7 +11,7 @@ authors:
|
|
11
11
|
autorequire:
|
12
12
|
bindir: bin
|
13
13
|
cert_chain: []
|
14
|
-
date: 2013-
|
14
|
+
date: 2013-11-22 00:00:00.000000000 Z
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
17
17
|
name: activerecord
|
@@ -83,7 +83,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
83
83
|
version: '0'
|
84
84
|
segments:
|
85
85
|
- 0
|
86
|
-
hash:
|
86
|
+
hash: -479183148352030802
|
87
87
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
88
88
|
none: false
|
89
89
|
requirements:
|
@@ -92,7 +92,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
92
92
|
version: '0'
|
93
93
|
segments:
|
94
94
|
- 0
|
95
|
-
hash:
|
95
|
+
hash: -479183148352030802
|
96
96
|
requirements: []
|
97
97
|
rubyforge_project:
|
98
98
|
rubygems_version: 1.8.25
|