diff --git a/docs/changelog.rst b/docs/changelog.rst index 67be970ea..a683cc7f6 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -8,6 +8,7 @@ Development =========== - (Fill this out as you fix issues and develop your features). - Fix for uuidRepresentation not read when provided in URI #2741 +- Fix connection for multiple db in same cluster created duplicate MongoClient #2768 - Add option to user array_filters https://www.mongodb.com/docs/manual/reference/operator/update/positional-filtered/ #2769 - Fix combination of __raw__ and mongoengine syntax #2773 - Add tests against MongoDB 6.0 and MongoDB 7.0 in the pipeline @@ -22,6 +23,7 @@ Development - Add `text_score` argument on :meth:`~mongoengine.Document.search_text()` to allow text_score computation to be turned off as it interfer with natural returned documents order #2759 + Changes in 0.27.0 ================= - Update uuidRepresentation warnings with "unspecified" as the future default (instead of 'standard' previously advertised) #2739 diff --git a/mongoengine/connection.py b/mongoengine/connection.py index 27a2970f7..3c8293af7 100644 --- a/mongoengine/connection.py +++ b/mongoengine/connection.py @@ -399,7 +399,13 @@ def _clean_settings(settings_dict): # Only remove the name but it's important to # keep the username/password/authentication_source/authentication_mechanism # to identify if the connection could be shared (cfr https://github.com/MongoEngine/mongoengine/issues/2047) - return {k: v for k, v in settings_dict.items() if k != "name"} + _clean = {k: v for k, v in settings_dict.items() if k != "name"} + + # In case of multiple db on the same cluster + _clean["host"] = [ + i.replace(f'/{settings_dict["name"]}', "") for i in _clean["host"] + ] + return _clean cleaned_conn_settings = _clean_settings(connection_settings) for db_alias, connection_settings in connection_settings_bis: diff --git a/tests/test_connection.py b/tests/test_connection.py index c88f87c93..28fbdefb6 100644 --- a/tests/test_connection.py +++ b/tests/test_connection.py @@ -132,6 +132,10 @@ class History2(Document): connect("db1", alias="db1") connect("db2", alias="db2") + conn1 = get_connection("db2") + conn2 = get_connection("db2") + assert conn1 == conn2 + History.drop_collection() History1.drop_collection() History2.drop_collection() @@ -704,6 +708,21 @@ def test_connect_uri_uuidrepresentation_default_to_pythonlegacy(self): ) disconnect(rand) + def test_connect_deduplication(self): + connect(host="mongodb://localhost:27017/mongoenginetest_1", alias="test_1") + connect(host="mongodb://localhost:27017/mongoenginetest_2", alias="test_2") + + conn1 = get_connection("test_1") + conn2 = get_connection("test_2") + assert conn1 == conn2 + assert isinstance(conn1, pymongo.mongo_client.MongoClient) + + db1 = get_db("test_1") + db2 = get_db("test_2") + assert isinstance(db2, pymongo.database.Database) + assert db1.name == "mongoenginetest_1" + assert db2.name == "mongoenginetest_2" + if __name__ == "__main__": unittest.main()