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

MONGOID-5819 Do not pass the :database option when creating a client (Backport to 9.0) #5890

Merged
merged 1 commit into from
Oct 22, 2024
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
9 changes: 6 additions & 3 deletions lib/mongoid/persistence_context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,15 @@ def database_name
def client
@client ||= begin
client = Clients.with_name(client_name)
options = client_options

if database_name_option
client = client.use(database_name)
options = options.except(:database, 'database')
end
unless client_options.empty?
client = client.with(client_options)
end

client = client.with(options) unless options.empty?

client
end
end
Expand Down
4 changes: 2 additions & 2 deletions spec/mongoid/clients/options_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
let(:options) { { database: 'other' } }

it 'sets the options on the client' do
expect(persistence_context.client.options['database']).to eq(options[:database])
expect(persistence_context.client.options['database'].to_s).to eq(options[:database].to_s)
end

it 'does not set the options on class level' do
Expand Down Expand Up @@ -319,7 +319,7 @@
end

it 'sets the options on the client' do
expect(persistence_context.client.options['database']).to eq(options[:database])
expect(persistence_context.client.options['database'].to_s).to eq(options[:database].to_s)
end

it 'does not set the options on instance level' do
Expand Down
31 changes: 31 additions & 0 deletions spec/mongoid/persistence_context_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,20 @@
end
end
end

context 'when the database is specified as a proc' do
let(:options) { { database: ->{ 'other' } } }

after { persistence_context.client.close }

it 'evaluates the proc' do
expect(persistence_context.database_name).to eq(:other)
end

it 'does not pass the proc to the client' do
expect(persistence_context.client.database.name).to eq('other')
end
end
end

describe '#client' do
Expand Down Expand Up @@ -608,6 +622,23 @@
end
end

context 'when the client is set as a proc in the storage options' do
let(:options) { {} }

before do
Band.store_in client: ->{ :alternative }
end

after do
persistence_context.client.close
Band.store_in client: nil
end

it 'uses the client option' do
expect(persistence_context.client).to eq(Mongoid::Clients.with_name(:alternative))
end
end

context 'when there is no client option set' do

let(:options) do
Expand Down
Loading