DatabaseCleaner / database_cleaner-mongoid

MIT License
9 stars 7 forks source link

Mongo 3 + Mongoid 5 Compatibility #2

Open simonmorley opened 9 years ago

simonmorley commented 9 years ago

The pull request from DatabaseCleaner/database_cleaner#343 fixes compatibility with Mongo 3 but doesn't address the problem of Mongo 3 + Mongoid 5.

In the current master, the Mongo 2 truncation mixin contains this:

database['system.namespaces'].find(:name => { '$not' => /\.system\.|\$/ }).to_a.map do |collection|
  _, name = collection['name'].split('.', 2)
  name
end

And this commit won't work with mongo v3. Instead, I've found this to work:

session.command(listCollections: 1).first[:cursor][:firstBatch].map do |collection|
    collection[:name]
...

Plus, you also need the sessions function, also excluded:

def session
  ::Mongoid.default_session
end

But that won't work with Mongo 2 I don't believe.

What is the plan for Mongo3 - do you want me to merge this in to a separate module or have you already dealt with?

It seems this has been covered but not for Mongoid 5.

https://github.com/DatabaseCleaner/database_cleaner/pull/343 https://github.com/DatabaseCleaner/database_cleaner/pull/349 https://github.com/andreale/database_cleaner/commit/e9b2c8cd5d335006b8a59e2a89cdbd3fb9e5a23d https://github.com/DatabaseCleaner/database_cleaner/issues/386

simonmorley commented 9 years ago

Actually, Mongoid.default_session has been depreciated too and need to be replaced with: default_client

Will test further.

tagliala commented 8 years ago

I'm getting this error with Mongoid 5: https://travis-ci.org/diowa/ruby2-rails4-bootstrap-heroku/builds/81394387#L313

/home/travis/build/diowa/ruby2-rails4-bootstrap-heroku/vendor/bundle/ruby/2.2.0/gems/mongo-2.1.0/lib/mongo/server_selector.rb:68:in `fetch': key not found: "primary" (KeyError)
fonji commented 8 years ago

+1 I'm getting this error (just in case someone is googling it):

NoMethodError: undefined method `each_pair' for "primary":String
from [redacted]/gems/bson-3.2.4/lib/bson/document.rb:82:in `initialize'

caused by

database['system.namespaces'].find(:name => { '$not' => /\.system\.|\$/ }).to_a

Edit: it seems that it's not related. I'm investigating.

vindia commented 8 years ago

@fonji Would love to hear what you find. I keep running into that one too and my knowledge of MongoDB / Mongoid is not deep enough to investigate this issue.

fonji commented 8 years ago

@vindia: sorry, I'm in a hurry and didn't find the cause in the time I allowed myself to investigate. I tried a fork of MongoidCleaner that is supposed to support Mongoid 5 and I still got the same error. I rolled back to Mongoid 4 for the time being and will try again when I'll have more time (if that ever happens).

vindia commented 8 years ago

@fonji Thanks! I think I will stick to Mongo 2.6 and Mongoid 4.0 for the time being.

agrass commented 8 years ago

I have the same error of @fonji. anyone has a workaround for this issue?

sbounmy commented 8 years ago

hello guys, I was able to get rid of this error by updating my mongoid.yml:

  test:
   sessions:
     default:
       database: my_app_test
       hosts:
         - localhost:27017
       options:
-        read: primary
+        read:
+          mode: :primary
         # In the test environment we lower the retries and retry interval to
         # low amounts for fast failures.
         max_retries: 1

Please let me know if this works for you :)

agrass commented 8 years ago

thanks @sbounmy, I tried but still having the same issue. What happens with the "clients:" and "database:" keys??

sbounmy commented 8 years ago

ups, i've updated my last post. For full mongoid.yml reference: https://github.com/mongodb/mongoid/blob/master/lib/rails/generators/mongoid/config/templates/mongoid.yml

Are you sure you have correctly updated your mongoid.yml?

agrass commented 8 years ago

Great! now with that configuratin is working. Thanks!

vindia commented 8 years ago

That works, thanks! BTW, you can also leave the who read option out, as :primary is the default anyway.

stefan-kolb commented 8 years ago

I would also love to see Mongoid 5 compatibility :+1:

rdetert commented 8 years ago

I'm not a MongoidCleaner user but I ran into the same issue when upgrading to Mongoid 5. Changing the mongoid.yml config file worked for me:

      options:
        read:
          mode: :primary
        max_pool_size: 1
snow commented 8 years ago

@sbounmy thank you, it works for me!

alexkravets commented 8 years ago

@sbounmy thank you for the hint!

+1 for mongoid 5.0 dependency update

sbounmy commented 8 years ago

@snow @alexkravets :beers:

rogsmith commented 8 years ago

Make "primary" a symbol ":primary" worked for me too.

satoryu commented 8 years ago

it doesn't work for me. In my environment, the versions are

tagliala commented 8 years ago

version 1.5.1 works for me

sample repos:

satoryu commented 8 years ago

umm, although I upgraded these gems to the same version, but it doesn't work yet...

satoryu commented 8 years ago

In my case, collections returns an empty array. collections uses system.namespace but the manual says it is deprecated since version mongodb versions 3.0. so it should not work, I think. I'm a beginner for MongoDB. Hoping anybody helps me :)

satoryu commented 8 years ago

I've resolved my problem! The problem is caused by using WiredTiger storage engine, default storage engine since 3.2. After I changed my mongo's configuration as follows, database_cleaner works.

storage:
  dbPath: /usr/local/var/mongoedb
  engine: mmpav1  # <= Added

So this means that database_cleaner doesn't work with wiredTiger, in other words v3.2 or newer of mongodb in default.

tagliala commented 8 years ago

@satoryu thanks for sharing this

netwire88 commented 8 years ago

I'm using MongoDB v3.2.4 with mongoid-5.1.1, and database_cleaner-1.5.1. The cleaning doesn't seem to be working anymore. I'm not sure if it's due to the engine, so far I'm using the default. I tried to use mmapv1 per https://docs.mongodb.org/manual/reference/configuration-options/ but my MongoDB won't restart. Anyone else having problems?

Here's my spec_helper.rb

  config.before(:suite) do
    # DatabaseCleaner.strategy = :truncation
    DatabaseCleaner[:mongoid].strategy = :truncation
    DatabaseCleaner.clean_with(:truncation)
    Capybara.javascript_driver = :webkit
  end
  config.before(:each) do
    Rails.cache.clear
    DatabaseCleaner.start
  end
  config.after(:each) do
    DatabaseCleaner.clean
    Capybara.reset_sessions!
  end