ar-multidb 0.5.1 → 0.7.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/prs.yml +71 -0
- data/.gitignore +1 -0
- data/.rubocop.yml +149 -5
- data/.simplecov +22 -0
- data/CHANGELOG.md +9 -1
- data/Gemfile +2 -5
- data/README.markdown +4 -2
- data/ar-multidb.gemspec +11 -7
- data/gemfiles/activerecord-6.1.gemfile +7 -0
- data/gemfiles/activerecord-7.0.gemfile +7 -0
- data/gemfiles/activerecord-7.1.gemfile +7 -0
- data/lib/multidb/balancer.rb +2 -2
- data/lib/multidb/candidate.rb +12 -3
- data/lib/multidb/configuration.rb +0 -22
- data/lib/multidb/log_subscriber.rb +2 -2
- data/lib/multidb/model_extensions.rb +7 -1
- data/lib/multidb/version.rb +1 -1
- data/lib/multidb.rb +26 -0
- data/spec/lib/multidb/balancer_spec.rb +393 -0
- data/spec/lib/multidb/candidate_spec.rb +105 -0
- data/spec/lib/multidb/configuration_spec.rb +23 -0
- data/spec/lib/multidb/log_subscriber_extension_spec.rb +89 -0
- data/spec/lib/multidb/model_extensions_spec.rb +61 -0
- data/spec/lib/multidb_spec.rb +91 -0
- data/spec/spec_helper.rb +30 -5
- data/spec/support/have_database_matcher.rb +15 -0
- data/spec/{helpers.rb → support/helpers.rb} +4 -1
- metadata +80 -21
- data/.travis.yml +0 -17
- data/spec/balancer_spec.rb +0 -161
- /data/gemfiles/{activerecord51.gemfile → activerecord-5.1.gemfile} +0 -0
- /data/gemfiles/{activerecord52.gemfile → activerecord-5.2.gemfile} +0 -0
- /data/gemfiles/{activerecord60.gemfile → activerecord-6.0.gemfile} +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 43b7a06c5b9e515743e31eabe777ed7f3d56374ac1861b505266636fb30fb2a6
|
4
|
+
data.tar.gz: cfdbffebdbd4c940c08fb0260abfe3f5c25700be5a9cf562f4ad7dc370b93aee
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cb45e7c3f610828ad5293ec478b5b0cfda8b17952d0f2a7772b3128e7c2515f396546a1f7a3014864ed489fa1a0da1e721c03cf50297c498051e0a7b3060218b
|
7
|
+
data.tar.gz: 68cce9fef6f957b8212c41bddaeca6fe0711a614cd84d8cb38e7419a572929e9e5119344b4602d6efc53deafed42fa0c9acda6122e16149ee68e3e1af4d0823c
|
@@ -0,0 +1,71 @@
|
|
1
|
+
---
|
2
|
+
name: CI PR Builds
|
3
|
+
'on':
|
4
|
+
push:
|
5
|
+
branches:
|
6
|
+
- master
|
7
|
+
pull_request:
|
8
|
+
concurrency:
|
9
|
+
group: ci-${{ github.ref }}
|
10
|
+
cancel-in-progress: true
|
11
|
+
jobs:
|
12
|
+
test:
|
13
|
+
name: CI Build
|
14
|
+
runs-on: ubuntu-latest
|
15
|
+
strategy:
|
16
|
+
fail-fast: false
|
17
|
+
matrix:
|
18
|
+
ruby:
|
19
|
+
- '2.5'
|
20
|
+
- '2.7'
|
21
|
+
- '3.0'
|
22
|
+
activerecord:
|
23
|
+
- '5.1'
|
24
|
+
- '5.2'
|
25
|
+
- '6.0'
|
26
|
+
- '6.1'
|
27
|
+
- '7.0'
|
28
|
+
- '7.1'
|
29
|
+
exclude:
|
30
|
+
- ruby: '2.5'
|
31
|
+
activerecord: '7.0'
|
32
|
+
- ruby: '2.5'
|
33
|
+
activerecord: '7.1'
|
34
|
+
- ruby: '3.0'
|
35
|
+
activerecord: '5.1'
|
36
|
+
- ruby: '3.0'
|
37
|
+
activerecord: '5.2'
|
38
|
+
env:
|
39
|
+
BUNDLE_GEMFILE: "${{ github.workspace }}/gemfiles/activerecord-${{ matrix.activerecord }}.gemfile"
|
40
|
+
steps:
|
41
|
+
- uses: actions/checkout@v4
|
42
|
+
- name: Set up Ruby
|
43
|
+
uses: ruby/setup-ruby@v1
|
44
|
+
with:
|
45
|
+
ruby-version: "${{ matrix.ruby }}"
|
46
|
+
bundler-cache: true
|
47
|
+
- name: Run bundle update
|
48
|
+
run: bundle update
|
49
|
+
- name: Run tests
|
50
|
+
run: bundle exec rspec
|
51
|
+
- name: Run rubocop
|
52
|
+
run: bundle exec rubocop
|
53
|
+
- name: Coveralls Parallel
|
54
|
+
if: "${{ !env.ACT }}"
|
55
|
+
uses: coverallsapp/github-action@v2
|
56
|
+
with:
|
57
|
+
github-token: "${{ secrets.GITHUB_TOKEN }}"
|
58
|
+
flag-name: run-${{ matrix.ruby }}-${{ matrix.activerecord }}
|
59
|
+
parallel: true
|
60
|
+
finish:
|
61
|
+
name: All CI Tests Passed
|
62
|
+
needs:
|
63
|
+
- test
|
64
|
+
runs-on: ubuntu-latest
|
65
|
+
steps:
|
66
|
+
- name: Coveralls Finished
|
67
|
+
if: "${{ !env.ACT }}"
|
68
|
+
uses: coverallsapp/github-action@v2
|
69
|
+
with:
|
70
|
+
github-token: "${{ secrets.GITHUB_TOKEN }}"
|
71
|
+
parallel-finished: true
|
data/.gitignore
CHANGED
data/.rubocop.yml
CHANGED
@@ -1,64 +1,208 @@
|
|
1
|
+
require:
|
2
|
+
- rubocop-rspec
|
3
|
+
|
1
4
|
AllCops:
|
2
|
-
TargetRubyVersion: 2.
|
5
|
+
TargetRubyVersion: 2.5
|
3
6
|
SuggestExtensions: false
|
4
7
|
NewCops: disable
|
5
8
|
Exclude:
|
6
9
|
- '**/vendor/**/*'
|
7
10
|
|
11
|
+
Gemspec/DateAssignment: # new in 1.10
|
12
|
+
Enabled: true
|
13
|
+
Gemspec/RequireMFA: # new in 1.23
|
14
|
+
Enabled: true
|
15
|
+
|
8
16
|
Layout/EmptyLinesAroundAttributeAccessor:
|
9
17
|
Enabled: true
|
10
|
-
Layout/
|
18
|
+
Layout/LineEndStringConcatenationIndentation: # new in 1.18
|
11
19
|
Enabled: true
|
12
20
|
Layout/LineLength:
|
13
21
|
Exclude:
|
14
|
-
-
|
22
|
+
- ar-multidb.gemspec
|
23
|
+
Layout/SpaceAroundMethodCallOperator:
|
24
|
+
Enabled: true
|
25
|
+
Layout/SpaceBeforeBrackets: # new in 1.7
|
26
|
+
Enabled: true
|
27
|
+
|
28
|
+
Lint/AmbiguousAssignment: # new in 1.7
|
29
|
+
Enabled: true
|
30
|
+
Lint/AmbiguousBlockAssociation:
|
31
|
+
IgnoredMethods:
|
32
|
+
- change
|
33
|
+
Lint/AmbiguousOperatorPrecedence: # new in 1.21
|
34
|
+
Enabled: true
|
35
|
+
Lint/AmbiguousRange: # new in 1.19
|
36
|
+
Enabled: true
|
37
|
+
Lint/DeprecatedConstants: # new in 1.8
|
38
|
+
Enabled: true
|
15
39
|
Lint/DeprecatedOpenSSLConstant:
|
16
40
|
Enabled: true
|
41
|
+
Lint/DuplicateBranch: # new in 1.3
|
42
|
+
Enabled: true
|
17
43
|
Lint/DuplicateElsifCondition:
|
18
44
|
Enabled: true
|
45
|
+
Lint/DuplicateRegexpCharacterClassElement: # new in 1.1
|
46
|
+
Enabled: true
|
47
|
+
Lint/EmptyBlock: # new in 1.1
|
48
|
+
Enabled: true
|
49
|
+
Lint/EmptyClass: # new in 1.3
|
50
|
+
Enabled: true
|
51
|
+
Lint/EmptyInPattern: # new in 1.16
|
52
|
+
Enabled: true
|
53
|
+
Lint/IncompatibleIoSelectWithFiberScheduler: # new in 1.21
|
54
|
+
Enabled: true
|
55
|
+
Lint/LambdaWithoutLiteralBlock: # new in 1.8
|
56
|
+
Enabled: true
|
19
57
|
Lint/MixedRegexpCaptureTypes:
|
20
58
|
Enabled: true
|
59
|
+
Lint/NoReturnInBeginEndBlocks: # new in 1.2
|
60
|
+
Enabled: true
|
61
|
+
Lint/NumberedParameterAssignment: # new in 1.9
|
62
|
+
Enabled: true
|
63
|
+
Lint/OrAssignmentToConstant: # new in 1.9
|
64
|
+
Enabled: true
|
21
65
|
Lint/RaiseException:
|
22
66
|
Enabled: true
|
67
|
+
Lint/RedundantDirGlobSort: # new in 1.8
|
68
|
+
Enabled: true
|
69
|
+
Lint/RefinementImportMethods: # new in 1.27
|
70
|
+
Enabled: true
|
71
|
+
Lint/RequireRelativeSelfPath: # new in 1.22
|
72
|
+
Enabled: true
|
23
73
|
Lint/StructNewOverride:
|
24
74
|
Enabled: true
|
75
|
+
Lint/SymbolConversion: # new in 1.9
|
76
|
+
Enabled: true
|
77
|
+
Lint/ToEnumArguments: # new in 1.1
|
78
|
+
Enabled: true
|
79
|
+
Lint/TripleQuotes: # new in 1.9
|
80
|
+
Enabled: true
|
81
|
+
Lint/UnexpectedBlockArity: # new in 1.5
|
82
|
+
Enabled: true
|
83
|
+
Lint/UnmodifiedReduceAccumulator: # new in 1.1
|
84
|
+
Enabled: true
|
85
|
+
Lint/UselessRuby2Keywords: # new in 1.23
|
86
|
+
Enabled: true
|
87
|
+
|
25
88
|
Naming/FileName:
|
26
89
|
Exclude:
|
27
|
-
-
|
90
|
+
- lib/ar-multidb.rb
|
91
|
+
- 'gemfiles/*'
|
92
|
+
Naming/BlockForwarding: # new in 1.24
|
93
|
+
Enabled: true
|
94
|
+
|
28
95
|
Metrics:
|
29
96
|
Enabled: false
|
97
|
+
|
98
|
+
RSpec/ExampleLength:
|
99
|
+
CountAsOne: [array, heredoc]
|
100
|
+
Max: 9
|
101
|
+
RSpec/ExpectChange:
|
102
|
+
EnforcedStyle: block
|
103
|
+
RSpec/ImplicitSubject:
|
104
|
+
Enabled: false
|
105
|
+
RSpec/NamedSubject:
|
106
|
+
Enabled: false
|
107
|
+
RSpec/MultipleMemoizedHelpers:
|
108
|
+
Enabled: false
|
109
|
+
RSpec/MultipleExpectations:
|
110
|
+
Enabled: false
|
111
|
+
RSpec/NestedGroups:
|
112
|
+
Max: 4
|
113
|
+
|
114
|
+
Security/CompoundHash: # new in 1.28
|
115
|
+
Enabled: true
|
116
|
+
Security/IoMethods: # new in 1.22
|
117
|
+
Enabled: true
|
118
|
+
|
30
119
|
Style/AccessorGrouping:
|
31
120
|
Enabled: true
|
121
|
+
Style/ArgumentsForwarding: # new in 1.1
|
122
|
+
Enabled: true
|
32
123
|
Style/ArrayCoercion:
|
33
124
|
Enabled: true
|
34
125
|
Style/BisectedAttrAccessor:
|
35
126
|
Enabled: true
|
127
|
+
Style/BlockDelimiters:
|
128
|
+
Enabled: false
|
36
129
|
Style/CaseLikeIf:
|
37
130
|
Enabled: true
|
131
|
+
Style/CollectionCompact: # new in 1.2
|
132
|
+
Enabled: true
|
38
133
|
Style/Documentation:
|
39
134
|
Enabled: false
|
135
|
+
Style/DocumentDynamicEvalDefinition: # new in 1.1
|
136
|
+
Enabled: true
|
137
|
+
Style/EndlessMethod: # new in 1.8
|
138
|
+
Enabled: true
|
40
139
|
Style/ExponentialNotation:
|
41
140
|
Enabled: true
|
141
|
+
Style/FetchEnvVar: # new in 1.28
|
142
|
+
Enabled: true
|
143
|
+
Style/FileRead: # new in 1.24
|
144
|
+
Enabled: true
|
145
|
+
Style/FileWrite: # new in 1.24
|
146
|
+
Enabled: true
|
42
147
|
Style/HashAsLastArrayItem:
|
43
148
|
Enabled: true
|
149
|
+
Style/HashConversion: # new in 1.10
|
150
|
+
Enabled: true
|
44
151
|
Style/HashEachMethods:
|
45
152
|
Enabled: true
|
153
|
+
Style/HashExcept: # new in 1.7
|
154
|
+
Enabled: true
|
46
155
|
Style/HashLikeCase:
|
47
156
|
Enabled: true
|
48
157
|
Style/HashTransformKeys:
|
49
158
|
Enabled: true
|
50
159
|
Style/HashTransformValues:
|
51
160
|
Enabled: true
|
161
|
+
Style/IfWithBooleanLiteralBranches: # new in 1.9
|
162
|
+
Enabled: true
|
163
|
+
Style/InPatternThen: # new in 1.16
|
164
|
+
Enabled: true
|
165
|
+
Style/MapToHash: # new in 1.24
|
166
|
+
Enabled: true
|
167
|
+
Style/MultilineInPatternThen: # new in 1.16
|
168
|
+
Enabled: true
|
169
|
+
Style/NegatedIfElseCondition: # new in 1.2
|
170
|
+
Enabled: true
|
171
|
+
Style/NestedFileDirname: # new in 1.26
|
172
|
+
Enabled: true
|
173
|
+
Style/NilLambda: # new in 1.3
|
174
|
+
Enabled: true
|
175
|
+
Style/NumberedParameters: # new in 1.22
|
176
|
+
Enabled: true
|
177
|
+
Style/NumberedParametersLimit: # new in 1.22
|
178
|
+
Enabled: true
|
179
|
+
Style/ObjectThen: # new in 1.28
|
180
|
+
Enabled: true
|
181
|
+
Style/OpenStructUse: # new in 1.23
|
182
|
+
Enabled: true
|
183
|
+
Style/QuotedSymbols: # new in 1.16
|
184
|
+
Enabled: true
|
185
|
+
Style/RedundantArgument: # new in 1.4
|
186
|
+
Enabled: true
|
52
187
|
Style/RedundantAssignment:
|
53
188
|
Enabled: true
|
54
189
|
Style/RedundantFetchBlock:
|
55
190
|
Enabled: true
|
56
191
|
Style/RedundantFileExtensionInRequire:
|
57
192
|
Enabled: true
|
193
|
+
Style/RedundantInitialize: # new in 1.27
|
194
|
+
Enabled: true
|
58
195
|
Style/RedundantRegexpCharacterClass:
|
59
196
|
Enabled: true
|
60
197
|
Style/RedundantRegexpEscape:
|
61
198
|
Enabled: true
|
199
|
+
Style/RedundantSelfAssignmentBranch: # new in 1.19
|
200
|
+
Enabled: true
|
201
|
+
Style/SelectByRegexp: # new in 1.22
|
202
|
+
Enabled: true
|
62
203
|
Style/SlicingWithRange:
|
63
204
|
Enabled: true
|
64
|
-
|
205
|
+
Style/StringChars: # new in 1.12
|
206
|
+
Enabled: true
|
207
|
+
Style/SwapValues: # new in 1.1
|
208
|
+
Enabled: true
|
data/.simplecov
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
SimpleCov.configure do
|
4
|
+
enable_coverage :branch
|
5
|
+
add_filter '/spec/'
|
6
|
+
add_filter 'lib/multidb/version.rb'
|
7
|
+
add_filter 'lib/ar-multidb.rb'
|
8
|
+
|
9
|
+
add_group 'Binaries', '/bin/'
|
10
|
+
add_group 'Libraries', '/lib/'
|
11
|
+
|
12
|
+
if ENV['CI']
|
13
|
+
require 'simplecov-lcov'
|
14
|
+
|
15
|
+
SimpleCov::Formatter::LcovFormatter.config do |c|
|
16
|
+
c.report_with_single_file = true
|
17
|
+
c.single_report_path = 'coverage/lcov.info'
|
18
|
+
end
|
19
|
+
|
20
|
+
formatter SimpleCov::Formatter::LcovFormatter
|
21
|
+
end
|
22
|
+
end
|
data/CHANGELOG.md
CHANGED
@@ -4,6 +4,15 @@ All notable changes to this project will be documented in this file.
|
|
4
4
|
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
|
5
5
|
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html)
|
6
6
|
|
7
|
+
## [0.7.0]
|
8
|
+
### Changed
|
9
|
+
- add AR 7.1 support
|
10
|
+
|
11
|
+
## [0.6.0]
|
12
|
+
### Changed
|
13
|
+
- dropped ruby 2.4 support
|
14
|
+
- add AR 6.1 and 7.0 support
|
15
|
+
|
7
16
|
## [0.5.1]
|
8
17
|
### Fixed
|
9
18
|
- corrected licenses
|
@@ -23,4 +32,3 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
|
|
23
32
|
## [0.4.1]
|
24
33
|
### Added
|
25
34
|
- Added support for database aliases ( PR #26 )
|
26
|
-
|
data/Gemfile
CHANGED
@@ -5,8 +5,5 @@ source 'http://rubygems.org'
|
|
5
5
|
# Specify your gem's dependencies in ar-multidb.gemspec
|
6
6
|
gemspec
|
7
7
|
|
8
|
-
local_gemfile = 'Gemfile.local'
|
9
|
-
|
10
|
-
if File.exist?(local_gemfile)
|
11
|
-
eval(File.read(local_gemfile)) # rubocop:disable Security/Eval
|
12
|
-
end
|
8
|
+
local_gemfile = File.expand_path('Gemfile.local', __dir__)
|
9
|
+
eval(File.read(local_gemfile), binding, local_gemfile) if File.exist?(local_gemfile) # rubocop:disable Security/Eval
|
data/README.markdown
CHANGED
@@ -1,4 +1,5 @@
|
|
1
|
-
[![Build Status](https://
|
1
|
+
[![Build Status](https://github.com/OutOfOrder/multidb/actions/workflows/prs.yml/badge.svg)](https://github.com/OutOfOrder/multidb/actions)
|
2
|
+
[![Coverage Status](https://coveralls.io/repos/github/OutOfOrder/multidb/badge.svg?branch=master)](https://coveralls.io/github/OutOfOrder/multidb?branch=master)
|
2
3
|
|
3
4
|
# Multidb
|
4
5
|
|
@@ -14,10 +15,11 @@ Randomized balancing of multiple connections within a group is supported. In the
|
|
14
15
|
|
15
16
|
## Requirements
|
16
17
|
|
17
|
-
* Ruby 2.
|
18
|
+
* Ruby 2.5 or later.
|
18
19
|
* ActiveRecord 5.1 or later.
|
19
20
|
|
20
21
|
## Older releases
|
22
|
+
For Ruby 2.4 use version 0.5.1
|
21
23
|
For ActiveRecord 4. through 5.0 use version 0.3
|
22
24
|
For ActiveRecord older than 4.0 use the gem version 0.1.13
|
23
25
|
For ActiveRecord older than 3.0 use 0.1.10
|
data/ar-multidb.gemspec
CHANGED
@@ -7,23 +7,27 @@ Gem::Specification.new do |s|
|
|
7
7
|
s.name = 'ar-multidb'
|
8
8
|
s.version = Multidb::VERSION
|
9
9
|
s.authors = ['Alexander Staubo', 'Edward Rudd']
|
10
|
-
s.email = [
|
11
|
-
s.homepage = ''
|
10
|
+
s.email = %w[alex@bengler.no urkle@outoforder.cc]
|
11
|
+
s.homepage = 'https://github.com/OutOfOrder/multidb'
|
12
12
|
s.summary = s.description = 'Multidb is an ActiveRecord extension for switching between multiple database connections, such as primary/replica setups.'
|
13
13
|
s.license = 'MIT'
|
14
|
+
s.metadata['rubygems_mfa_required'] = 'true'
|
14
15
|
|
15
16
|
s.files = `git ls-files`.split("\n")
|
16
17
|
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
17
18
|
s.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }
|
18
19
|
s.require_paths = ['lib']
|
19
20
|
|
20
|
-
s.required_ruby_version = '>= 2.
|
21
|
+
s.required_ruby_version = '>= 2.5.0'
|
21
22
|
|
22
|
-
s.add_runtime_dependency 'activerecord', '>= 5.1', '<
|
23
|
-
s.add_runtime_dependency 'activesupport', '>= 5.1', '<
|
23
|
+
s.add_runtime_dependency 'activerecord', '>= 5.1', '< 7.2'
|
24
|
+
s.add_runtime_dependency 'activesupport', '>= 5.1', '< 7.2'
|
24
25
|
|
25
|
-
s.add_development_dependency 'rake', '~>
|
26
|
+
s.add_development_dependency 'rake', '~> 13.0'
|
26
27
|
s.add_development_dependency 'rspec', '~> 3.8'
|
27
|
-
s.add_development_dependency 'rubocop', '~> 1.
|
28
|
+
s.add_development_dependency 'rubocop', '~> 1.28.0'
|
29
|
+
s.add_development_dependency 'rubocop-rspec', '~> 2.10.0'
|
30
|
+
s.add_development_dependency 'simplecov', '~> 0.21.2'
|
31
|
+
s.add_development_dependency 'simplecov-lcov', '~> 0.8.0'
|
28
32
|
s.add_development_dependency 'sqlite3', '~> 1.3'
|
29
33
|
end
|
data/lib/multidb/balancer.rb
CHANGED
@@ -26,7 +26,7 @@ module Multidb
|
|
26
26
|
end
|
27
27
|
|
28
28
|
def append(databases)
|
29
|
-
databases.each_pair do |name, config|
|
29
|
+
databases.with_indifferent_access.each_pair do |name, config|
|
30
30
|
configs = config.is_a?(Array) ? config : [config]
|
31
31
|
configs.each do |cfg|
|
32
32
|
if cfg['alias']
|
@@ -51,7 +51,7 @@ module Multidb
|
|
51
51
|
|
52
52
|
raise ArgumentError, "No such database connection '#{name}'" if candidates.empty?
|
53
53
|
|
54
|
-
candidate = candidates.
|
54
|
+
candidate = candidates.sample
|
55
55
|
|
56
56
|
block_given? ? yield(candidate) : candidate
|
57
57
|
end
|
data/lib/multidb/candidate.rb
CHANGED
@@ -2,13 +2,22 @@
|
|
2
2
|
|
3
3
|
module Multidb
|
4
4
|
class Candidate
|
5
|
+
USE_RAILS_61 = Gem::Version.new(::ActiveRecord::VERSION::STRING) >= Gem::Version.new('6.1')
|
6
|
+
SPEC_NAME = if USE_RAILS_61
|
7
|
+
'ActiveRecord::Base'
|
8
|
+
else
|
9
|
+
'primary'
|
10
|
+
end
|
11
|
+
|
5
12
|
def initialize(name, target)
|
6
13
|
@name = name
|
7
14
|
|
8
15
|
case target
|
9
16
|
when Hash
|
17
|
+
target = target.merge(name: 'primary') unless USE_RAILS_61
|
18
|
+
|
10
19
|
@connection_handler = ActiveRecord::ConnectionAdapters::ConnectionHandler.new
|
11
|
-
@connection_handler.establish_connection(target
|
20
|
+
@connection_handler.establish_connection(target)
|
12
21
|
when ActiveRecord::ConnectionAdapters::ConnectionHandler
|
13
22
|
@connection_handler = target
|
14
23
|
else
|
@@ -18,9 +27,9 @@ module Multidb
|
|
18
27
|
|
19
28
|
def connection(&block)
|
20
29
|
if block_given?
|
21
|
-
@connection_handler.retrieve_connection_pool(
|
30
|
+
@connection_handler.retrieve_connection_pool(SPEC_NAME).with_connection(&block)
|
22
31
|
else
|
23
|
-
@connection_handler.retrieve_connection(
|
32
|
+
@connection_handler.retrieve_connection(SPEC_NAME)
|
24
33
|
end
|
25
34
|
end
|
26
35
|
|
@@ -1,28 +1,6 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module Multidb
|
4
|
-
class << self
|
5
|
-
delegate :use, :get, :disconnect!, to: :balancer
|
6
|
-
end
|
7
|
-
|
8
|
-
def self.init(config)
|
9
|
-
activerecord_config = config.dup.with_indifferent_access
|
10
|
-
default_adapter = activerecord_config
|
11
|
-
configuration_hash = activerecord_config.delete(:multidb)
|
12
|
-
|
13
|
-
@balancer = Balancer.new(Configuration.new(default_adapter, configuration_hash || {}))
|
14
|
-
end
|
15
|
-
|
16
|
-
def self.balancer
|
17
|
-
@balancer || raise(NotInitializedError, 'Balancer not initialized. You need to run Multidb.init first')
|
18
|
-
end
|
19
|
-
|
20
|
-
def self.reset!
|
21
|
-
@balancer = nil
|
22
|
-
end
|
23
|
-
|
24
|
-
class NotInitializedError < StandardError; end
|
25
|
-
|
26
4
|
class Configuration
|
27
5
|
def initialize(default_adapter, configuration_hash)
|
28
6
|
@default_handler = ActiveRecord::Base.connection_handler
|
@@ -8,11 +8,11 @@ module Multidb
|
|
8
8
|
super
|
9
9
|
end
|
10
10
|
|
11
|
-
def debug(msg)
|
11
|
+
def debug(msg = nil)
|
12
12
|
name = Multidb.balancer.current_connection_name
|
13
13
|
if name
|
14
14
|
db = color("[DB: #{name}]", ActiveSupport::LogSubscriber::GREEN, true)
|
15
|
-
super(db + msg)
|
15
|
+
super(db + msg.to_s)
|
16
16
|
else
|
17
17
|
super
|
18
18
|
end
|
@@ -6,7 +6,13 @@ module Multidb
|
|
6
6
|
module Connection
|
7
7
|
def establish_connection(spec = nil)
|
8
8
|
super(spec)
|
9
|
-
|
9
|
+
config = if connection_pool.respond_to?(:db_config)
|
10
|
+
connection_pool.db_config.configuration_hash
|
11
|
+
else
|
12
|
+
connection_pool.spec.config
|
13
|
+
end
|
14
|
+
|
15
|
+
Multidb.init(config)
|
10
16
|
end
|
11
17
|
|
12
18
|
def connection
|
data/lib/multidb/version.rb
CHANGED
data/lib/multidb.rb
CHANGED
@@ -12,3 +12,29 @@ require_relative 'multidb/log_subscriber'
|
|
12
12
|
require_relative 'multidb/candidate'
|
13
13
|
require_relative 'multidb/balancer'
|
14
14
|
require_relative 'multidb/version'
|
15
|
+
|
16
|
+
module Multidb
|
17
|
+
# Error raised when the configuration has not been initialized
|
18
|
+
class NotInitializedError < StandardError; end
|
19
|
+
|
20
|
+
class << self
|
21
|
+
delegate :use, :get, :disconnect!, to: :balancer
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.init(config)
|
25
|
+
activerecord_config = config.dup.with_indifferent_access
|
26
|
+
default_adapter = activerecord_config
|
27
|
+
configuration_hash = activerecord_config.delete(:multidb)
|
28
|
+
|
29
|
+
@balancer = Balancer.new(Configuration.new(default_adapter, configuration_hash || {}))
|
30
|
+
end
|
31
|
+
|
32
|
+
def self.balancer
|
33
|
+
@balancer || raise(NotInitializedError, 'Balancer not initialized. You need to run Multidb.init first')
|
34
|
+
end
|
35
|
+
|
36
|
+
def self.reset!
|
37
|
+
@balancer = nil
|
38
|
+
Thread.current[:multidb] = nil
|
39
|
+
end
|
40
|
+
end
|