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

Commit

Permalink
Fix deleting objects with BSON::ObjectId _id fields.
Browse files Browse the repository at this point in the history
closes #8
  • Loading branch information
nelhage committed Feb 14, 2013
1 parent 4dfb085 commit a4a15ae
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/mosql/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ def handle_op(op)
if options[:ignore_delete]
log.debug("Ignoring delete op on #{ns} as instructed.")
else
@sql.table_for_ns(ns).where(:_id => op['o']['_id']).delete
@sql.delete_ns(ns, op['o'])
end
else
log.info("Skipping unknown op #{op.inspect}")
Expand Down
10 changes: 10 additions & 0 deletions lib/mosql/sql.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def table_for_ns(ns)
@db[@schema.table_for_ns(ns).intern]
end


def upsert_ns(ns, obj)
h = {}
cols = @schema.all_columns(@schema.find_ns(ns))
Expand All @@ -33,6 +34,15 @@ def upsert_ns(ns, obj)
upsert(table_for_ns(ns), h)
end

# obj must contain an _id field. All other fields will be ignored.
def delete_ns(ns, obj)
cols = @schema.all_columns(@schema.find_ns(ns))
row = @schema.transform(ns, obj)
sqlid = row[cols.index("_id")]
raise "No _id found in transform of #{obj}" if sqlid.nil?
table_for_ns(ns).where(:_id => sqlid).delete
end

def upsert(table, item)
begin
upsert!(table, item)
Expand Down
12 changes: 12 additions & 0 deletions test/functional/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def fake_cli
cli.instance_variable_set(:@mongo, mongo)
cli.instance_variable_set(:@schemamap, @map)
cli.instance_variable_set(:@sql, @adapter)
cli.instance_variable_set(:@options, {})
cli
end

Expand All @@ -44,4 +45,15 @@ def fake_cli
})
assert_equal(27, sequel[:sqltable].where(:_id => o['_id'].to_s).select.first[:var])
end

it 'handle "d" ops with BSON::ObjectIds' do
o = { '_id' => BSON::ObjectId.new, 'var' => 17 }
@adapter.upsert_ns('mosql_test.collection', o)

@cli.handle_op({ 'ns' => 'mosql_test.collection',
'op' => 'd',
'o' => { '_id' => o['_id'] },
})
assert_equal(0, sequel[:sqltable].where(:_id => o['_id'].to_s).count)
end
end

0 comments on commit a4a15ae

Please sign in to comment.