Skip to content

Commit 92caf14

Browse files
committed
Create an dummy collection when check atlas support
1 parent f3db7b0 commit 92caf14

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

django_mongodb_backend/features.py

+13-5
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):
@@ -613,13 +613,21 @@ def django_test_expected_failures(self):
613613
def is_mongodb_6_3(self):
614614
return self.connection.get_database_version() >= (6, 3)
615615

616-
@property
616+
@cached_property
617617
def supports_search_indexes(self):
618+
dummy_collection = "__null"
618619
try:
619-
# Try to execute an search indexes operation.
620-
self.connection.get_collection("__null").list_search_indexes()
620+
# Try to execute an search indexes operation over an existing collection.
621+
try:
622+
collection = self.connection.database.create_collection(dummy_collection)
623+
except CollectionInvalid:
624+
# If the collection exists, it will be removed after this operation.
625+
collection = self.connection.get_collection(dummy_collection)
626+
collection.list_search_indexes()
621627
except OperationFailure:
622-
# Operation fails then search indexes isn't supported
628+
# Operation fails then search indexes isn't supported.
623629
return False
624630
else:
625631
return True
632+
finally:
633+
collection.drop()

0 commit comments

Comments
 (0)