:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(mongodb): make the no-collections validation error actionable (#68370)
Tom Owersc4c5121fee9e
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/mongodb/source.py b/products/warehouse_sources/backend/temporal/data_imports/sources/mongodb/source.pyindex ea56225ce6dc..7cc4a34477e2 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/mongodb/source.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/mongodb/source.py@@ -68,6 +68,16 @@ _MONGO_CONNECT_FAILED_MESSAGE = ("Could not connect to your MongoDB database. Check your connection string and credentials, then try again.")+# Connection succeeded but nothing importable came back. This is usually a wrong-database or+# permission problem rather than a genuinely empty database: a connection string ending in /admin+# or /test lands on an empty system database, and a user without read access sees no collections.+_MONGO_NO_COLLECTIONS_MESSAGE = (+ "PostHog connected to MongoDB but found no collections in the selected database. Check that "+ "your connection string points at the database that holds your data (a string ending in /admin "+ "or /test connects to an empty system database) or set the Database name field, and make sure "+ "your user has read access to that database's collections."+)+# Substrings pymongo embeds in ServerSelectionTimeoutError when the OS can't resolve the host._DNS_RESOLUTION_FAILURE_MARKERS = ("No address associated with hostname",@@ -209,7 +219,7 @@ class MongoDBSource(SimpleSource[MongoDBSourceConfig], ValidateDatabaseHostMixintry:collection_names = get_collection_names(config, team_id=team_id)if len(collection_names) == 0:- return False, "No collections found in database"+ return False, _MONGO_NO_COLLECTIONS_MESSAGEexcept OperationFailure as e:# pymongo's OperationFailure stringifies the full server response — clusterTime,# signature hashes, BSON ids — so str(e) must never be surfaced. Map the stable errordiff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/mongodb/test_database_name.py b/products/warehouse_sources/backend/temporal/data_imports/sources/mongodb/test_database_name.pyindex fd72b53b8180..c35ded731b2e 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/mongodb/test_database_name.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/mongodb/test_database_name.py@@ -13,6 +13,7 @@ from products.warehouse_sources.backend.temporal.data_imports.sources.mongodb.so_MONGO_AUTHENTICATION_FAILED_MESSAGE,_MONGO_CONNECT_FAILED_MESSAGE,_MONGO_HOST_UNRESOLVED_MESSAGE,+ _MONGO_NO_COLLECTIONS_MESSAGE,_MONGO_NOT_AUTHORIZED_MESSAGE,_MONGO_UNESCAPED_CREDENTIALS_MESSAGE,_MONGO_UNREACHABLE_MESSAGE,@@ -60,6 +61,18 @@ class TestMongoValidateCredentialsDatabaseName:assert ok is Trueassert err is None+ @patch("products.warehouse_sources.backend.temporal.data_imports.sources.mongodb.source.get_collection_names")+ def test_no_collections_returns_actionable_error(self, mock_get_collections):+ # Connecting to /admin or /test, or a user lacking read access, surfaces zero collections.+ # That must fail with guidance about the wrong database / missing access, not a terse message.+ mock_get_collections.return_value = []+ config = MongoDBSourceConfig.from_dict({"connection_string": _SRV_WITH_DB})++ ok, err = MongoDBSource().validate_credentials(config, team_id=1)++ assert ok is False+ assert err == _MONGO_NO_COLLECTIONS_MESSAGE+class TestMongoValidateCredentialsServerSelection:@pytest.mark.parametrize("marker", _DNS_RESOLUTION_FAILURE_MARKERS)