Skip to content

Commit 81dd7ab

Browse files
committed
add rails 6.1 support
1 parent e4a0f5b commit 81dd7ab

7 files changed

Lines changed: 38 additions & 9 deletions

File tree

.github/workflows/prs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ jobs:
2323
- '5.1'
2424
- '5.2'
2525
- '6.0'
26+
- '6.1'
2627
exclude:
2728
- ruby: '3.0'
2829
activerecord: '5.1'

ar-multidb.gemspec

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ Gem::Specification.new do |s|
2020

2121
s.required_ruby_version = '>= 2.5.0'
2222

23-
s.add_runtime_dependency 'activerecord', '>= 5.1', '< 6.1'
24-
s.add_runtime_dependency 'activesupport', '>= 5.1', '< 6.1'
23+
s.add_runtime_dependency 'activerecord', '>= 5.1', '< 6.2'
24+
s.add_runtime_dependency 'activesupport', '>= 5.1', '< 6.2'
2525

2626
s.add_development_dependency 'rake', '~> 13.0'
2727
s.add_development_dependency 'rspec', '~> 3.8'

gemfiles/activerecord-6.1.gemfile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# frozen_string_literal: true
2+
3+
source 'https://rubygems.org'
4+
5+
gem 'activerecord', '~> 6.1.0'
6+
7+
gemspec path: '..'

lib/multidb/candidate.rb

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,22 @@
22

33
module Multidb
44
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+
512
def initialize(name, target)
613
@name = name
714

815
case target
916
when Hash
17+
target = target.merge(name: 'primary') unless USE_RAILS_61
18+
1019
@connection_handler = ActiveRecord::ConnectionAdapters::ConnectionHandler.new
11-
@connection_handler.establish_connection(target.merge(name: 'primary'))
20+
@connection_handler.establish_connection(target)
1221
when ActiveRecord::ConnectionAdapters::ConnectionHandler
1322
@connection_handler = target
1423
else
@@ -18,9 +27,9 @@ def initialize(name, target)
1827

1928
def connection(&block)
2029
if block_given?
21-
@connection_handler.retrieve_connection_pool('primary').with_connection(&block)
30+
@connection_handler.retrieve_connection_pool(SPEC_NAME).with_connection(&block)
2231
else
23-
@connection_handler.retrieve_connection('primary')
32+
@connection_handler.retrieve_connection(SPEC_NAME)
2433
end
2534
end
2635

lib/multidb/model_extensions.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,13 @@ module Multidb
66
module Connection
77
def establish_connection(spec = nil)
88
super(spec)
9-
Multidb.init(connection_pool.spec.config)
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)
1016
end
1117

1218
def connection

spec/lib/multidb/candidate_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
expect(handler).to an_instance_of(ActiveRecord::ConnectionAdapters::ConnectionHandler)
1919
end
2020

21-
it 'merges the name: primary into the hash' do
21+
it 'merges the name: primary into the hash', rails: '< 6.1' do
2222
handler = instance_double('ActiveRecord::ConnectionAdapters::ConnectionHandler')
2323
allow(ActiveRecord::ConnectionAdapters::ConnectionHandler).to receive(:new).and_return(handler)
2424
allow(handler).to receive(:establish_connection)
@@ -68,7 +68,7 @@
6868
it 'calls retrieve_connection_pool' do
6969
subject.connection { |_| nil }
7070

71-
expect(target).to have_received(:retrieve_connection_pool).with('primary')
71+
expect(target).to have_received(:retrieve_connection_pool).with(Multidb::Candidate::SPEC_NAME)
7272
end
7373

7474
it 'yields a connection object' do
@@ -82,7 +82,7 @@
8282
it 'calls retrieve_connection on the handler' do
8383
subject.connection
8484

85-
expect(target).to have_received(:retrieve_connection).with('primary')
85+
expect(target).to have_received(:retrieve_connection).with(Multidb::Candidate::SPEC_NAME)
8686
end
8787
end
8888
end

spec/spec_helper.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@
2323
config.filter_run :focus
2424
config.run_all_when_everything_filtered = true
2525

26+
config.filter_run_excluding rails: lambda { |v|
27+
rails_version = Gem::Version.new(ActiveRecord::VERSION::STRING)
28+
test = Gem::Requirement.new(v)
29+
!test.satisfied_by?(rails_version)
30+
}
31+
2632
config.expect_with :rspec do |expectations|
2733
expectations.syntax = :expect
2834
expectations.include_chain_clauses_in_custom_matcher_descriptions = true

0 commit comments

Comments
 (0)