Skip to content

Commit 6c83067

Browse files
committed
implement SchemaEditor.add/remove_index() and creating indexes in create_model()
1 parent e2fbb21 commit 6c83067

File tree

3 files changed

+67
-21
lines changed

3 files changed

+67
-21
lines changed

django_mongodb/features.py

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -83,23 +83,14 @@ class DatabaseFeatures(BaseDatabaseFeatures):
8383
"backends.tests.ThreadTests.test_pass_connection_between_threads",
8484
"backends.tests.ThreadTests.test_closing_non_shared_connections",
8585
"backends.tests.ThreadTests.test_default_connection_thread_local",
86-
# Add/RemoveIndex
87-
"schema.tests.SchemaTests.test_add_remove_index",
88-
"schema.tests.SchemaTests.test_composed_desc_index_with_fk",
89-
"schema.tests.SchemaTests.test_composed_index_with_fk",
90-
"schema.tests.SchemaTests.test_create_index_together",
91-
"schema.tests.SchemaTests.test_order_index",
92-
"schema.tests.SchemaTests.test_text_field_with_db_index",
9386
# AlterField
9487
"schema.tests.SchemaTests.test_alter_auto_field_to_integer_field",
9588
"schema.tests.SchemaTests.test_alter_field_add_index_to_integerfield",
9689
"schema.tests.SchemaTests.test_alter_field_default_dropped",
97-
"schema.tests.SchemaTests.test_alter_field_fk_keeps_index",
9890
"schema.tests.SchemaTests.test_alter_field_fk_to_o2o",
9991
"schema.tests.SchemaTests.test_alter_field_o2o_keeps_unique",
10092
"schema.tests.SchemaTests.test_alter_field_o2o_to_fk",
10193
"schema.tests.SchemaTests.test_alter_int_pk_to_int_unique",
102-
"schema.tests.SchemaTests.test_alter_not_unique_field_to_primary_key",
10394
"schema.tests.SchemaTests.test_alter_null_to_not_null",
10495
"schema.tests.SchemaTests.test_alter_primary_key_the_same_name",
10596
"schema.tests.SchemaTests.test_autofield_to_o2o",
@@ -118,6 +109,8 @@ class DatabaseFeatures(BaseDatabaseFeatures):
118109
"schema.tests.SchemaTests.test_composed_constraint_with_fk",
119110
"schema.tests.SchemaTests.test_remove_ignored_unique_constraint_not_create_fk_index",
120111
"schema.tests.SchemaTests.test_unique_constraint",
112+
# constraints not fully implemented.
113+
"introspection.tests.IntrospectionTests.test_get_constraints",
121114
}
122115
# $bitAnd, #bitOr, and $bitXor are new in MongoDB 6.3.
123116
_django_test_expected_failures_bitwise = {
@@ -204,6 +197,7 @@ def django_test_expected_failures(self):
204197
"get_or_create.tests.GetOrCreateThroughManyToMany.test_something",
205198
"get_or_create.tests.UpdateOrCreateTests.test_manual_primary_key_test",
206199
"get_or_create.tests.UpdateOrCreateTestsWithManualPKs.test_create_with_duplicate_primary_key",
200+
"introspection.tests.IntrospectionTests.test_get_constraints_unique_indexes_orders",
207201
"model_fields.test_filefield.FileFieldTests.test_unique_when_same_filename",
208202
"one_to_one.tests.OneToOneTests.test_multiple_o2o",
209203
"queries.test_bulk_update.BulkUpdateTests.test_database_routing_batch_atomicity",
@@ -584,14 +578,8 @@ def django_test_expected_failures(self):
584578
"introspection.tests.IntrospectionTests.test_get_table_description_types",
585579
"introspection.tests.IntrospectionTests.test_smallautofield",
586580
},
587-
"DatabaseIntrospection.get_constraints() not implemented.": {
588-
"introspection.tests.IntrospectionTests.test_get_constraints",
589-
"introspection.tests.IntrospectionTests.test_get_constraints_index_types",
590-
"introspection.tests.IntrospectionTests.test_get_constraints_indexes_orders",
591-
"introspection.tests.IntrospectionTests.test_get_constraints_unique_indexes_orders",
592-
"introspection.tests.IntrospectionTests.test_get_primary_key_column",
593-
},
594581
"MongoDB can't introspect primary key.": {
582+
"introspection.tests.IntrospectionTests.test_get_primary_key_column",
595583
"schema.tests.SchemaTests.test_primary_key",
596584
},
597585
"Known issue querying JSONField.": {

django_mongodb/introspection.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,31 @@
11
from django.db.backends.base.introspection import BaseDatabaseIntrospection
2+
from django.db.models import Index
23

34

45
class DatabaseIntrospection(BaseDatabaseIntrospection):
6+
ORDER_DIR = {1: "ASC", -1: "DESC"}
7+
58
def table_names(self, cursor=None, include_views=False):
69
return sorted([x["name"] for x in self.connection.database.list_collections()])
10+
11+
def get_constraints(self, cursor, table_name):
12+
indexes = self.connection.database[table_name].index_information()
13+
constraints = {}
14+
for name, details in indexes.items():
15+
# Remove underscore prefix from "_id" columns in primary key index.
16+
if is_primary_key := name == "_id_":
17+
name = "id"
18+
details["key"] = [("id", 1)]
19+
constraints[name] = {
20+
"check": False,
21+
"columns": [field for field, order in details["key"]],
22+
"definition": None,
23+
"foreign_key": None,
24+
"index": True,
25+
"orders": [self.ORDER_DIR[order] for field, order in details["key"]],
26+
"primary_key": is_primary_key,
27+
"type": Index.suffix,
28+
"unique": details.get("unique", False),
29+
"options": {},
30+
}
31+
return constraints

django_mongodb/schema.py

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,39 @@
11
from django.db.backends.base.schema import BaseDatabaseSchemaEditor
2+
from django.db.models import Index
3+
from pymongo.operations import IndexModel
24

35

46
class DatabaseSchemaEditor(BaseDatabaseSchemaEditor):
57
def create_model(self, model):
68
self.connection.database.create_collection(model._meta.db_table)
9+
self._create_model_indexes(model)
710
# Make implicit M2M tables.
811
for field in model._meta.local_many_to_many:
912
if field.remote_field.through._meta.auto_created:
1013
self.create_model(field.remote_field.through)
1114

15+
def _create_model_indexes(self, model):
16+
"""
17+
Create all indexes (field indexes, index_together, Meta.indexes) for
18+
the specified model.
19+
"""
20+
if not model._meta.managed or model._meta.proxy or model._meta.swapped:
21+
return
22+
# Field indexes
23+
for field in model._meta.local_fields:
24+
if self._field_should_be_indexed(model, field):
25+
index = Index(fields=[field.name])
26+
index.set_name_with_model(model)
27+
self.add_index(model, index)
28+
# Meta.index_together (RemovedInDjango51Warning)
29+
for field_names in model._meta.index_together:
30+
index = Index(fields=field_names)
31+
index.set_name_with_model(model)
32+
self.add_index(model, index)
33+
# Meta.indexes
34+
for index in model._meta.indexes:
35+
self.add_index(model, index)
36+
1237
def delete_model(self, model):
1338
# Delete implicit M2m tables.
1439
for field in model._meta.local_many_to_many:
@@ -60,13 +85,21 @@ def alter_unique_together(self, model, old_unique_together, new_unique_together)
6085
pass
6186

6287
def add_index(self, model, index):
63-
pass
64-
65-
def rename_index(self, model, old_index, new_index):
66-
pass
88+
if index.contains_expressions:
89+
return
90+
idx = IndexModel(
91+
[
92+
(model._meta.get_field(field_name).column, 1 if order == "" else -1)
93+
for field_name, order in index.fields_orders
94+
],
95+
name=index.name,
96+
)
97+
self.connection.database[model._meta.db_table].create_indexes([idx])
6798

6899
def remove_index(self, model, index):
69-
pass
100+
if index.contains_expressions:
101+
return
102+
self.connection.database[model._meta.db_table].drop_index(index.name)
70103

71104
def add_constraint(self, model, constraint):
72105
pass

0 commit comments

Comments
 (0)