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

Automatically parse the dbname from the URI #350

Merged
merged 6 commits into from
Dec 2, 2020
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions src/maggma/stores/mongolike.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from monty.serialization import loadfn
from pydash import get, has, set_
from pymongo import MongoClient, ReplaceOne
from pymongo.errors import OperationFailure, DocumentTooLarge
from pymongo.errors import OperationFailure, DocumentTooLarge, ConfigurationError
from sshtunnel import SSHTunnelForwarder


Expand Down Expand Up @@ -366,15 +366,28 @@ class MongoURIStore(MongoStore):
client parameters via TXT records
"""

def __init__(self, uri: str, database: str, collection_name: str, **kwargs):
def __init__(self, uri: str, collection_name: str, database: str = None, **kwargs):
"""
Args:
uri: MongoDB+SRV URI
database: database to connect to
collection_name: The collection name
"""
self.uri = uri
self.database = database

# parse the dbname from the uri
if database is None:
with MongoClient(self.uri) as conn:
try:
db = conn.get_default_database()
except ConfigurationError:
raise ConfigurationError(
"If database name is not supplied, a database must be set in the uri"
)
self.database = db.name
else:
self.database = database
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does self.database even need to be stored if it's not provided; IE, does anything break if database = None? Also, this should probably be in connect ? Connection on init can be bad.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok using the Mongoclient there was a little hacky.
I have changed it.
Technically the code will not break if database == None but it will break if database == None and no value is found in the URI.
I do think the database should be kept since every Mongo-like store must have a database anyways.


self.collection_name = collection_name
self.kwargs = kwargs
self._collection = None
Expand Down
17 changes: 16 additions & 1 deletion tests/stores/test_mongolike.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import mongomock.collection
import pymongo.collection
from pymongo.errors import OperationFailure, DocumentTooLarge
from pymongo.errors import OperationFailure, DocumentTooLarge, ConfigurationError
import pytest

from maggma.core import StoreError
Expand Down Expand Up @@ -286,3 +286,18 @@ def test_mongo_uri():
is_name = store.name is uri
# This is try and keep the secret safe
assert is_name


def test_mongo_uri_dbname_parse():
# test parsing dbname from uri
uri_with_db = "mongodb://uuu:xxxx@host:27017/fake_db"
store = MongoURIStore(uri_with_db, "test")
assert store.database == "fake_db"

uri_with_db = "mongodb://uuu:xxxx@host:27017/fake_db"
store = MongoURIStore(uri_with_db, "test", database="fake_db2")
assert store.database == "fake_db2"

uri_with_db = "mongodb://uuu:xxxx@host:27017"
with pytest.raises(ConfigurationError):
MongoURIStore(uri_with_db, "test")