Skip to content

Commit 456af8f

Browse files
WaVEVtimgraham
authored andcommitted
Add supports_search_indexes flag
1 parent 173c753 commit 456af8f

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):
@@ -548,3 +549,12 @@ def django_test_expected_failures(self):
548549
@cached_property
549550
def is_mongodb_6_3(self):
550551
return self.connection.get_database_version() >= (6, 3)
552+
553+
@cached_property
554+
def supports_search_indexes(self):
555+
try:
556+
self.connection.get_collection("__null").list_search_indexes()
557+
except OperationFailure:
558+
return False
559+
else:
560+
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)