Skip to content
This repository was archived by the owner on Dec 22, 2020. It is now read-only.

Commit 187fa6e

Browse files
committed
Fix a bug with updates on objects with BSON::ObjectId's.
closes #9.
1 parent fa2ce80 commit 187fa6e

File tree

4 files changed

+30
-10
lines changed

4 files changed

+30
-10
lines changed

Gemfile.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
mosql (0.1.0)
4+
mosql (0.1.1)
55
bson_ext
66
json
77
log4r
@@ -17,7 +17,7 @@ GEM
1717
bson (1.8.2)
1818
bson_ext (1.8.2)
1919
bson (~> 1.8.2)
20-
json (1.7.6)
20+
json (1.7.7)
2121
log4r (1.1.10)
2222
metaclass (0.0.1)
2323
minitest (3.0.0)

lib/mosql/cli.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,11 +253,12 @@ def optail
253253
end
254254

255255
def sync_object(ns, _id)
256+
sqlid = @sql.transform_one_ns(ns, { '_id' => _id })['_id']
256257
obj = collection_for_ns(ns).find_one({:_id => _id})
257258
if obj
258259
@sql.upsert_ns(ns, obj)
259260
else
260-
@sql.table_for_ns(ns).where(:_id => _id).delete()
261+
@sql.table_for_ns(ns).where(:_id => sqlid).delete()
261262
end
262263
end
263264

lib/mosql/sql.rb

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,24 @@ def table_for_ns(ns)
2525
@db[@schema.table_for_ns(ns).intern]
2626
end
2727

28-
29-
def upsert_ns(ns, obj)
28+
def transform_one_ns(ns, obj)
3029
h = {}
3130
cols = @schema.all_columns(@schema.find_ns(ns))
3231
row = @schema.transform(ns, obj)
3332
cols.zip(row).each { |k,v| h[k] = v }
33+
h
34+
end
35+
36+
def upsert_ns(ns, obj)
37+
h = transform_one_ns(ns, obj)
3438
upsert(table_for_ns(ns), h)
3539
end
3640

3741
# obj must contain an _id field. All other fields will be ignored.
3842
def delete_ns(ns, obj)
39-
cols = @schema.all_columns(@schema.find_ns(ns))
40-
row = @schema.transform(ns, obj)
41-
sqlid = row[cols.index("_id")]
42-
raise "No _id found in transform of #{obj}" if sqlid.nil?
43-
table_for_ns(ns).where(:_id => sqlid).delete
43+
h = transform_one_ns(ns, obj)
44+
raise "No _id found in transform of #{obj.inspect}" if h['_id'].nil?
45+
table_for_ns(ns).where(:_id => h['_id']).delete
4446
end
4547

4648
def upsert(table, item)

test/functional/cli.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,21 @@ def fake_cli
5656
})
5757
assert_equal(0, sequel[:sqltable].where(:_id => o['_id'].to_s).count)
5858
end
59+
60+
it 'handle "u" ops with $set and BSON::ObjectIDs' do
61+
o = { '_id' => BSON::ObjectId.new, 'var' => 17 }
62+
@adapter.upsert_ns('mosql_test.collection', o)
63+
64+
# $set's are currently a bit of a hack where we read the object
65+
# from the db, so make sure the new object exists in mongo
66+
connect_mongo['mosql_test']['collection'].insert(o.merge('var' => 100),
67+
:w => 1)
68+
69+
@cli.handle_op({ 'ns' => 'mosql_test.collection',
70+
'op' => 'u',
71+
'o2' => { '_id' => o['_id'] },
72+
'o' => { '$set' => { 'var' => 100 } },
73+
})
74+
assert_equal(100, sequel[:sqltable].where(:_id => o['_id'].to_s).select.first[:var])
75+
end
5976
end

0 commit comments

Comments
 (0)