Skip to content

Commit 6fee641

Browse files
committed
Refactor: remove singledispatchmethod
1 parent 3b80117 commit 6fee641

File tree

1 file changed

+8
-20
lines changed

1 file changed

+8
-20
lines changed

django_mongodb_backend/schema.py

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
from functools import singledispatchmethod
2-
31
from django.db.backends.base.schema import BaseDatabaseSchemaEditor
42
from django.db.models import Index, UniqueConstraint
53
from pymongo.operations import IndexModel, SearchIndexModel
@@ -261,18 +259,13 @@ def alter_unique_together(
261259
model, constraint, parent_model=parent_model, column_prefix=column_prefix
262260
)
263261

264-
@singledispatchmethod
265262
def _add_index(self, index, model):
263+
if isinstance(index, SearchIndexModel):
264+
return self.get_collection(model._meta.db_table).create_search_index(index)
265+
if isinstance(index, IndexModel):
266+
return self.get_collection(model._meta.db_table).create_indexes([index])
266267
raise ValueError(f"{type(index)} isn't a supported index type")
267268

268-
@_add_index.register
269-
def _(self, index: IndexModel, model):
270-
return self.get_collection(model._meta.db_table).create_indexes([index])
271-
272-
@_add_index.register
273-
def _(self, index: SearchIndexModel, model):
274-
return self.get_collection(model._meta.db_table).create_search_index(index)
275-
276269
@ignore_embedded_models
277270
def add_index(
278271
self, model, index, *, field=None, unique=False, column_prefix="", parent_model=None
@@ -296,18 +289,13 @@ def _add_field_index(self, model, field, *, column_prefix=""):
296289
index.name = self._create_index_name(model._meta.db_table, [column_prefix + field.column])
297290
self.add_index(model, index, field=field, column_prefix=column_prefix)
298291

299-
@singledispatchmethod
300292
def _remove_index(self, index, model):
293+
if isinstance(index, AtlasSearchIndex | AtlasVectorSearchIndex):
294+
return self.get_collection(model._meta.db_table).drop_search_index(index.name)
295+
if isinstance(index, Index):
296+
return self.get_collection(model._meta.db_table).drop_index(index.name)
301297
raise ValueError(f"{type(index)} isn't a supported index type")
302298

303-
@_remove_index.register
304-
def _(self, index: Index, model):
305-
return self.get_collection(model._meta.db_table).drop_index(index.name)
306-
307-
@_remove_index.register
308-
def _(self, index: AtlasSearchIndex | AtlasVectorSearchIndex, model):
309-
return self.get_collection(model._meta.db_table).drop_search_index(index.name)
310-
311299
@ignore_embedded_models
312300
def remove_index(self, model, index):
313301
if index.contains_expressions:

0 commit comments

Comments
 (0)