1+ from functools import singledispatchmethod
2+
13from django .db .backends .base .schema import BaseDatabaseSchemaEditor
24from django .db .models import Index , UniqueConstraint
5+ from pymongo .operations import IndexModel , SearchIndexModel
6+
7+ from django_mongodb_backend .indexes import AtlasSearchIndex
38
49from .fields import EmbeddedModelField
510from .query import wrap_database_errors
@@ -256,16 +261,33 @@ def alter_unique_together(
256261 model , constraint , parent_model = parent_model , column_prefix = column_prefix
257262 )
258263
264+ @singledispatchmethod
265+ def _add_index (self , index , model ):
266+ raise ValueError (f"{ type (index )} isn't a supported index type" )
267+
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+
259276 @ignore_embedded_models
260277 def add_index (
261278 self , model , index , * , field = None , unique = False , column_prefix = "" , parent_model = None
262279 ):
263280 idx = index .create_mongodb_index (
264- model , self , field = field , unique = unique , column_prefix = column_prefix
281+ model ,
282+ self ,
283+ field = field ,
284+ unique = unique ,
285+ column_prefix = column_prefix ,
286+ connection = self .connection ,
265287 )
266288 if idx :
267289 model = parent_model or model
268- self .get_collection ( model . _meta . db_table ). create_indexes ([ idx ] )
290+ self ._add_index ( idx , model )
269291
270292 def _add_composed_index (self , model , field_names , column_prefix = "" , parent_model = None ):
271293 """Add an index on the given list of field_names."""
@@ -279,11 +301,23 @@ def _add_field_index(self, model, field, *, column_prefix=""):
279301 index .name = self ._create_index_name (model ._meta .db_table , [column_prefix + field .column ])
280302 self .add_index (model , index , field = field , column_prefix = column_prefix )
281303
304+ @singledispatchmethod
305+ def _remove_index (self , index , model ):
306+ raise ValueError (f"{ type (index )} isn't a supported index type" )
307+
308+ @_remove_index .register
309+ def _ (self , index : Index , model ):
310+ return self .get_collection (model ._meta .db_table ).drop_index (index .name )
311+
312+ @_remove_index .register
313+ def _ (self , index : AtlasSearchIndex , model ):
314+ return self .get_collection (model ._meta .db_table ).drop_search_index (index .name )
315+
282316 @ignore_embedded_models
283317 def remove_index (self , model , index ):
284318 if index .contains_expressions :
285319 return
286- self .get_collection ( model . _meta . db_table ). drop_index ( index . name )
320+ self ._remove_index ( index , model )
287321
288322 def _remove_composed_index (
289323 self , model , field_names , constraint_kwargs , column_prefix = "" , parent_model = None
0 commit comments