Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow 'servers' to propagate to Sequel connection #169

Merged
merged 1 commit into from
Mar 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/sequel_rails/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ def normalize_repository_config(hash)

config['max_connections'] = max_connections if max_connections
config['search_path'] = search_path if search_path
config['servers'] = servers if servers
config['test'] = test_connect

url = ENV['DATABASE_URL']
Expand Down
8 changes: 6 additions & 2 deletions lib/sequel_rails/db_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ def build_url(cfg)
return URI::Generic.build(:scheme => adapter, :opaque => database)
end

# these four are handled separately
params = cfg.reject { |k, _| %w(adapter host port database).include? k }
# these are handled separately
params = cfg.reject { |k, _| non_params.include? k }

if (v = params['search_path'])
# make sure there's no whitespace
Expand All @@ -96,6 +96,10 @@ def build_url(cfg)
:query => q
)
end

def non_params
%w(adapter host port database servers)
end
end
end

Expand Down
42 changes: 42 additions & 0 deletions spec/lib/sequel_rails/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,44 @@
end
end

shared_examples 'servers' do
context 'servers' do
let(:servers) { { read_only: { host: 'replica' } } }

shared_examples 'passes the value only through options hash' do
it 'passes the value only through options hash' do
expect(::Sequel).to receive(:connect) do |hash_or_url, *_|
if hash_or_url.is_a? Hash
servers.keys.each do |k|
expect(hash_or_url[:servers][k]).to eq servers[k]
end
else
puts hash_or_url
expect(hash_or_url).not_to include('servers=')
end
end
subject.connect environment
end
end

context 'with servers set in config' do
before do
subject.servers = servers
end

include_examples 'passes the value only through options hash'
end

context 'with servers set in environment' do
before do
environments[environment]['servers'] = servers
end

include_examples 'passes the value only through options hash'
end
end
end

shared_examples 'with DATABASE_URL in ENV' do
let(:database_url) { 'adapter://user:pass@host/db' }
def with_database_url_env
Expand Down Expand Up @@ -283,6 +321,7 @@ def with_database_url_env
include_examples 'test_connect'
include_examples 'max_connections'
include_examples 'search_path'
include_examples 'servers'
include_examples 'with DATABASE_URL in ENV'

let(:is_jruby) { false }
Expand All @@ -299,6 +338,7 @@ def with_database_url_env
include_examples 'test_connect'
include_examples 'max_connections'
include_examples 'search_path'
include_examples 'servers'
include_examples 'with DATABASE_URL in ENV'

let(:is_jruby) { true }
Expand Down Expand Up @@ -332,6 +372,7 @@ def with_database_url_env
context 'in C-Ruby' do
include_examples 'test_connect'
include_examples 'max_connections'
include_examples 'servers'
include_examples 'with DATABASE_URL in ENV'

let(:is_jruby) { false }
Expand All @@ -347,6 +388,7 @@ def with_database_url_env
context 'in JRuby' do
include_examples 'test_connect'
include_examples 'max_connections'
include_examples 'servers'
include_examples 'with DATABASE_URL in ENV'

let(:is_jruby) { true }
Expand Down