Skip to content

Commit 4fd0895

Browse files
committed
implement SchemaEditor.alter_index_together()
1 parent f6d48c2 commit 4fd0895

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

django_mongodb/features.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,6 @@ class DatabaseFeatures(BaseDatabaseFeatures):
8484
# AlterField (unique)
8585
"schema.tests.SchemaTests.test_unique",
8686
"schema.tests.SchemaTests.test_unique_and_reverse_m2m",
87-
# alter_index_together
88-
"migrations.test_operations.OperationTests.test_alter_index_together",
89-
"schema.tests.SchemaTests.test_index_together",
9087
# alter_unique_together
9188
"migrations.test_operations.OperationTests.test_alter_unique_together",
9289
"schema.tests.SchemaTests.test_unique_together",

django_mongodb/schema.py

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,14 @@ def remove_field(self, model, field):
100100
self._remove_field_index(model, field)
101101

102102
def alter_index_together(self, model, old_index_together, new_index_together):
103-
pass
103+
olds = {tuple(fields) for fields in old_index_together}
104+
news = {tuple(fields) for fields in new_index_together}
105+
# Deleted indexes
106+
for field_names in olds.difference(news):
107+
self._remove_composed_index(model, field_names, {"index": True, "unique": False})
108+
# Created indexes
109+
for field_names in news.difference(olds):
110+
self._add_composed_index(model, field_names)
104111

105112
def alter_unique_together(self, model, old_unique_together, new_unique_together):
106113
pass
@@ -138,6 +145,30 @@ def remove_index(self, model, index):
138145
return
139146
self.get_collection(model._meta.db_table).drop_index(index.name)
140147

148+
def _remove_composed_index(self, model, field_names, constraint_kwargs):
149+
"""
150+
Remove the index on the given list of field_names created by
151+
index/unique_together, depending on constraint_kwargs.
152+
"""
153+
meta_constraint_names = {constraint.name for constraint in model._meta.constraints}
154+
meta_index_names = {constraint.name for constraint in model._meta.indexes}
155+
columns = [model._meta.get_field(field).column for field in field_names]
156+
constraint_names = self._constraint_names(
157+
model,
158+
columns,
159+
exclude=meta_constraint_names | meta_index_names,
160+
**constraint_kwargs,
161+
)
162+
if len(constraint_names) != 1:
163+
num_found = len(constraint_names)
164+
columns_str = ", ".join(columns)
165+
raise ValueError(
166+
f"Found wrong number ({num_found}) of constraints for "
167+
f"{model._meta.db_table}({columns_str})."
168+
)
169+
collection = self.get_collection(model._meta.db_table)
170+
collection.drop_index(constraint_names[0])
171+
141172
def _remove_field_index(self, model, field):
142173
"""Remove a field's db_index=True index."""
143174
collection = self.get_collection(model._meta.db_table)

0 commit comments

Comments
 (0)