MongoEngine / MongoEngine/mongoengine

connect() does not use connection settings previously registered as default

Open
#1,770 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Client/Connect
Dominant language
Python
Stars
4.3k
Forks
1.2k
Avg merge
4h 41m
Merged PRs (30d)
11

Description

Hi,

I use register_connection() to register the default connection settings and connect() to establish a connection afterwards. When calling connect() without an alias, I would expect to connect using the default settings previously defined with register_connection(), though connect() ignores the default alias and falls back to the sane defaults:

>>> import mongoengine as m
>>> m.register_connection('default', db='mongoenginetest', host='mongomock://user:pass@localhost1/mongoenginetest')
>>> conn = m.connect(db='mongoenginetest')
>>> conn
MongoClient(host=['localhost:27017'], document_class=dict, tz_aware=False, connect=True, read_preference=Primary())

Note, the host used when registering the default alias and the host of the established client are different. connect() ends up overwriting the default connection settings.

get_connection() seems to respect the default connection settings instead:

>>> import mongoengine as m
>>> m.register_connection('default', db='mongoenginetest', host='mongomock://user:pass@localhost1/mongoenginetest')
>>> conn = m.connection.get_connection()
>>> conn
mongomock.MongoClient('mongodb://user:pass@localhost1/mongoenginetest', 27017)

I am a bit confused whether this is an expected behaviour or not. My proposed fix is changing connect() to look at _connection_settings instead of _connections before registering a connection which doesn't seem to break any of the existing tests.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading the connect(), register_connection(), and get_connection() entry points, then reproduce the issue using the default alias and the _connection_settings and _connections state described here. Done means connect() preserves the previously registered default settings, including the host, with tests covering the behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
mongodb, python
Domain
database
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.