From 34212262bd6ea0ec206d694a763204158bb6f469 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Mon, 16 Nov 2020 06:07:49 +0000 Subject: [PATCH 1/4] Bump sshtunnel from 0.1.5 to 0.3.1 --- requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index ec8946f78..7f0410687 100644 --- a/requirements.txt +++ b/requirements.txt @@ -12,6 +12,6 @@ numpy==1.19.2 pynng==0.5.0 dnspython==2.0.0 uvicorn==0.12.2 -sshtunnel==0.1.5 +sshtunnel==0.3.1 msgpack==1.0.0 msgpack-python==0.5.6 From 5004137fe2589271f210f9272787ed25305b92dd Mon Sep 17 00:00:00 2001 From: jmmshn Date: Wed, 2 Dec 2020 12:36:09 -0500 Subject: [PATCH 2/4] parse the db name from the uri if present --- src/maggma/stores/mongolike.py | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/src/maggma/stores/mongolike.py b/src/maggma/stores/mongolike.py index 3f585b4e9..a6219da55 100644 --- a/src/maggma/stores/mongolike.py +++ b/src/maggma/stores/mongolike.py @@ -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 +from pymongo.errors import OperationFailure, ConfigurationError from sshtunnel import SSHTunnelForwarder from maggma.core import Sort, Store, StoreError @@ -343,7 +343,7 @@ 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 @@ -351,7 +351,20 @@ def __init__(self, uri: str, database: str, collection_name: str, **kwargs): 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 + self.collection_name = collection_name self.kwargs = kwargs self._collection = None From 91fdd8b82ea34d6e1adad27a2e74d1e56891cd7e Mon Sep 17 00:00:00 2001 From: jmmshn Date: Wed, 2 Dec 2020 14:55:02 -0500 Subject: [PATCH 3/4] added test with dummy uri --- tests/stores/test_mongolike.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/tests/stores/test_mongolike.py b/tests/stores/test_mongolike.py index 8f4d6553c..2c0472787 100644 --- a/tests/stores/test_mongolike.py +++ b/tests/stores/test_mongolike.py @@ -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 @@ -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") From 1d3e19115ba87ea83c566c3e779226635c0fd6eb Mon Sep 17 00:00:00 2001 From: jmmshn Date: Wed, 2 Dec 2020 17:46:26 -0500 Subject: [PATCH 4/4] no more connecting in init --- src/maggma/stores/mongolike.py | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/src/maggma/stores/mongolike.py b/src/maggma/stores/mongolike.py index bef51652a..92c897520 100644 --- a/src/maggma/stores/mongolike.py +++ b/src/maggma/stores/mongolike.py @@ -17,7 +17,7 @@ from monty.json import jsanitize, MSONable from monty.serialization import loadfn from pydash import get, has, set_ -from pymongo import MongoClient, ReplaceOne +from pymongo import MongoClient, ReplaceOne, uri_parser from pymongo.errors import OperationFailure, DocumentTooLarge, ConfigurationError from sshtunnel import SSHTunnelForwarder @@ -377,14 +377,12 @@ def __init__(self, uri: str, collection_name: str, database: str = None, **kwarg # 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 + d_uri = uri_parser.parse_uri(uri) + if d_uri["database"] is None: + raise ConfigurationError( + "If database name is not supplied, a database must be set in the uri" + ) + self.database = d_uri["database"] else: self.database = database