ar-multidb 0.3 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: ea08a6b114f52250b4d917e901839dd063dab40b
4
- data.tar.gz: 6cdde3b0f316fdbb1b68379944fb0eadb91afa25
2
+ SHA256:
3
+ metadata.gz: e055c742a6029415524f28349e3241360013adc1184666609ed854273b0bd0af
4
+ data.tar.gz: dad603f9c611e867dfba0a83f2253cedbcfa5b20b75459d645105d9ed57c8faf
5
5
  SHA512:
6
- metadata.gz: 547ad2beb0fd69f417e0670e404623a8d08a09e66e65bf954a34a34374e51c1db529420d35c686b8fd740afb0cdb2f10724a4538f5f3205b1c3b5af86418afdb
7
- data.tar.gz: 4e1869077ee855daf7d1fdc95e374d0944e731ca962d97cc8a1546b771f2f20bd065b6b8bfb7904985b1a452fe5e8f94deb0079c4ad0e2b229a90a80fdd63a93
6
+ metadata.gz: 80653278f75c95b67e5813c0307b26ff63e5796dd9371228911200ed532711370d7e48e6ec7d841ca59ba820e14843d15a7603099d5f20a451d3f03e3a4c54a9
7
+ data.tar.gz: 11feb31f0e7aed8f9059a91a1da11393df366da7c7d3960a68704ae078dca92e520524a4f23eec7754210a09fac407bb5b449dca1b9dcf449d3b447590f6eb40
@@ -4,9 +4,7 @@ language: ruby
4
4
  matrix:
5
5
  fast_finish: true
6
6
  include:
7
- - rvm: 2.2
8
- gemfile: gemfiles/activerecord40.gemfile
9
- - rvm: 2.4.3
10
- gemfile: gemfiles/activerecord42.gemfile
11
7
  - rvm: 2.5.0
12
- gemfile: Gemfile
8
+ gemfile: gemfiles/activerecord51.gemfile
9
+ - rvm: 2.5.0
10
+ gemfile: gemfiles/activerecord52.gemfile
@@ -15,7 +15,12 @@ Randomized balancing of multiple connections within a group is supported. In the
15
15
  ## Requirements
16
16
 
17
17
  * Ruby 2.2 or later.
18
- * ActiveRecord 4.0 or later. (Earlier versions can use the gem version 0.1.13 for Rails 3.2 and 0.1.10 for older version)
18
+ * ActiveRecord 5.1 or later.
19
+
20
+ ## Older releases
21
+ For ActiveRecord 4. through 5.0 use version 0.3
22
+ For ActiveRecord older than 4.0 use the gem version 0.1.13
23
+ For ActiveRecord older than 3.0 use 0.1.10
19
24
 
20
25
  ## Comparison to other ActiveRecord extensions
21
26
 
@@ -17,8 +17,8 @@ Gem::Specification.new do |s|
17
17
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
18
18
  s.require_paths = ["lib"]
19
19
 
20
- s.add_runtime_dependency 'activesupport', '>= 4.0', '<= 6.0'
21
- s.add_runtime_dependency 'activerecord', '>= 4.0', '<= 6.0'
20
+ s.add_runtime_dependency 'activesupport', '>= 5.1', '<= 6.0'
21
+ s.add_runtime_dependency 'activerecord', '>= 5.1', '<= 6.0'
22
22
 
23
23
  s.add_development_dependency 'rspec'
24
24
  s.add_development_dependency 'sqlite3'
@@ -1,5 +1,5 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
- gem 'activerecord', '~> 4.0.0'
3
+ gem 'activerecord', '~> 5.1.0'
4
4
 
5
5
  gemspec path: '..'
@@ -1,5 +1,5 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
- gem 'activerecord', '~> 4.2'
3
+ gem 'activerecord', '~> 5.2.0'
4
4
 
5
5
  gemspec path: '..'
@@ -6,5 +6,7 @@ require 'active_support/core_ext/module/aliasing'
6
6
 
7
7
  require_relative 'multidb/configuration'
8
8
  require_relative 'multidb/model_extensions'
9
+ require_relative 'multidb/log_subscriber'
10
+ require_relative 'multidb/candidate'
9
11
  require_relative 'multidb/balancer'
10
12
  require_relative 'multidb/version'
@@ -1,48 +1,6 @@
1
1
  module Multidb
2
-
3
- class Candidate
4
- def initialize(name, target)
5
- @name = name
6
-
7
- if target.is_a?(Hash)
8
- adapter = target[:adapter]
9
- begin
10
- require "active_record/connection_adapters/#{adapter}_adapter"
11
- rescue LoadError
12
- raise "Please install the #{adapter} adapter: `gem install activerecord-#{adapter}-adapter` (#{$!})"
13
- end
14
- if defined?(ActiveRecord::ConnectionAdapters::ConnectionSpecification)
15
- spec_class = ActiveRecord::ConnectionAdapters::ConnectionSpecification
16
- else
17
- spec_class = ActiveRecord::Base::ConnectionSpecification
18
- end
19
-
20
- spec =
21
- if ActiveRecord::VERSION::MAJOR >= 5
22
- # ActiveRecord 5.0.1 introduced `name` to initialize
23
- spec_class.new(name, target, "#{adapter}_connection")
24
- else
25
- spec_class.new(target, "#{adapter}_connection")
26
- end
27
-
28
- @connection_pool = ActiveRecord::ConnectionAdapters::ConnectionPool.new(spec)
29
- else
30
- @connection_pool = target
31
- end
32
- end
33
-
34
- def connection(&block)
35
- if block_given?
36
- @connection_pool.with_connection(&block)
37
- else
38
- @connection_pool.connection
39
- end
40
- end
41
-
42
- attr_reader :connection_pool, :name
43
- end
44
-
45
2
  class Balancer
3
+ attr_accessor :fallback
46
4
 
47
5
  def initialize(configuration)
48
6
  @candidates = {}.with_indifferent_access
@@ -59,13 +17,13 @@ module Multidb
59
17
  else
60
18
  @fallback = false
61
19
  end
62
- @default_candidate = Candidate.new('default', @default_configuration.default_pool)
20
+ @default_candidate = Candidate.new('default', @default_configuration.default_handler)
63
21
  unless @candidates.include?(:default)
64
22
  @candidates[:default] = [@default_candidate]
65
23
  end
66
24
  end
67
25
  end
68
-
26
+
69
27
  def append(databases)
70
28
  databases.each_pair do |name, config|
71
29
  configs = config.is_a?(Array) ? config : [config]
@@ -78,9 +36,7 @@ module Multidb
78
36
  end
79
37
 
80
38
  def disconnect!
81
- @candidates.values.flatten.each do |candidate|
82
- candidate.connection_pool.disconnect!
83
- end
39
+ @candidates.values.flatten.each(&:disconnect!)
84
40
  end
85
41
 
86
42
  def get(name, &block)
@@ -97,30 +53,47 @@ module Multidb
97
53
  get(name) do |candidate|
98
54
  if block_given?
99
55
  candidate.connection do |connection|
100
- previous_connection, Thread.current[:multidb_connection] =
101
- Thread.current[:multidb_connection], connection
56
+ previous_configuration = Thread.current[:multidb]
57
+ Thread.current[:multidb] = {
58
+ connection: connection,
59
+ connection_name: name
60
+ }
102
61
  begin
103
62
  result = yield
104
63
  result = result.to_a if result.is_a?(ActiveRecord::Relation)
105
64
  ensure
106
- Thread.current[:multidb_connection] = previous_connection
65
+ Thread.current[:multidb] = previous_configuration
107
66
  end
108
67
  result
109
68
  end
110
69
  else
111
- result = Thread.current[:multidb_connection] = candidate.connection
70
+ Thread.current[:multidb] = {
71
+ connection: candidate.connection,
72
+ connection_name: name
73
+ }
74
+ result = candidate.connection
112
75
  end
113
76
  end
114
77
  result
115
78
  end
116
79
 
117
80
  def current_connection
118
- Thread.current[:multidb_connection] || @default_candidate.connection
81
+ if Thread.current[:multidb]
82
+ Thread.current[:multidb][:connection]
83
+ else
84
+ @default_candidate.connection
85
+ end
119
86
  end
120
87
 
121
- class << self
122
- delegate :use, :current_connection, :disconnect!, to: :balancer
88
+ def current_connection_name
89
+ if Thread.current[:multidb]
90
+ Thread.current[:multidb][:connection_name]
91
+ else
92
+ :default
93
+ end
94
+ end
123
95
 
96
+ class << self
124
97
  def use(name, &block)
125
98
  Multidb.balancer.use(name, &block)
126
99
  end
@@ -129,6 +102,10 @@ module Multidb
129
102
  Multidb.balancer.current_connection
130
103
  end
131
104
 
105
+ def current_connection_name
106
+ Multidb.balancer.current_connection_name
107
+ end
108
+
132
109
  def disconnect!
133
110
  Multidb.balancer.disconnect!
134
111
  end
@@ -0,0 +1,31 @@
1
+ module Multidb
2
+ class Candidate
3
+ def initialize(name, target)
4
+ @name = name
5
+
6
+ if target.is_a?(Hash)
7
+ @connection_handler = ActiveRecord::ConnectionAdapters::ConnectionHandler.new
8
+ @connection_handler.establish_connection(target.merge(name: 'primary'))
9
+ elsif target.is_a?(ActiveRecord::ConnectionAdapters::ConnectionHandler)
10
+ @connection_handler = target
11
+ else
12
+ raise ArgumentError, 'Connection handler not passed to target'
13
+ end
14
+
15
+ end
16
+
17
+ def connection(&block)
18
+ if block_given?
19
+ @connection_handler.retrieve_connection_pool('primary').with_connection(&block)
20
+ else
21
+ @connection_handler.retrieve_connection('primary')
22
+ end
23
+ end
24
+
25
+ def disconnect!
26
+ @connection_handler.clear_all_connections!
27
+ end
28
+
29
+ attr_reader :name
30
+ end
31
+ end
@@ -14,7 +14,7 @@ module Multidb
14
14
  if @balancer
15
15
  @balancer
16
16
  else
17
- raise NotInitializedError, "Balancer not initialized. You need to run Multidb.setup first"
17
+ raise NotInitializedError, "Balancer not initialized. You need to run Multidb.init first"
18
18
  end
19
19
  end
20
20
 
@@ -22,16 +22,16 @@ module Multidb
22
22
  @balancer = nil
23
23
  end
24
24
 
25
- class NotInitializedError < StandardError; end;
25
+ class NotInitializedError < StandardError; end
26
26
 
27
27
  class Configuration
28
28
  def initialize(default_adapter, configuration_hash)
29
- @default_pool = ActiveRecord::Base.connection_pool
29
+ @default_handler = ActiveRecord::Base.connection_handler
30
30
  @default_adapter = default_adapter
31
31
  @raw_configuration = configuration_hash
32
32
  end
33
33
 
34
- attr_reader :default_pool
34
+ attr_reader :default_handler
35
35
  attr_reader :default_adapter
36
36
  attr_reader :raw_configuration
37
37
  end
@@ -0,0 +1,21 @@
1
+ module Multidb
2
+ module LogSubscriberExtension
3
+ def sql(event)
4
+ if name = Multidb.balancer.current_connection_name
5
+ event.payload[:db_name] = name
6
+ end
7
+ super
8
+ end
9
+
10
+ def debug(msg)
11
+ if name = Multidb.balancer.current_connection_name
12
+ db = color("[DB: #{name}]", ActiveSupport::LogSubscriber::GREEN, true)
13
+ super(db + msg)
14
+ else
15
+ super
16
+ end
17
+ end
18
+ end
19
+ end
20
+
21
+ ActiveRecord::LogSubscriber.prepend(Multidb::LogSubscriberExtension)
@@ -1,3 +1,3 @@
1
1
  module Multidb
2
- VERSION = '0.3'
2
+ VERSION = '0.4.0'
3
3
  end
@@ -26,6 +26,12 @@ describe 'Multidb.balancer' do
26
26
 
27
27
  Multidb.balancer.current_connection.should eq conn
28
28
  end
29
+
30
+ it 'returns default connection name for default connection' do
31
+ conn = ActiveRecord::Base.connection
32
+
33
+ Multidb.balancer.current_connection_name.should eq :default
34
+ end
29
35
 
30
36
  context 'with additional configurations' do
31
37
  before do
@@ -42,6 +48,12 @@ describe 'Multidb.balancer' do
42
48
  File.basename(list[0]['file']).should eq 'test-slave4.sqlite'
43
49
  end
44
50
  end
51
+
52
+ it 'returns the connection name' do
53
+ Multidb.use(:slave4) do
54
+ Multidb.balancer.current_connection_name.should eq :slave4
55
+ end
56
+ end
45
57
  end
46
58
  end
47
59
 
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.3'
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexander Staubo
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-07-31 00:00:00.000000000 Z
11
+ date: 2019-01-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -16,7 +16,7 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '4.0'
19
+ version: '5.1'
20
20
  - - "<="
21
21
  - !ruby/object:Gem::Version
22
22
  version: '6.0'
@@ -26,7 +26,7 @@ dependencies:
26
26
  requirements:
27
27
  - - ">="
28
28
  - !ruby/object:Gem::Version
29
- version: '4.0'
29
+ version: '5.1'
30
30
  - - "<="
31
31
  - !ruby/object:Gem::Version
32
32
  version: '6.0'
@@ -36,7 +36,7 @@ dependencies:
36
36
  requirements:
37
37
  - - ">="
38
38
  - !ruby/object:Gem::Version
39
- version: '4.0'
39
+ version: '5.1'
40
40
  - - "<="
41
41
  - !ruby/object:Gem::Version
42
42
  version: '6.0'
@@ -46,7 +46,7 @@ dependencies:
46
46
  requirements:
47
47
  - - ">="
48
48
  - !ruby/object:Gem::Version
49
- version: '4.0'
49
+ version: '5.1'
50
50
  - - "<="
51
51
  - !ruby/object:Gem::Version
52
52
  version: '6.0'
@@ -107,12 +107,14 @@ files:
107
107
  - README.markdown
108
108
  - Rakefile
109
109
  - ar-multidb.gemspec
110
- - gemfiles/activerecord40.gemfile
111
- - gemfiles/activerecord42.gemfile
110
+ - gemfiles/activerecord51.gemfile
111
+ - gemfiles/activerecord52.gemfile
112
112
  - lib/ar-multidb.rb
113
113
  - lib/multidb.rb
114
114
  - lib/multidb/balancer.rb
115
+ - lib/multidb/candidate.rb
115
116
  - lib/multidb/configuration.rb
117
+ - lib/multidb/log_subscriber.rb
116
118
  - lib/multidb/model_extensions.rb
117
119
  - lib/multidb/version.rb
118
120
  - spec/balancer_spec.rb
@@ -137,7 +139,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
137
139
  version: '0'
138
140
  requirements: []
139
141
  rubyforge_project: ar-multidb
140
- rubygems_version: 2.5.2
142
+ rubygems_version: 2.7.8
141
143
  signing_key:
142
144
  specification_version: 4
143
145
  summary: Multidb is an ActiveRecord extension for switching between multiple database