开发者

Atomic removal of mongo collection using ruby driver

My current rails app is backed by mongoid/mongo. Between each test I want to clear the database. To this I run

::Mongoid.database.collections.select { |c| c.name !~ /^system/ }.each { |c| c.remove() }

Problem is the removal of the collec开发者_运维问答tion seems to run in the background. Sometimes the next test will start, insert a document and then have it cleared by the remove operation. Is there any way to make the collection removal blocking?

I understand there is an $atomic option, having looked at the source for mongo/collection I can't see any way to pass the option in. How can I make the collcetion removal blocking?


Mongoid when you call remove, simply passes what you send as arguments to the underlying mongo ruby driver. From API documentation http://api.mongodb.org/ruby/1.2.1/Mongo/Collection.html#remove-instance_method safe=>true blocks until it is done. So:

::Mongoid.database.collections.select { |c| c.name !~ /^system/ }.each { |c| c.remove(:safe => true) }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜