Skip to content

Commit fca63ba

Browse files
committed
Atlas index creation
1 parent 10185ab commit fca63ba

File tree

4 files changed

+45
-7
lines changed

4 files changed

+45
-7
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
@@ -101,7 +101,7 @@ def where_node_idx(self, compiler, connection):
101101
return mql
102102

103103

104-
def create_mongodb_index(self, model, schema_editor, field=None, unique=False, column_prefix=""):
104+
def create_mongodb_index(self, model, schema_editor, *, field=None, unique=False, column_prefix=""):
105105
from collections import defaultdict
106106

107107
if self.contains_expressions:
@@ -146,10 +146,14 @@ def __init__(self, *expressions, **kwargs):
146146
super().__init__(*expressions, **kwargs)
147147

148148
def create_mongodb_index(
149-
self, model, schema_editor, field=None, unique=False, column_prefix=""
149+
self, model, schema_editor, connection=None, field=None, unique=False, column_prefix=""
150150
):
151+
fields = {}
152+
for field_name, _ in self.fields_orders:
153+
field_ = model._meta.get_field(field_name)
154+
fields[field_name] = {"type": field_.db_type(connection)}
151155
return SearchIndexModel(
152-
definitions={},
156+
definition={"mappings": {"dynamic": False}, "fields": fields}, name=self.name
153157
)
154158

155159

django_mongodb_backend/schema.py

Lines changed: 31 additions & 2 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,6 +261,18 @@ 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
@@ -265,7 +282,7 @@ def add_index(
265282
)
266283
if idx:
267284
model = parent_model or model
268-
self.get_collection(model._meta.db_table).create_indexes([idx])
285+
self._add_index(idx, model)
269286

270287
def _add_composed_index(self, model, field_names, column_prefix="", parent_model=None):
271288
"""Add an index on the given list of field_names."""
@@ -279,11 +296,23 @@ def _add_field_index(self, model, field, *, column_prefix=""):
279296
index.name = self._create_index_name(model._meta.db_table, [column_prefix + field.column])
280297
self.add_index(model, index, field=field, column_prefix=column_prefix)
281298

299+
@singledispatchmethod
300+
def _remove_index(self, index, model):
301+
raise ValueError(f"{type(index)} isn't a supported index type")
302+
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, model):
309+
return self.get_collection(model._meta.db_table).drop_search_index(index.name)
310+
282311
@ignore_embedded_models
283312
def remove_index(self, model, index):
284313
if index.contains_expressions:
285314
return
286-
self.get_collection(model._meta.db_table).drop_index(index.name)
315+
self._remove_index(index, model)
287316

288317
def _remove_composed_index(
289318
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)