Skip to content

Commit 4b0172c

Browse files
committed
add index creation/deletion support to SchemaEditor.alter_field()
1 parent 84a23d7 commit 4b0172c

File tree

2 files changed

+39
-5
lines changed

2 files changed

+39
-5
lines changed

django_mongodb/features.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,16 +79,12 @@ class DatabaseFeatures(BaseDatabaseFeatures):
7979
# AddField
8080
"schema.tests.SchemaTests.test_add_unique_charfield",
8181
# AlterField
82-
"schema.tests.SchemaTests.test_alter_field_add_index_to_integerfield",
8382
"schema.tests.SchemaTests.test_alter_field_fk_to_o2o",
8483
"schema.tests.SchemaTests.test_alter_field_o2o_keeps_unique",
8584
"schema.tests.SchemaTests.test_alter_field_o2o_to_fk",
8685
"schema.tests.SchemaTests.test_alter_int_pk_to_int_unique",
87-
# AlterField (db_index)
88-
"schema.tests.SchemaTests.test_alter_renames_index",
89-
"schema.tests.SchemaTests.test_indexes",
90-
"schema.tests.SchemaTests.test_remove_db_index_doesnt_remove_custom_indexes",
9186
# AlterField (unique)
87+
"schema.tests.SchemaTests.test_indexes",
9288
"schema.tests.SchemaTests.test_unique",
9389
"schema.tests.SchemaTests.test_unique_and_reverse_m2m",
9490
# alter_unique_together

django_mongodb/schema.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,31 @@ def _alter_field(
6666
strict=False,
6767
):
6868
collection = self.connection.database[model._meta.db_table]
69+
# Removed an index?
70+
if self._field_should_be_indexed(model, old_field) and not self._field_should_be_indexed(
71+
model, new_field
72+
):
73+
self._remove_field_index(model, old_field)
6974
# Have they renamed the column?
7075
if old_field.column != new_field.column:
7176
collection.update_many({}, {"$rename": {old_field.column: new_field.column}})
77+
# Move index to the new field, if needed.
78+
if self._field_should_be_indexed(model, old_field) and self._field_should_be_indexed(
79+
model, new_field
80+
):
81+
self._remove_field_index(model, old_field)
82+
self._add_field_index(model, new_field)
7283
# Replace NULL with the field default if the field and was changed from
7384
# NULL to NOT NULL.
7485
if new_field.has_default() and old_field.null and not new_field.null:
7586
column = new_field.column
7687
default = self.effective_default(new_field)
7788
collection.update_many({column: {"$eq": None}}, [{"$set": {column: default}}])
89+
# Added an index?
90+
if not self._field_should_be_indexed(model, old_field) and self._field_should_be_indexed(
91+
model, new_field
92+
):
93+
self._add_field_index(model, new_field)
7894

7995
def remove_field(self, model, field):
8096
# Remove implicit M2M tables.
@@ -156,6 +172,28 @@ def _remove_composed_index(self, model, field_names, constraint_kwargs):
156172
collection = self.connection.database[model._meta.db_table]
157173
collection.drop_index(constraint_names[0])
158174

175+
def _remove_field_index(self, model, field):
176+
"""Remove a field's db_index=True index."""
177+
collection = self.connection.database[model._meta.db_table]
178+
# Find the index for this field
179+
meta_index_names = {index.name for index in model._meta.indexes}
180+
# Retrieve only BTREE indexes since this is what's created with
181+
# db_index=True.
182+
index_names = self._constraint_names(
183+
model,
184+
[field.column],
185+
index=True,
186+
type_=Index.suffix,
187+
exclude=meta_index_names,
188+
)
189+
if not index_names:
190+
raise ValueError(f"Index not found for {model._meta.db_table}.{field.column}.")
191+
for index_name in index_names:
192+
# The only way to check if an index was created with
193+
# db_index=True or with Index(['field'], name='foo')
194+
# is to look at its name (refs #28053).
195+
collection.drop_index(index_name)
196+
159197
def add_constraint(self, model, constraint):
160198
pass
161199

0 commit comments

Comments
 (0)