Quick Fix (and a question): Mongoid Edge

I just went throught the process of upgrading my current app to the latest edge for Rails 3, Mongoid and all of my other
gems. And when I went to run my specs, I received this error:

    Database command 'drop' failed: {"ns"=>"your_db.system.indexes", "errmsg"=>"assertion: can't drop system ns", "ok"=>0.0}

The only time a drop is called is in my spec_helper.rb file:

/spec/spec_helper.rb

    Rspec.configure do |config|
      # other config stuff here

      config.before(:each) do
        Mongoid.master.collections.each(&:drop)
      end
    end

What that block does is drop all of my Mongoid collections before each spec is run to ensure I have a “known state” for
my specs. This has always worked just fine as is, but it appears that something has changed in the latest Mongoid
causing it to error on attempting to drop “system.indexes”. After some tinkering, this is how I resolved it:

/spec/spec_helper.rb

    Rspec.configure do |config|
      # other config stuff here

      config.before(:each) do
        Mongoid.master.collections.select { |c| c.name != 'system.indexes' }.each(&:drop)
      end
    end

Simple fix, but it gets the job done. I just simply exclude “system.indexes” from the collections to drop and all is
well. Since this is only for my tests, I think this should be fine. But…

I want to know if anyone else knows why this is happening and if there is a better way to resolve it. Anyone?

Related Articles:

Post Footer automatically generated by Add Post Footer Plugin for wordpress.

This entry was posted in mongodb, mongoid, rspec, ruby. Bookmark the permalink. Follow any comments here with the RSS feed for this post.

3 Responses to Quick Fix (and a question): Mongoid Edge

  1. Max Hawkins says:

    Nice. This is just what I was looking for.

    I modified the snippet to use reject instead of select != because I think it reads better:

    config.before(:each) do
    Mongoid.master.collections.reject { |c| c.name == ‘system.indexes’ }.each(&:drop)
    end

  2. Jeremy says:

    Thanks, man – I just about threw a clot when I fired up my tests this morning. Bundler has me tracking edges way more than I used to on Rails 2 projects. I suppose that’s mostly a good thing, though.

  3. erwin says:

    Thanks Joey ! you saved my day …