Skip to content

Commit 3cd1d87

Browse files
committed
Add supports_search_indexes flag
1 parent e0fb949 commit 3cd1d87

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

django_mongodb_backend/features.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +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
34

45

56
class DatabaseFeatures(BaseDatabaseFeatures):
@@ -611,3 +612,12 @@ def django_test_expected_failures(self):
611612
@cached_property
612613
def is_mongodb_6_3(self):
613614
return self.connection.get_database_version() >= (6, 3)
615+
616+
@cached_property
617+
def supports_search_indexes(self):
618+
try:
619+
self.connection.get_collection("__null").list_search_indexes()
620+
except OperationFailure:
621+
return False
622+
else:
623+
return True

django_mongodb_backend/introspection.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ def _get_index_info(self, table_name):
3434
return constraints
3535

3636
def _get_search_index_info(self, table_name):
37+
if not self.connection.features.supports_search_indexes:
38+
return {}
3739
constraints = {}
3840
indexes = self.connection.get_collection(table_name).list_search_indexes()
3941
for details in indexes:

tests/indexes_/test_atlas_indexes.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
from django.db import connection
2-
from django.test import TestCase
2+
from django.test import TestCase, skipUnlessDBFeature
33

44
from django_mongodb_backend.indexes import SearchIndex, VectorSearchIndex
55

66
from .models import Article
77

88

9+
@skipUnlessDBFeature("supports_search_indexes")
910
class SearchIndexTests(TestCase):
1011
# Tests for creating, validating, and removing search indexes using Django's schema editor.
1112
available_apps = None
@@ -79,6 +80,7 @@ def test_multiple_fields(self):
7980
self.assertAddRemoveIndex(editor, Article, index)
8081

8182

83+
@skipUnlessDBFeature("supports_search_indexes")
8284
class VectorSearchIndexTests(TestCase):
8385
# Tests for creating, validating, and removing vector search indexes
8486
# using Django's schema editor.

0 commit comments

Comments
 (0)