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

Commit a4a15ae

Browse files
committed
Fix deleting objects with BSON::ObjectId _id fields.
closes #8
1 parent 4dfb085 commit a4a15ae

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

lib/mosql/cli.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ def handle_op(op)
305305
if options[:ignore_delete]
306306
log.debug("Ignoring delete op on #{ns} as instructed.")
307307
else
308-
@sql.table_for_ns(ns).where(:_id => op['o']['_id']).delete
308+
@sql.delete_ns(ns, op['o'])
309309
end
310310
else
311311
log.info("Skipping unknown op #{op.inspect}")

lib/mosql/sql.rb

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

28+
2829
def upsert_ns(ns, obj)
2930
h = {}
3031
cols = @schema.all_columns(@schema.find_ns(ns))
@@ -33,6 +34,15 @@ def upsert_ns(ns, obj)
3334
upsert(table_for_ns(ns), h)
3435
end
3536

37+
# obj must contain an _id field. All other fields will be ignored.
38+
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
44+
end
45+
3646
def upsert(table, item)
3747
begin
3848
upsert!(table, item)

test/functional/cli.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ def fake_cli
2020
cli.instance_variable_set(:@mongo, mongo)
2121
cli.instance_variable_set(:@schemamap, @map)
2222
cli.instance_variable_set(:@sql, @adapter)
23+
cli.instance_variable_set(:@options, {})
2324
cli
2425
end
2526

@@ -44,4 +45,15 @@ def fake_cli
4445
})
4546
assert_equal(27, sequel[:sqltable].where(:_id => o['_id'].to_s).select.first[:var])
4647
end
48+
49+
it 'handle "d" ops with BSON::ObjectIds' do
50+
o = { '_id' => BSON::ObjectId.new, 'var' => 17 }
51+
@adapter.upsert_ns('mosql_test.collection', o)
52+
53+
@cli.handle_op({ 'ns' => 'mosql_test.collection',
54+
'op' => 'd',
55+
'o' => { '_id' => o['_id'] },
56+
})
57+
assert_equal(0, sequel[:sqltable].where(:_id => o['_id'].to_s).count)
58+
end
4759
end

0 commit comments

Comments
 (0)