decisiv-sharded_database 0.3.1 → 0.3.1.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 +15 -24
- data/lib/sharded_database/model_with_connection.rb +1 -1
- data/lib/sharded_database.rb +1 -1
- data/test/helper.rb +3 -3
- data/test/lib/boot.rb +1 -1
- data/test/lib/models.rb +1 -1
- data/test/lib/test_case.rb +2 -3
- data/test/sharded_database/association_test.rb +4 -8
- data/test/sharded_database/connection_test.rb +8 -1
- metadata +2 -2
@@ -82,34 +82,25 @@ module ShardedDatabase
|
|
82
82
|
end
|
83
83
|
end
|
84
84
|
end
|
85
|
-
|
85
|
+
|
86
86
|
def channel_associations_to_proper_connection
|
87
|
-
self.class.reflect_on_all_associations.each do |
|
88
|
-
metaclass.
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
proxy_#{method}.proxy_reflection.klass.metaclass.delegate :connection, :to => self.source_class
|
100
|
-
proxy_#{method}
|
101
|
-
else
|
102
|
-
# Hacked implementation of belongs_to to superficially simulate an association proxy
|
103
|
-
# Revisit this later and do it properly.
|
104
|
-
|
105
|
-
reflection = self.class.reflect_on_all_associations.find { |a| a.name == :#{method} }
|
106
|
-
klass = reflection.klass
|
107
|
-
klass.metaclass.delegate :connection, :to => self.source_class
|
108
|
-
@#{method} ||= klass.find(send(reflection.primary_key_name))
|
109
|
-
@#{method}
|
87
|
+
self.class.reflect_on_all_associations.each do |association|
|
88
|
+
metaclass.class_eval %{
|
89
|
+
|
90
|
+
def #{association.name}_with_connection(*args)
|
91
|
+
reflection = self.class.reflect_on_association(:#{association.name})
|
92
|
+
klass = reflection.klass
|
93
|
+
ModelWithConnection.borrow_connection(klass, #{@klass.name}) do
|
94
|
+
unless reflection.belongs_to?
|
95
|
+
# TODO
|
96
|
+
klass.send :include, ShardedDatabase::ModelWithConnection
|
97
|
+
end
|
98
|
+
#{association.name}_without_connection(*args)
|
110
99
|
end
|
111
100
|
end
|
101
|
+
|
112
102
|
}, __FILE__, __LINE__
|
103
|
+
metaclass.send :alias_method_chain, association.name, :connection
|
113
104
|
end
|
114
105
|
end
|
115
106
|
|
@@ -29,7 +29,7 @@ module ShardedDatabase
|
|
29
29
|
case connection_arg
|
30
30
|
when Symbol then send(connection_arg, *args)
|
31
31
|
when Proc then connection_arg.call(*args)
|
32
|
-
|
32
|
+
when Class then connection_arg
|
33
33
|
end
|
34
34
|
ModelWithConnection.borrow_connection(self, connection) { find_without_connection(*args) }
|
35
35
|
else
|
data/lib/sharded_database.rb
CHANGED
data/test/helper.rb
CHANGED
@@ -6,9 +6,9 @@ require 'lib/models'
|
|
6
6
|
module ShardedDatabase
|
7
7
|
class TestCase < Test::Unit::TestCase
|
8
8
|
|
9
|
-
self.new_backtrace_silencer(:shoulda) { |line| line.include? 'lib/shoulda' }
|
10
|
-
self.new_backtrace_silencer(:mocha) { |line| line.include? 'lib/mocha' }
|
11
|
-
self.backtrace_silencers << :shoulda << :mocha
|
9
|
+
#self.new_backtrace_silencer(:shoulda) { |line| line.include? 'lib/shoulda' }
|
10
|
+
#self.new_backtrace_silencer(:mocha) { |line| line.include? 'lib/mocha' }
|
11
|
+
#self.backtrace_silencers << :shoulda << :mocha
|
12
12
|
|
13
13
|
|
14
14
|
def assert_connection(configuration, *objects)
|
data/test/lib/boot.rb
CHANGED
@@ -5,7 +5,7 @@ require 'active_record'
|
|
5
5
|
require 'active_support'
|
6
6
|
require 'fileutils'
|
7
7
|
require 'shoulda'
|
8
|
-
require 'quietbacktrace'
|
8
|
+
#require 'quietbacktrace'
|
9
9
|
|
10
10
|
ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__)+'/../debug.log')
|
11
11
|
ActiveRecord::Base.configurations = $config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))
|
data/test/lib/models.rb
CHANGED
data/test/lib/test_case.rb
CHANGED
@@ -34,9 +34,8 @@ module ShardedDatabase
|
|
34
34
|
end
|
35
35
|
|
36
36
|
# Setup sharded DBs for employees
|
37
|
-
|
38
|
-
|
39
|
-
::ActiveRecord::Base.class_eval do
|
37
|
+
[Connection::One, Connection::Two].each do |klass|
|
38
|
+
klass.class_eval do
|
40
39
|
silence do
|
41
40
|
connection.create_table :employees, :force => true do |t|
|
42
41
|
t.belongs_to :company
|
@@ -9,18 +9,14 @@ class AssociationTest < ShardedDatabase::TestCase
|
|
9
9
|
|
10
10
|
context 'Connection delegation on has_many associations' do
|
11
11
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
end
|
16
|
-
|
17
|
-
should 'keep its connection when bubbling up to an associations parent' do
|
18
|
-
assert_equal @parent, @parent.items.first.employee
|
12
|
+
should_eventually 'fetch items from the parent instance connection' do
|
13
|
+
assert_instance_of Array, @parent.items
|
14
|
+
assert_equal '', @parent.items
|
19
15
|
end
|
20
16
|
|
21
17
|
end
|
22
18
|
|
23
|
-
context '
|
19
|
+
context 'Connection delegation on belongs_to associations' do
|
24
20
|
|
25
21
|
should 'fetch the associated company for an employee from the respective connection' do
|
26
22
|
assert_instance_of Company, @parent.company
|
@@ -19,13 +19,20 @@ class ConnectionTest < ShardedDatabase::TestCase
|
|
19
19
|
assert_match %{(Connection::One)}, AggregateEmployee.find_by_source('one').inspect
|
20
20
|
end
|
21
21
|
|
22
|
-
should 'return original connection
|
22
|
+
should 'return original connection after accessing just the target model' do
|
23
23
|
original = Employee.connection.instance_variable_get('@config')[:database]
|
24
24
|
AggregateEmployee.find_by_source('one')
|
25
25
|
final = Employee.connection.instance_variable_get('@config')[:database]
|
26
26
|
assert_equal original, final
|
27
27
|
end
|
28
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]
|
33
|
+
assert_equal original, final
|
34
|
+
end
|
35
|
+
|
29
36
|
context 'loading records when given a :connection key in the options hash' do
|
30
37
|
|
31
38
|
should 'allow for an ActiveRecord::Base class to be supplied' do
|
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.1
|
4
|
+
version: 0.3.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Brennan Dunn
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-
|
12
|
+
date: 2009-02-12 00:00:00 -08:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|