Skip to content

Commit dc4b3bb

Browse files
p-mongop
andcommitted
Fix RUBY-2054 upsert option is not sent to legacy servers with w:0 due to string/symbol key mismatch (#1610)
* Fix RUBY-2054 upsert option is not sent to legacy servers with w:0 due to string/symbol key mismatch * Remove debug print Co-authored-by: Oleg Pudeyev <p@users.noreply.github.com>
1 parent d128bdf commit dc4b3bb

File tree

3 files changed

+51
-6
lines changed

3 files changed

+51
-6
lines changed

lib/mongo/bulk_write/transformable.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ module Transformable
9292
Operation::U => doc[:replacement],
9393
}.tap do |d|
9494
if doc[:upsert]
95-
d[:upsert] = true
95+
d['upsert'] = true
9696
end
9797
d[Operation::COLLATION] = doc[:collation] if doc[:collation]
9898
end
@@ -108,7 +108,7 @@ module Transformable
108108
Operation::MULTI => true,
109109
}.tap do |d|
110110
if doc[:upsert]
111-
d[:upsert] = true
111+
d['upsert'] = true
112112
end
113113
d[Operation::COLLATION] = doc[:collation] if doc[:collation]
114114
d[Operation::ARRAY_FILTERS] = doc[:array_filters] if doc[:array_filters]
@@ -124,7 +124,7 @@ module Transformable
124124
Operation::U => doc[:update],
125125
}.tap do |d|
126126
if doc[:upsert]
127-
d[:upsert] = true
127+
d['upsert'] = true
128128
end
129129
d[Operation::COLLATION] = doc[:collation] if doc[:collation]
130130
d[Operation::ARRAY_FILTERS] = doc[:array_filters] if doc[:array_filters]

lib/mongo/collection/view/writable.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ def replace_one(replacement, opts = {})
234234
Operation::U => replacement,
235235
}
236236
if opts[:upsert]
237-
update_doc[:upsert] = true
237+
update_doc['upsert'] = true
238238
end
239239
with_session(opts) do |session|
240240
write_concern = write_concern_with_session(session)
@@ -281,7 +281,7 @@ def update_many(spec, opts = {})
281281
Operation::MULTI => true,
282282
}
283283
if opts[:upsert]
284-
update_doc[:upsert] = true
284+
update_doc['upsert'] = true
285285
end
286286
with_session(opts) do |session|
287287
write_concern = write_concern_with_session(session)
@@ -325,7 +325,7 @@ def update_one(spec, opts = {})
325325
Operation::U => spec,
326326
}
327327
if opts[:upsert]
328-
update_doc[:upsert] = true
328+
update_doc['upsert'] = true
329329
end
330330
with_session(opts) do |session|
331331
write_concern = write_concern_with_session(session)

spec/integration/crud_spec.rb

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
require 'spec_helper'
2+
3+
describe 'CRUD operations' do
4+
let(:collection) { authorized_client['crud_integration'] }
5+
6+
before do
7+
collection.delete_many
8+
end
9+
10+
describe 'upsert' do
11+
context 'with default write concern' do
12+
it 'upserts' do
13+
collection.count_documents({}).should == 0
14+
15+
res = collection.find(_id: 'foo').update_one({'$set' => {foo: 'bar'}}, upsert: true)
16+
17+
res.documents.first['upserted'].length.should == 1
18+
19+
collection.count_documents({}).should == 1
20+
end
21+
end
22+
23+
context 'unacknowledged write' do
24+
let(:unack_collection) do
25+
collection.with(write_concern: {w: 0})
26+
end
27+
28+
before do
29+
unack_collection.write_concern.acknowledged?.should be false
30+
end
31+
32+
it 'upserts' do
33+
unack_collection.count_documents({}).should == 0
34+
35+
res = unack_collection.find(_id: 'foo').update_one({'$set' => {foo: 'bar'}}, upsert: true)
36+
37+
# since write concern is unacknowledged, wait for the data to be
38+
# persisted (hopefully)
39+
sleep 0.25
40+
41+
unack_collection.count_documents({}).should == 1
42+
end
43+
end
44+
end
45+
end

0 commit comments

Comments
 (0)