Skip to content

Commit e491c19

Browse files
committed
Atlas index creation
1 parent 97ca627 commit e491c19

File tree

4 files changed

+51
-8
lines changed

4 files changed

+51
-8
lines changed

django_mongodb_backend/base.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,12 @@ def get_connection_params(self):
175175

176176
@async_unsafe
177177
def get_new_connection(self, conn_params):
178-
return MongoClient(**conn_params, driver=self._driver_info())
178+
# import ipdb
179+
# ipdb.set_trace()
180+
return MongoClient(
181+
"mongodb://127.0.0.1:27017/?directConnection=true", driver=self._driver_info()
182+
)
183+
# return MongoClient(**conn_params, driver=self._driver_info())
179184

180185
def _driver_info(self):
181186
if not os.environ.get("RUNNING_DJANGOS_TEST_SUITE"):

django_mongodb_backend/indexes.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def where_node_idx(self, compiler, connection):
6060
return mql
6161

6262

63-
def create_mongodb_index(self, model, schema_editor, field=None, unique=False, column_prefix=""):
63+
def create_mongodb_index(self, model, schema_editor, *, field=None, unique=False, column_prefix=""):
6464
from collections import defaultdict
6565

6666
if self.contains_expressions:
@@ -105,10 +105,14 @@ def __init__(self, *expressions, **kwargs):
105105
super().__init__(*expressions, **kwargs)
106106

107107
def create_mongodb_index(
108-
self, model, schema_editor, field=None, unique=False, column_prefix=""
108+
self, model, schema_editor, connection=None, field=None, unique=False, column_prefix=""
109109
):
110+
fields = {}
111+
for field_name, _ in self.fields_orders:
112+
field_ = model._meta.get_field(field_name)
113+
fields[field_name] = {"type": field_.db_type(connection)}
110114
return SearchIndexModel(
111-
definitions={},
115+
definition={"mappings": {"dynamic": False}, "fields": fields}, name=self.name
112116
)
113117

114118

django_mongodb_backend/schema.py

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1+
from functools import singledispatchmethod
2+
13
from django.db.backends.base.schema import BaseDatabaseSchemaEditor
24
from django.db.models import Index, UniqueConstraint
5+
from pymongo.operations import IndexModel, SearchIndexModel
6+
7+
from django_mongodb_backend.indexes import AtlasSearchIndex
38

49
from .fields import EmbeddedModelField
510
from .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

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
django>=5.1,<5.2
2-
pymongo>=4.6,<5.0
2+
pymongo>=4.11,<5.0

0 commit comments

Comments
 (0)