Skip to content

Commit d784806

Browse files
committed
Drop search index if atlas is supported
1 parent 3b47b30 commit d784806

File tree

2 files changed

+12
-57
lines changed

2 files changed

+12
-57
lines changed

django_mongodb_backend/schema.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -290,13 +290,8 @@ def remove_index(self, model, index):
290290
if index.contains_expressions:
291291
return
292292
if isinstance(index, SearchIndex | VectorSearchIndex):
293-
# Drop the index if it exists, particularly if it may not have been created previously
294-
# due to lack of Atlas search support, but now the database supports it.
295-
constraints = self.connection.introspection.get_constraints(
296-
cursor=None,
297-
table_name=model._meta.db_table,
298-
)
299-
if self.connection.features.supports_atlas_search and index.name in constraints:
293+
# Drop the index if it is supported.
294+
if self.connection.features.supports_atlas_search:
300295
self.get_collection(model._meta.db_table).drop_search_index(index.name)
301296
else:
302297
self.get_collection(model._meta.db_table).drop_index(index.name)

tests/indexes_/test_atlas_indexes.py

Lines changed: 10 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
from unittest import mock
2-
31
from django.db import connection
42
from django.test import TestCase, skipIfDBFeature, skipUnlessDBFeature
53

@@ -8,10 +6,7 @@
86
from .models import Article
97

108

11-
class SearchIndexTests(TestCase):
12-
# Tests for creating, validating, and removing search indexes using Django's schema editor.
13-
available_apps = None
14-
9+
class TestMixin:
1510
def assertAddRemoveIndex(self, editor, model, index):
1611
editor.add_index(index=index, model=model)
1712
self.assertIn(
@@ -30,6 +25,10 @@ def assertAddRemoveIndex(self, editor, model, index):
3025
),
3126
)
3227

28+
29+
class SearchIndexTests(TestMixin, TestCase):
30+
"""Tests for creating, validating, and removing search indexes using Django's schema editor."""
31+
3332
@skipUnlessDBFeature("supports_atlas_search")
3433
def test_simple(self):
3534
with connection.schema_editor() as editor:
@@ -40,17 +39,6 @@ def test_simple(self):
4039
editor.add_index(index=index, model=Article)
4140
self.assertAddRemoveIndex(editor, Article, index)
4241

43-
def test_drop_non_existing_index(self):
44-
with connection.schema_editor() as editor:
45-
index = SearchIndex(
46-
name="recent_article_idx",
47-
fields=["number"],
48-
)
49-
editor.get_collection = mock.MagicMock()
50-
editor.remove_index(index=index, model=Article)
51-
# Verify that the collection was not accessed
52-
editor.get_collection.assert_not_called()
53-
5442
@skipIfDBFeature("supports_atlas_search")
5543
def test_index_not_created(self):
5644
with connection.schema_editor() as editor:
@@ -110,39 +98,11 @@ def test_multiple_fields(self):
11098
self.assertAddRemoveIndex(editor, Article, index)
11199

112100

113-
class VectorSearchIndexTests(TestCase):
114-
# Tests for creating, validating, and removing vector search indexes
115-
# using Django's schema editor.
116-
available_apps = None
117-
118-
def assertAddRemoveIndex(self, editor, model, index):
119-
editor.add_index(index=index, model=model)
120-
self.assertIn(
121-
index.name,
122-
connection.introspection.get_constraints(
123-
cursor=None,
124-
table_name=model._meta.db_table,
125-
),
126-
)
127-
editor.remove_index(index=index, model=model)
128-
self.assertNotIn(
129-
index.name,
130-
connection.introspection.get_constraints(
131-
cursor=None,
132-
table_name=model._meta.db_table,
133-
),
134-
)
135-
136-
def test_drop_non_existing_index(self):
137-
with connection.schema_editor() as editor:
138-
index = SearchIndex(
139-
name="recent_article_idx",
140-
fields=["number"],
141-
)
142-
editor.get_collection = mock.MagicMock()
143-
editor.remove_index(index=index, model=Article)
144-
# Verify that the collection was not accessed
145-
editor.get_collection.assert_not_called()
101+
class VectorSearchIndexTests(TestMixin, TestCase):
102+
"""
103+
Tests for creating, validating, and removing vector search indexes
104+
using Django's schema editor.
105+
"""
146106

147107
@skipUnlessDBFeature("supports_atlas_search")
148108
def test_deconstruct_default_similarity(self):

0 commit comments

Comments
 (0)