Skip to content

Commit 5032dd5

Browse files
committed
Merge pull request #708 from estolfo/RUBY-1054-cmd-read
RUBY-1054 Use primary server selector for command unless read pref in opts
2 parents c2deda1 + d2e3bcf commit 5032dd5

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

lib/mongo/database.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,8 @@ def collections
148148
#
149149
# @return [ Hash ] The result of the command execution.
150150
def command(operation, opts = {})
151-
preference = opts[:read] ? ServerSelector.get(client.options.merge(opts[:read])) : read_preference
152-
server = preference.select_server(cluster)
151+
preference = ServerSelector.get(client.options.merge(opts[:read])) if opts[:read]
152+
server = preference ? preference.select_server(cluster) : cluster.next_primary
153153
Operation::Command.new({
154154
:selector => operation,
155155
:db_name => name,

spec/mongo/database_spec.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,18 @@
228228
end.to raise_error(Mongo::Error::NoServerAvailable)
229229
end
230230
end
231+
232+
context 'when there is a read preference set on the client' do
233+
234+
let(:database) do
235+
described_class.new(authorized_client.with(read: { mode: :secondary }), TEST_DB)
236+
end
237+
238+
it 'does not use the read preference' do
239+
expect(database.client.cluster).to receive(:next_primary).and_call_original
240+
database.command(ping: 1)
241+
end
242+
end
231243
end
232244

233245
describe '#drop' do

0 commit comments

Comments
 (0)