Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mongo 3 + Mongoid 5 Compatibility #2

Open
simonmorley opened this issue Sep 9, 2015 · 25 comments
Open

Mongo 3 + Mongoid 5 Compatibility #2

simonmorley opened this issue Sep 9, 2015 · 25 comments

Comments

@simonmorley
Copy link

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.

DatabaseCleaner/database_cleaner#343
DatabaseCleaner/database_cleaner#349
andreale/database_cleaner@e9b2c8c
https://github.com/DatabaseCleaner/database_cleaner/issues/386

@simonmorley
Copy link
Author

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

Will test further.

@tagliala
Copy link

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
Copy link

fonji commented Sep 22, 2015

+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
Copy link

vindia commented Sep 23, 2015

@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
Copy link

fonji commented Sep 23, 2015

@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
Copy link

vindia commented Sep 23, 2015

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

@agrass
Copy link

agrass commented Sep 25, 2015

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

@sbounmy
Copy link

sbounmy commented Sep 28, 2015

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
Copy link

agrass commented Sep 28, 2015

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

@sbounmy
Copy link

sbounmy commented Sep 28, 2015

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
Copy link

agrass commented Sep 28, 2015

Great! now with that configuratin is working. Thanks!

@vindia
Copy link

vindia commented Sep 28, 2015

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

@stefan-kolb
Copy link

I would also love to see Mongoid 5 compatibility 👍

@rdetert
Copy link

rdetert commented Oct 7, 2015

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
Copy link

snow commented Oct 13, 2015

@sbounmy thank you, it works for me!

@alexkravets
Copy link

@sbounmy thank you for the hint!

+1 for mongoid 5.0 dependency update

@sbounmy
Copy link

sbounmy commented Oct 15, 2015

@snow @alexkravets 🍻

@rogsmith
Copy link

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

machu referenced this issue in tdiary/rpaproxy-sinatra Oct 27, 2015
@satoryu
Copy link

satoryu commented Jan 25, 2016

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

  • mongoid 5.0.0
  • mongo 2.1.1
  • MongoDB 3.0.7

@tagliala
Copy link

version 1.5.1 works for me

  • mongoid 5.0.2
  • mongo 2.2.1
  • MongoDB 3.2.1 and MongoLab on Heroku

sample repos:

@satoryu
Copy link

satoryu commented Jan 25, 2016

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

@satoryu
Copy link

satoryu commented Feb 6, 2016

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
Copy link

satoryu commented Feb 8, 2016

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
Copy link

tagliala commented Feb 8, 2016

@satoryu thanks for sharing this

@netwire88
Copy link

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

@botandrose botandrose transferred this issue from DatabaseCleaner/database_cleaner Feb 18, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests