Skip to content

Commit 240e433

Browse files
WaVEVtimgraham
authored andcommitted
Create an dummy collection when check atlas support
1 parent aa9ee6e commit 240e433

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

django_mongodb_backend/features.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from django.db.backends.base.features import BaseDatabaseFeatures
22
from django.utils.functional import cached_property
3-
from pymongo.errors import OperationFailure
3+
from pymongo.errors import CollectionInvalid, OperationFailure
44

55

66
class DatabaseFeatures(BaseDatabaseFeatures):
@@ -550,13 +550,21 @@ def django_test_expected_failures(self):
550550
def is_mongodb_6_3(self):
551551
return self.connection.get_database_version() >= (6, 3)
552552

553-
@property
553+
@cached_property
554554
def supports_search_indexes(self):
555+
dummy_collection = "__null"
555556
try:
556-
# Try to execute an search indexes operation.
557-
self.connection.get_collection("__null").list_search_indexes()
557+
# Try to execute an search indexes operation over an existing collection.
558+
try:
559+
collection = self.connection.database.create_collection(dummy_collection)
560+
except CollectionInvalid:
561+
# If the collection exists, it will be removed after this operation.
562+
collection = self.connection.get_collection(dummy_collection)
563+
collection.list_search_indexes()
558564
except OperationFailure:
559-
# Operation fails then search indexes isn't supported
565+
# Operation fails then search indexes isn't supported.
560566
return False
561567
else:
562568
return True
569+
finally:
570+
collection.drop()

0 commit comments

Comments
 (0)