ar-multidb 0.1.13 → 0.3

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
2
  SHA1:
3
- metadata.gz: ebc5c80b4af196924f76bb368ffec424b94d6090
4
- data.tar.gz: 50c67359cdce94f25a6c64f251423281f79e53b7
3
+ metadata.gz: ea08a6b114f52250b4d917e901839dd063dab40b
4
+ data.tar.gz: 6cdde3b0f316fdbb1b68379944fb0eadb91afa25
5
5
  SHA512:
6
- metadata.gz: fe6db0140c93565223db1739c68916ab4334da42ed8b451f75d024456f5aea5194a5885a714b9f29625002e348ed7d60e53f5dba3caaf1e41317194a3a4573aa
7
- data.tar.gz: b304471c77b2cc4ef243f21db533fa09a921ad57c4cc183a97ae108fdd54724ab942bd24d1bbd42ca35c77c0fdcbd9d704c635647ff1260c50d1ebae7b40a817
6
+ metadata.gz: 547ad2beb0fd69f417e0670e404623a8d08a09e66e65bf954a34a34374e51c1db529420d35c686b8fd740afb0cdb2f10724a4538f5f3205b1c3b5af86418afdb
7
+ data.tar.gz: 4e1869077ee855daf7d1fdc95e374d0944e731ca962d97cc8a1546b771f2f20bd065b6b8bfb7904985b1a452fe5e8f94deb0079c4ad0e2b229a90a80fdd63a93
data/.gitignore CHANGED
@@ -3,3 +3,5 @@
3
3
  \.DS_Store
4
4
  *.sqlite
5
5
  Gemfile.lock
6
+ Gemfile.local
7
+ .rspec
@@ -1,3 +1,12 @@
1
- rvm:
2
- - 1.9.3
3
- - 2.0
1
+ sudo: false
2
+ language: ruby
3
+
4
+ matrix:
5
+ fast_finish: true
6
+ include:
7
+ - rvm: 2.2
8
+ gemfile: gemfiles/activerecord40.gemfile
9
+ - rvm: 2.4.3
10
+ gemfile: gemfiles/activerecord42.gemfile
11
+ - rvm: 2.5.0
12
+ gemfile: Gemfile
data/Gemfile CHANGED
@@ -3,7 +3,8 @@ source "http://rubygems.org"
3
3
  # Specify your gem's dependencies in ar-multidb.gemspec
4
4
  gemspec
5
5
 
6
- group :test do
7
- # For travis-ci.org
8
- gem "rake"
6
+ local_gemfile = "Gemfile.local"
7
+
8
+ if File.exist?(local_gemfile)
9
+ eval(File.read(local_gemfile)) # rubocop:disable Security/Eval
9
10
  end
@@ -14,8 +14,8 @@ Randomized balancing of multiple connections within a group is supported. In the
14
14
 
15
15
  ## Requirements
16
16
 
17
- * Ruby 1.9.3 or later.
18
- * ActiveRecord 3.0 or later. (Earlier versions can use the gem version 0.1.10.)
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)
19
19
 
20
20
  ## Comparison to other ActiveRecord extensions
21
21
 
@@ -17,9 +17,10 @@ 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', '>= 3.0'
21
- s.add_runtime_dependency 'activerecord', '>= 3.0'
20
+ s.add_runtime_dependency 'activesupport', '>= 4.0', '<= 6.0'
21
+ s.add_runtime_dependency 'activerecord', '>= 4.0', '<= 6.0'
22
22
 
23
23
  s.add_development_dependency 'rspec'
24
24
  s.add_development_dependency 'sqlite3'
25
+ s.add_development_dependency 'rake'
25
26
  end
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'activerecord', '~> 4.0.0'
4
+
5
+ gemspec path: '..'
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'activerecord', '~> 4.2'
4
+
5
+ gemspec path: '..'
@@ -1,7 +1,9 @@
1
1
  module Multidb
2
2
 
3
3
  class Candidate
4
- def initialize(target)
4
+ def initialize(name, target)
5
+ @name = name
6
+
5
7
  if target.is_a?(Hash)
6
8
  adapter = target[:adapter]
7
9
  begin
@@ -14,8 +16,16 @@ module Multidb
14
16
  else
15
17
  spec_class = ActiveRecord::Base::ConnectionSpecification
16
18
  end
17
- @connection_pool = ActiveRecord::ConnectionAdapters::ConnectionPool.new(
18
- spec_class.new(target, "#{adapter}_connection"))
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)
19
29
  else
20
30
  @connection_pool = target
21
31
  end
@@ -29,36 +39,43 @@ module Multidb
29
39
  end
30
40
  end
31
41
 
32
- attr_reader :connection_pool
42
+ attr_reader :connection_pool, :name
33
43
  end
34
44
 
35
45
  class Balancer
36
46
 
37
47
  def initialize(configuration)
38
48
  @candidates = {}.with_indifferent_access
39
- @configuration = configuration
40
- if @configuration
41
- (@configuration.raw_configuration[:databases] || {}).each_pair do |name, config|
42
- configs = config.is_a?(Array) ? config : [config]
43
- configs.each do |config|
44
- candidate = Candidate.new(@configuration.default_adapter.merge(config))
45
- @candidates[name] ||= []
46
- @candidates[name].push(candidate)
47
- end
48
- end
49
- if @configuration.raw_configuration.include?(:fallback)
50
- @fallback = @configuration.raw_configuration[:fallback]
49
+ @default_configuration = configuration
50
+
51
+ if @default_configuration
52
+
53
+ append(@default_configuration.raw_configuration[:databases] || {})
54
+
55
+ if @default_configuration.raw_configuration.include?(:fallback)
56
+ @fallback = @default_configuration.raw_configuration[:fallback]
51
57
  elsif defined?(Rails)
52
58
  @fallback = %w(development test).include?(Rails.env)
53
59
  else
54
60
  @fallback = false
55
61
  end
56
- @default_candidate = Candidate.new(@configuration.default_pool)
62
+ @default_candidate = Candidate.new('default', @default_configuration.default_pool)
57
63
  unless @candidates.include?(:default)
58
64
  @candidates[:default] = [@default_candidate]
59
65
  end
60
66
  end
61
67
  end
68
+
69
+ def append(databases)
70
+ databases.each_pair do |name, config|
71
+ configs = config.is_a?(Array) ? config : [config]
72
+ configs.each do |config|
73
+ candidate = Candidate.new(name, @default_configuration.default_adapter.merge(config))
74
+ @candidates[name] ||= []
75
+ @candidates[name].push(candidate)
76
+ end
77
+ end
78
+ end
62
79
 
63
80
  def disconnect!
64
81
  @candidates.values.flatten.each do |candidate|
@@ -84,6 +101,7 @@ module Multidb
84
101
  Thread.current[:multidb_connection], connection
85
102
  begin
86
103
  result = yield
104
+ result = result.to_a if result.is_a?(ActiveRecord::Relation)
87
105
  ensure
88
106
  Thread.current[:multidb_connection] = previous_connection
89
107
  end
@@ -118,4 +136,4 @@ module Multidb
118
136
 
119
137
  end
120
138
 
121
- end
139
+ end
@@ -1,26 +1,25 @@
1
1
  require 'active_record/base'
2
2
 
3
3
  module Multidb
4
+ module Connection
5
+ def establish_connection(spec = nil)
6
+ super(spec)
7
+ Multidb.init(connection_pool.spec.config)
8
+ end
9
+
10
+ def connection
11
+ Multidb.balancer.current_connection
12
+ rescue Multidb::NotInitializedError
13
+ super
14
+ end
15
+ end
16
+
4
17
  module ModelExtensions
5
18
  extend ActiveSupport::Concern
6
19
 
7
20
  included do
8
21
  class << self
9
- alias_method_chain :establish_connection, :multidb
10
- alias_method_chain :connection, :multidb
11
- end
12
- end
13
-
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
-
20
- def connection_with_multidb
21
- Multidb.balancer.current_connection
22
- rescue Multidb::NotInitializedError
23
- connection_without_multidb
22
+ prepend Multidb::Connection
24
23
  end
25
24
  end
26
25
  end
@@ -1,3 +1,3 @@
1
1
  module Multidb
2
- VERSION = '0.1.13'
3
- end
2
+ VERSION = '0.3'
3
+ end
@@ -26,6 +26,23 @@ describe 'Multidb.balancer' do
26
26
 
27
27
  Multidb.balancer.current_connection.should eq conn
28
28
  end
29
+
30
+ context 'with additional configurations' do
31
+ before do
32
+ additional_configuration = {slave4: { database: 'spec/test-slave4.sqlite' }}
33
+ Multidb.balancer.append(additional_configuration)
34
+ end
35
+
36
+ it 'makes the new database available' do
37
+ Multidb.use(:slave4) do
38
+ conn = ActiveRecord::Base.connection
39
+ conn.should eq Multidb.balancer.current_connection
40
+ list = conn.execute('pragma database_list')
41
+ list.length.should eq 1
42
+ File.basename(list[0]['file']).should eq 'test-slave4.sqlite'
43
+ end
44
+ end
45
+ end
29
46
  end
30
47
 
31
48
  describe '#use' do
@@ -65,6 +82,16 @@ describe 'Multidb.balancer' do
65
82
  end
66
83
  end
67
84
 
85
+ it 'returns results instead of relation' do
86
+ class FooBar < ActiveRecord::Base; end
87
+ res = Multidb.use(:slave1) do
88
+ ActiveRecord::Migration.verbose = false
89
+ ActiveRecord::Schema.define(version: 1) { create_table :foo_bars }
90
+ FooBar.where(id: 42)
91
+ end
92
+ res.should eq []
93
+ end
94
+
68
95
  it 'returns supports nested slave connection' do
69
96
  Multidb.use(:slave1) do
70
97
  Multidb.use(:slave2) do
@@ -12,6 +12,7 @@ require_relative 'helpers'
12
12
 
13
13
  RSpec.configure do |config|
14
14
  config.include Helpers
15
+ config.expect_with(:rspec) { |c| c.syntax = :should }
15
16
  config.before :each do
16
17
  ActiveRecord::Base.clear_all_connections!
17
18
  Multidb.reset!
metadata CHANGED
@@ -1,69 +1,95 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ar-multidb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.13
4
+ version: '0.3'
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-05-15 00:00:00.000000000 Z
11
+ date: 2018-07-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - '>='
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '3.0'
19
+ version: '4.0'
20
+ - - "<="
21
+ - !ruby/object:Gem::Version
22
+ version: '6.0'
20
23
  type: :runtime
21
24
  prerelease: false
22
25
  version_requirements: !ruby/object:Gem::Requirement
23
26
  requirements:
24
- - - '>='
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: '4.0'
30
+ - - "<="
25
31
  - !ruby/object:Gem::Version
26
- version: '3.0'
32
+ version: '6.0'
27
33
  - !ruby/object:Gem::Dependency
28
34
  name: activerecord
29
35
  requirement: !ruby/object:Gem::Requirement
30
36
  requirements:
31
- - - '>='
37
+ - - ">="
32
38
  - !ruby/object:Gem::Version
33
- version: '3.0'
39
+ version: '4.0'
40
+ - - "<="
41
+ - !ruby/object:Gem::Version
42
+ version: '6.0'
34
43
  type: :runtime
35
44
  prerelease: false
36
45
  version_requirements: !ruby/object:Gem::Requirement
37
46
  requirements:
38
- - - '>='
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: '4.0'
50
+ - - "<="
39
51
  - !ruby/object:Gem::Version
40
- version: '3.0'
52
+ version: '6.0'
41
53
  - !ruby/object:Gem::Dependency
42
54
  name: rspec
43
55
  requirement: !ruby/object:Gem::Requirement
44
56
  requirements:
45
- - - '>='
57
+ - - ">="
46
58
  - !ruby/object:Gem::Version
47
59
  version: '0'
48
60
  type: :development
49
61
  prerelease: false
50
62
  version_requirements: !ruby/object:Gem::Requirement
51
63
  requirements:
52
- - - '>='
64
+ - - ">="
53
65
  - !ruby/object:Gem::Version
54
66
  version: '0'
55
67
  - !ruby/object:Gem::Dependency
56
68
  name: sqlite3
57
69
  requirement: !ruby/object:Gem::Requirement
58
70
  requirements:
59
- - - '>='
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ type: :development
75
+ prerelease: false
76
+ version_requirements: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ - !ruby/object:Gem::Dependency
82
+ name: rake
83
+ requirement: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
60
86
  - !ruby/object:Gem::Version
61
87
  version: '0'
62
88
  type: :development
63
89
  prerelease: false
64
90
  version_requirements: !ruby/object:Gem::Requirement
65
91
  requirements:
66
- - - '>='
92
+ - - ">="
67
93
  - !ruby/object:Gem::Version
68
94
  version: '0'
69
95
  description: Multidb is an ActiveRecord extension for switching between multiple database
@@ -74,13 +100,15 @@ executables: []
74
100
  extensions: []
75
101
  extra_rdoc_files: []
76
102
  files:
77
- - .gitignore
78
- - .travis.yml
103
+ - ".gitignore"
104
+ - ".travis.yml"
79
105
  - Gemfile
80
106
  - LICENSE
81
107
  - README.markdown
82
108
  - Rakefile
83
109
  - ar-multidb.gemspec
110
+ - gemfiles/activerecord40.gemfile
111
+ - gemfiles/activerecord42.gemfile
84
112
  - lib/ar-multidb.rb
85
113
  - lib/multidb.rb
86
114
  - lib/multidb/balancer.rb
@@ -99,17 +127,17 @@ require_paths:
99
127
  - lib
100
128
  required_ruby_version: !ruby/object:Gem::Requirement
101
129
  requirements:
102
- - - '>='
130
+ - - ">="
103
131
  - !ruby/object:Gem::Version
104
132
  version: '0'
105
133
  required_rubygems_version: !ruby/object:Gem::Requirement
106
134
  requirements:
107
- - - '>='
135
+ - - ">="
108
136
  - !ruby/object:Gem::Version
109
137
  version: '0'
110
138
  requirements: []
111
139
  rubyforge_project: ar-multidb
112
- rubygems_version: 2.0.3
140
+ rubygems_version: 2.5.2
113
141
  signing_key:
114
142
  specification_version: 4
115
143
  summary: Multidb is an ActiveRecord extension for switching between multiple database
@@ -118,4 +146,3 @@ test_files:
118
146
  - spec/balancer_spec.rb
119
147
  - spec/helpers.rb
120
148
  - spec/spec_helper.rb
121
- has_rdoc: