decisiv-sharded_database 0.3.1.1 → 0.3.2.1
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/lib/sharded_database/aggregate.rb +1 -4
- data/lib/sharded_database/has_many_association.rb +37 -0
- data/lib/sharded_database/model_with_connection.rb +2 -2
- data/lib/sharded_database.rb +2 -1
- data/test/sharded_database/association_test.rb +16 -2
- data/test/sharded_database/connection_test.rb +2 -19
- metadata +2 -1
@@ -89,12 +89,9 @@ module ShardedDatabase
|
|
89
89
|
|
90
90
|
def #{association.name}_with_connection(*args)
|
91
91
|
reflection = self.class.reflect_on_association(:#{association.name})
|
92
|
+
reflection.options.merge!({ :connection_class => #{@klass.name}} )
|
92
93
|
klass = reflection.klass
|
93
94
|
ModelWithConnection.borrow_connection(klass, #{@klass.name}) do
|
94
|
-
unless reflection.belongs_to?
|
95
|
-
# TODO
|
96
|
-
klass.send :include, ShardedDatabase::ModelWithConnection
|
97
|
-
end
|
98
95
|
#{association.name}_without_connection(*args)
|
99
96
|
end
|
100
97
|
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
module ShardedDatabase
|
2
|
+
module HasManyAssociation
|
3
|
+
|
4
|
+
def self.included(klass)
|
5
|
+
klass.class_eval do
|
6
|
+
alias_method_chain :initialize, :connection
|
7
|
+
#alias_method_chain :find, :connection
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
def initialize_with_connection(owner, reflection)
|
12
|
+
if @connection_class = reflection.options[:connection_class]
|
13
|
+
@original_class = reflection.klass
|
14
|
+
reflection.metaclass.class_eval %{
|
15
|
+
def klass
|
16
|
+
ModelWithConnection.borrow_connection(#{@original_class.name}, #{@connection_class.name})
|
17
|
+
end
|
18
|
+
}
|
19
|
+
end
|
20
|
+
initialize_without_connection(owner, reflection)
|
21
|
+
end
|
22
|
+
|
23
|
+
def find_with_connection(*args)
|
24
|
+
if connection_class = @reflection.options[:connection_class]
|
25
|
+
ShardedDatabase::ModelWithConnection.borrow_connection(@reflection.klass, connection_class) do
|
26
|
+
find_without_connection(*args)
|
27
|
+
end
|
28
|
+
else
|
29
|
+
find_without_connection(*args)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
ActiveRecord::Associations::HasManyAssociation.send :include, ShardedDatabase::HasManyAssociation
|
37
|
+
ActiveRecord::Associations::HasManyThroughAssociation.send :include, ShardedDatabase::HasManyAssociation
|
@@ -15,9 +15,9 @@ module ShardedDatabase
|
|
15
15
|
@eigen = requesting_class.metaclass
|
16
16
|
@eigen.send :alias_method, :proxy_connection, :connection
|
17
17
|
@eigen.delegate :connection, :to => target_class
|
18
|
-
yield(requesting_class)
|
18
|
+
block_given? ? yield(requesting_class) : requesting_class
|
19
19
|
ensure
|
20
|
-
@eigen.send :alias_method, :connection, :proxy_connection
|
20
|
+
@eigen.send :alias_method, :connection, :proxy_connection if block_given?
|
21
21
|
end
|
22
22
|
|
23
23
|
|
data/lib/sharded_database.rb
CHANGED
@@ -1,10 +1,11 @@
|
|
1
1
|
require 'sharded_database/model_with_connection'
|
2
2
|
require 'sharded_database/aggregate_proxy'
|
3
3
|
require 'sharded_database/aggregate'
|
4
|
+
require 'sharded_database/has_many_association'
|
4
5
|
require 'sharded_database/core_extensions'
|
5
6
|
|
6
7
|
module ShardedDatabase
|
7
8
|
|
8
|
-
VERSION = '0.3.
|
9
|
+
VERSION = '0.3.2'
|
9
10
|
|
10
11
|
end
|
@@ -9,9 +9,16 @@ class AssociationTest < ShardedDatabase::TestCase
|
|
9
9
|
|
10
10
|
context 'Connection delegation on has_many associations' do
|
11
11
|
|
12
|
-
|
12
|
+
should 'fetch items from the parent instance connection' do
|
13
13
|
assert_instance_of Array, @parent.items
|
14
|
-
assert_equal
|
14
|
+
assert_equal 1, @parent.items.size
|
15
|
+
end
|
16
|
+
|
17
|
+
should 'return original connection after accessing a has_many association' do
|
18
|
+
original = Item.connection
|
19
|
+
AggregateEmployee.find_by_source('one').items
|
20
|
+
final = Item.connection
|
21
|
+
assert_equal original, final
|
15
22
|
end
|
16
23
|
|
17
24
|
end
|
@@ -30,6 +37,13 @@ class AssociationTest < ShardedDatabase::TestCase
|
|
30
37
|
assert_equal @parent.call_company, @parent.company
|
31
38
|
end
|
32
39
|
|
40
|
+
should 'return original connection after accessing a belongs_to association' do
|
41
|
+
original = Company.connection
|
42
|
+
AggregateEmployee.find_by_source('one').company
|
43
|
+
final = Company.connection
|
44
|
+
assert_equal original, final
|
45
|
+
end
|
46
|
+
|
33
47
|
end
|
34
48
|
|
35
49
|
end
|
@@ -10,8 +10,6 @@ class ConnectionTest < ShardedDatabase::TestCase
|
|
10
10
|
|
11
11
|
should 'have instances of the same source share the same connection' do
|
12
12
|
first, second = AggregateEmployee.find_all_by_source('two', :limit => 2)
|
13
|
-
|
14
|
-
#assert_connection :shard_two, first, second
|
15
13
|
assert_equal first.connection.object_id, second.connection.object_id # ensure that delegation is working correctly
|
16
14
|
end
|
17
15
|
|
@@ -20,16 +18,9 @@ class ConnectionTest < ShardedDatabase::TestCase
|
|
20
18
|
end
|
21
19
|
|
22
20
|
should 'return original connection after accessing just the target model' do
|
23
|
-
original = Employee.connection
|
21
|
+
original = Employee.connection
|
24
22
|
AggregateEmployee.find_by_source('one')
|
25
|
-
final = Employee.connection
|
26
|
-
assert_equal original, final
|
27
|
-
end
|
28
|
-
|
29
|
-
should 'return original connection after accessing an association' do
|
30
|
-
original = Company.connection.instance_variable_get('@config')[:database]
|
31
|
-
AggregateEmployee.find_by_source('one').company
|
32
|
-
final = Company.connection.instance_variable_get('@config')[:database]
|
23
|
+
final = Employee.connection
|
33
24
|
assert_equal original, final
|
34
25
|
end
|
35
26
|
|
@@ -37,18 +28,10 @@ class ConnectionTest < ShardedDatabase::TestCase
|
|
37
28
|
|
38
29
|
should 'allow for an ActiveRecord::Base class to be supplied' do
|
39
30
|
object = Employee.first(:connection => Connection::One)
|
40
|
-
#assert_connection :shard_one, object
|
41
31
|
end
|
42
32
|
|
43
33
|
should 'allow for a Proc object to be supplied' do
|
44
34
|
proc = lambda { |*args| (args.first % 2) == 1 ? Connection::One : Connection::Two }
|
45
|
-
|
46
|
-
#assert_connection :shard_one, Employee.find(1, :connection => proc)
|
47
|
-
#assert_connection :shard_two, Employee.find(2, :connection => proc)
|
48
|
-
end
|
49
|
-
|
50
|
-
should 'allow for a symbol to be supplied (which calls a method)' do
|
51
|
-
#assert_connection :shard_one, Employee.find(1, :connection => :pick_connection)
|
52
35
|
end
|
53
36
|
|
54
37
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: decisiv-sharded_database
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Brennan Dunn
|
@@ -30,6 +30,7 @@ files:
|
|
30
30
|
- lib/sharded_database/aggregate_proxy.rb
|
31
31
|
- lib/sharded_database/core_extensions.rb
|
32
32
|
- lib/sharded_database/model_with_connection.rb
|
33
|
+
- lib/sharded_database/has_many_association.rb
|
33
34
|
has_rdoc: true
|
34
35
|
homepage: http://github.com/brennandunn/sharded_database/
|
35
36
|
post_install_message:
|