Skip to content

Commit 2241796

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 cffe70b commit 2241796

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
@@ -232,7 +232,7 @@ def replace_one(replacement, opts = {})
232232
Operation::U => replacement,
233233
}
234234
if opts[:upsert]
235-
update_doc[:upsert] = true
235+
update_doc['upsert'] = true
236236
end
237237
with_session(opts) do |session|
238238
write_concern = write_concern_with_session(session)
@@ -279,7 +279,7 @@ def update_many(spec, opts = {})
279279
Operation::MULTI => true,
280280
}
281281
if opts[:upsert]
282-
update_doc[:upsert] = true
282+
update_doc['upsert'] = true
283283
end
284284
with_session(opts) do |session|
285285
write_concern = write_concern_with_session(session)
@@ -323,7 +323,7 @@ def update_one(spec, opts = {})
323323
Operation::U => spec,
324324
}
325325
if opts[:upsert]
326-
update_doc[:upsert] = true
326+
update_doc['upsert'] = true
327327
end
328328
with_session(opts) do |session|
329329
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)