ar-multidb 0.1.12 → 0.1.13
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +2 -1
- data/lib/multidb/balancer.rb +1 -0
- data/lib/multidb/configuration.rb +15 -31
- data/lib/multidb/model_extensions.rb +11 -7
- data/lib/multidb/version.rb +1 -1
- data/spec/balancer_spec.rb +11 -11
- metadata +2 -3
- data/Gemfile.lock +0 -54
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ebc5c80b4af196924f76bb368ffec424b94d6090
|
4
|
+
data.tar.gz: 50c67359cdce94f25a6c64f251423281f79e53b7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fe6db0140c93565223db1739c68916ab4334da42ed8b451f75d024456f5aea5194a5885a714b9f29625002e348ed7d60e53f5dba3caaf1e41317194a3a4573aa
|
7
|
+
data.tar.gz: b304471c77b2cc4ef243f21db533fa09a921ad57c4cc183a97ae108fdd54724ab942bd24d1bbd42ca35c77c0fdcbd9d704c635647ff1260c50d1ebae7b40a817
|
data/.gitignore
CHANGED
data/lib/multidb/balancer.rb
CHANGED
@@ -1,19 +1,29 @@
|
|
1
1
|
module Multidb
|
2
|
-
|
3
|
-
mattr_reader :configuration
|
4
|
-
|
5
2
|
class << self
|
6
3
|
delegate :use, :get, :disconnect!, to: :balancer
|
7
4
|
end
|
8
5
|
|
6
|
+
def self.init(config)
|
7
|
+
activerecord_config = config.dup.with_indifferent_access
|
8
|
+
default_adapter, configuration_hash = activerecord_config, activerecord_config.delete(:multidb)
|
9
|
+
|
10
|
+
@balancer = Balancer.new(Configuration.new(default_adapter, configuration_hash || {}))
|
11
|
+
end
|
12
|
+
|
9
13
|
def self.balancer
|
10
|
-
@balancer
|
14
|
+
if @balancer
|
15
|
+
@balancer
|
16
|
+
else
|
17
|
+
raise NotInitializedError, "Balancer not initialized. You need to run Multidb.setup first"
|
18
|
+
end
|
11
19
|
end
|
12
20
|
|
13
21
|
def self.reset!
|
14
|
-
@balancer
|
22
|
+
@balancer = nil
|
15
23
|
end
|
16
24
|
|
25
|
+
class NotInitializedError < StandardError; end;
|
26
|
+
|
17
27
|
class Configuration
|
18
28
|
def initialize(default_adapter, configuration_hash)
|
19
29
|
@default_pool = ActiveRecord::Base.connection_pool
|
@@ -25,30 +35,4 @@ module Multidb
|
|
25
35
|
attr_reader :default_adapter
|
26
36
|
attr_reader :raw_configuration
|
27
37
|
end
|
28
|
-
|
29
|
-
private
|
30
|
-
|
31
|
-
def self.create_balancer
|
32
|
-
unless @configuration
|
33
|
-
begin
|
34
|
-
connection_pool = ActiveRecord::Base.connection_pool
|
35
|
-
rescue ActiveRecord::ConnectionNotEstablished
|
36
|
-
# Ignore
|
37
|
-
else
|
38
|
-
connection = connection_pool.connection
|
39
|
-
|
40
|
-
# FIXME: This is hacky, but apparently the only way to get at
|
41
|
-
# the internal configuration hash.
|
42
|
-
activerecord_config = connection.instance_variable_get(:@config).dup.with_indifferent_access
|
43
|
-
|
44
|
-
default_adapter, configuration_hash = activerecord_config, activerecord_config.delete(:multidb)
|
45
|
-
|
46
|
-
@configuration = Configuration.new(default_adapter, configuration_hash || {})
|
47
|
-
end
|
48
|
-
end
|
49
|
-
if @configuration
|
50
|
-
Balancer.new(@configuration)
|
51
|
-
end
|
52
|
-
end
|
53
|
-
|
54
38
|
end
|
@@ -1,24 +1,28 @@
|
|
1
|
+
require 'active_record/base'
|
2
|
+
|
1
3
|
module Multidb
|
2
4
|
module ModelExtensions
|
3
|
-
|
4
5
|
extend ActiveSupport::Concern
|
5
6
|
|
6
7
|
included do
|
7
8
|
class << self
|
9
|
+
alias_method_chain :establish_connection, :multidb
|
8
10
|
alias_method_chain :connection, :multidb
|
9
11
|
end
|
10
12
|
end
|
11
13
|
|
12
14
|
module ClassMethods
|
15
|
+
def establish_connection_with_multidb(spec = nil)
|
16
|
+
establish_connection_without_multidb(spec)
|
17
|
+
Multidb.init(connection_pool.spec.config)
|
18
|
+
end
|
19
|
+
|
13
20
|
def connection_with_multidb
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
connection_without_multidb
|
18
|
-
end
|
21
|
+
Multidb.balancer.current_connection
|
22
|
+
rescue Multidb::NotInitializedError
|
23
|
+
connection_without_multidb
|
19
24
|
end
|
20
25
|
end
|
21
|
-
|
22
26
|
end
|
23
27
|
end
|
24
28
|
|
data/lib/multidb/version.rb
CHANGED
data/spec/balancer_spec.rb
CHANGED
@@ -3,8 +3,8 @@ require_relative 'spec_helper'
|
|
3
3
|
describe 'Multidb.balancer' do
|
4
4
|
|
5
5
|
context 'with no configuration' do
|
6
|
-
it '
|
7
|
-
Multidb.balancer.should
|
6
|
+
it 'raises exception' do
|
7
|
+
-> { Multidb.balancer }.should raise_error(Multidb::NotInitializedError)
|
8
8
|
end
|
9
9
|
end
|
10
10
|
|
@@ -29,20 +29,20 @@ describe 'Multidb.balancer' do
|
|
29
29
|
end
|
30
30
|
|
31
31
|
describe '#use' do
|
32
|
-
context 'with no configuration' do
|
33
|
-
it 'raises exception' do
|
34
|
-
-> {
|
35
|
-
Multidb.use(:something) do
|
36
|
-
end
|
37
|
-
}.should raise_error(ArgumentError)
|
38
|
-
end
|
39
|
-
end
|
40
|
-
|
41
32
|
context 'with configuration' do
|
42
33
|
before do
|
43
34
|
ActiveRecord::Base.establish_connection(configuration_with_slaves)
|
44
35
|
end
|
45
36
|
|
37
|
+
context 'undefined connection' do
|
38
|
+
it 'raises exception' do
|
39
|
+
-> {
|
40
|
+
Multidb.use(:something) do
|
41
|
+
end
|
42
|
+
}.should raise_error(ArgumentError)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
46
|
it 'returns default connection on :default' do
|
47
47
|
conn = ActiveRecord::Base.connection
|
48
48
|
Multidb.use(:default) do
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ar-multidb
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.13
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alexander Staubo
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-05-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -77,7 +77,6 @@ files:
|
|
77
77
|
- .gitignore
|
78
78
|
- .travis.yml
|
79
79
|
- Gemfile
|
80
|
-
- Gemfile.lock
|
81
80
|
- LICENSE
|
82
81
|
- README.markdown
|
83
82
|
- Rakefile
|
data/Gemfile.lock
DELETED
@@ -1,54 +0,0 @@
|
|
1
|
-
PATH
|
2
|
-
remote: .
|
3
|
-
specs:
|
4
|
-
ar-multidb (0.1.11)
|
5
|
-
activerecord (>= 3.0)
|
6
|
-
activesupport (>= 3.0)
|
7
|
-
|
8
|
-
GEM
|
9
|
-
remote: http://rubygems.org/
|
10
|
-
specs:
|
11
|
-
activemodel (4.0.2)
|
12
|
-
activesupport (= 4.0.2)
|
13
|
-
builder (~> 3.1.0)
|
14
|
-
activerecord (4.0.2)
|
15
|
-
activemodel (= 4.0.2)
|
16
|
-
activerecord-deprecated_finders (~> 1.0.2)
|
17
|
-
activesupport (= 4.0.2)
|
18
|
-
arel (~> 4.0.0)
|
19
|
-
activerecord-deprecated_finders (1.0.3)
|
20
|
-
activesupport (4.0.2)
|
21
|
-
i18n (~> 0.6, >= 0.6.4)
|
22
|
-
minitest (~> 4.2)
|
23
|
-
multi_json (~> 1.3)
|
24
|
-
thread_safe (~> 0.1)
|
25
|
-
tzinfo (~> 0.3.37)
|
26
|
-
arel (4.0.1)
|
27
|
-
atomic (1.1.14)
|
28
|
-
builder (3.1.4)
|
29
|
-
diff-lcs (1.2.5)
|
30
|
-
i18n (0.6.9)
|
31
|
-
minitest (4.7.5)
|
32
|
-
multi_json (1.8.4)
|
33
|
-
rake (10.1.1)
|
34
|
-
rspec (2.14.1)
|
35
|
-
rspec-core (~> 2.14.0)
|
36
|
-
rspec-expectations (~> 2.14.0)
|
37
|
-
rspec-mocks (~> 2.14.0)
|
38
|
-
rspec-core (2.14.7)
|
39
|
-
rspec-expectations (2.14.5)
|
40
|
-
diff-lcs (>= 1.1.3, < 2.0)
|
41
|
-
rspec-mocks (2.14.5)
|
42
|
-
sqlite3 (1.3.8)
|
43
|
-
thread_safe (0.1.3)
|
44
|
-
atomic
|
45
|
-
tzinfo (0.3.38)
|
46
|
-
|
47
|
-
PLATFORMS
|
48
|
-
ruby
|
49
|
-
|
50
|
-
DEPENDENCIES
|
51
|
-
ar-multidb!
|
52
|
-
rake
|
53
|
-
rspec
|
54
|
-
sqlite3
|