Skip to content

Commit 34dc035

Browse files
WaVEVtimgraham
authored andcommitted
Create an dummy collection when check atlas support
1 parent 50d9b10 commit 34dc035

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):
@@ -627,13 +627,21 @@ def django_test_expected_failures(self):
627627
def is_mongodb_6_3(self):
628628
return self.connection.get_database_version() >= (6, 3)
629629

630-
@property
630+
@cached_property
631631
def supports_search_indexes(self):
632+
dummy_collection = "__null"
632633
try:
633-
# Try to execute an search indexes operation.
634-
self.connection.get_collection("__null").list_search_indexes()
634+
# Try to execute an search indexes operation over an existing collection.
635+
try:
636+
collection = self.connection.database.create_collection(dummy_collection)
637+
except CollectionInvalid:
638+
# If the collection exists, it will be removed after this operation.
639+
collection = self.connection.get_collection(dummy_collection)
640+
collection.list_search_indexes()
635641
except OperationFailure:
636-
# Operation fails then search indexes isn't supported
642+
# Operation fails then search indexes isn't supported.
637643
return False
638644
else:
639645
return True
646+
finally:
647+
collection.drop()

0 commit comments

Comments
 (0)