Skip to content

Commit 203b813

Browse files
psaghm
authored andcommitted
Fix read preference passing to ruby driver
1 parent be97393 commit 203b813

File tree

2 files changed

+33
-14
lines changed

2 files changed

+33
-14
lines changed

lib/mongoid/persistence_context.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,10 @@ def database_name
107107
#
108108
# @since 6.0.0
109109
def client
110+
client_options = send(:client_options)
111+
if client_options[:read].is_a?(Symbol)
112+
client_options = client_options.merge(read: {mode: client_options[:read]})
113+
end
110114
@client ||= (client = Clients.with_name(client_name)
111115
client = client.use(database_name) if database_name_option
112116
client.with(client_options))
@@ -208,7 +212,7 @@ def clear(object, cluster = nil)
208212
if context = get(object)
209213
context.client.close unless (context.cluster.equal?(cluster) || cluster.nil?)
210214
end
211-
ensure
215+
ensure
212216
Thread.current["[mongoid][#{object.object_id}]:context"] = nil
213217
end
214218
end

spec/mongoid/clients/options_spec.rb

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -188,25 +188,40 @@ class TestModel
188188

189189
context 'when returning a criteria' do
190190

191-
let(:context_and_criteria) do
192-
collection = nil
193-
cxt = TestModel.with(read: :secondary) do |klass|
194-
collection = klass.all.collection
195-
klass.persistence_context
191+
shared_context 'applies secondary read preference' do
192+
193+
let(:context_and_criteria) do
194+
collection = nil
195+
cxt = TestModel.with(read_secondary_option) do |klass|
196+
collection = klass.all.collection
197+
klass.persistence_context
198+
end
199+
[ cxt, collection ]
196200
end
197-
[ cxt, collection ]
198-
end
199201

200-
let(:persistence_context) do
201-
context_and_criteria[0]
202+
let(:persistence_context) do
203+
context_and_criteria[0]
204+
end
205+
206+
let(:client) do
207+
context_and_criteria[1].client
208+
end
209+
210+
it 'applies the options to the criteria client' do
211+
expect(client.options['read']).to eq('mode' => :secondary)
212+
end
202213
end
203214

204-
let(:client) do
205-
context_and_criteria[1].client
215+
context 'read: :secondary shorthand' do
216+
let(:read_secondary_option) { {read: :secondary} }
217+
218+
it_behaves_like 'applies secondary read preference'
206219
end
207220

208-
it 'applies the options to the criteria client' do
209-
expect(client.options['read']).to eq(:secondary)
221+
context 'read: {mode: :secondary}' do
222+
let(:read_secondary_option) { {read: {mode: :secondary}} }
223+
224+
it_behaves_like 'applies secondary read preference'
210225
end
211226
end
212227

0 commit comments

Comments
 (0)