Skip to content

Commit 853f5b5

Browse files
authored
Test and fix for unique index lost when renaming M2M field (#88)
* Fix ambiguous issue number references from previous fork Add absolute links in place of #NN which are no longer valid in this repo. * TDD: test which fails due to mssql-django/issues/86 * Don't drop unique indexes before M2M rename This method override was added by: ESSolutions/django-mssql-backend@08c2721 meaning that it dropped (but did not re-instate) any unique index on the M2M through table, before renaming that table. However MSSQL does not seem to require this, so rather than trying to re-create the correct indexes afterwards just don't drop them unnecessarily in the first place.
1 parent 8738904 commit 853f5b5

13 files changed

+100
-31
lines changed

mssql/schema.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -225,14 +225,6 @@ def _model_indexes_sql(self, model):
225225
output.append(index.create_sql(model, self))
226226
return output
227227

228-
def _alter_many_to_many(self, model, old_field, new_field, strict):
229-
"""Alter M2Ms to repoint their to= endpoints."""
230-
231-
for idx in self._constraint_names(old_field.remote_field.through, index=True, unique=True):
232-
self.execute(self.sql_delete_index % {'name': idx, 'table': old_field.remote_field.through._meta.db_table})
233-
234-
return super()._alter_many_to_many(model, old_field, new_field, strict)
235-
236228
def _db_table_constraint_names(self, db_table, column_names=None, unique=None,
237229
primary_key=None, index=None, foreign_key=None,
238230
check=None, type_=None, exclude=None):

testapp/migrations/0002_test_unique_nullable_part1.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class Migration(migrations.Migration):
88
]
99

1010
operations = [
11-
# Issue #38 test prep
11+
# Prep test for issue https://github.com/ESSolutions/django-mssql-backend/issues/38
1212
# Create with a field that is unique *and* nullable so it is implemented with a filtered unique index.
1313
migrations.CreateModel(
1414
name='TestUniqueNullableModel',

testapp/migrations/0003_test_unique_nullable_part2.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class Migration(migrations.Migration):
88
]
99

1010
operations = [
11-
# Issue #38 test
11+
# Run test for issue https://github.com/ESSolutions/django-mssql-backend/issues/38
1212
# Now remove the null=True to check this transition is correctly handled.
1313
migrations.AlterField(
1414
model_name='testuniquenullablemodel',

testapp/migrations/0004_test_issue45_unique_type_change_part1.py renamed to testapp/migrations/0004_test_unique_type_change_part1.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class Migration(migrations.Migration):
77
('testapp', '0003_test_unique_nullable_part2'),
88
]
99

10-
# Issue #45 test prep
10+
# Prep test for issue https://github.com/ESSolutions/django-mssql-backend/issues/45
1111
operations = [
1212
# for case 1:
1313
migrations.AddField(

testapp/migrations/0005_test_issue45_unique_type_change_part2.py renamed to testapp/migrations/0005_test_unique_type_change_part2.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
class Migration(migrations.Migration):
55

66
dependencies = [
7-
('testapp', '0004_test_issue45_unique_type_change_part1'),
7+
('testapp', '0004_test_unique_type_change_part1'),
88
]
99

10-
# Issue #45 test
10+
# Run test for issue https://github.com/ESSolutions/django-mssql-backend/issues/45
1111
operations = [
1212
# Case 1: changing max_length changes the column type - the filtered UNIQUE INDEX which implements
1313
# the nullable unique constraint, should be correctly reinstated after this change of column type

testapp/migrations/0006_test_remove_onetoone_field_part1.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
class Migration(migrations.Migration):
88

99
dependencies = [
10-
('testapp', '0005_test_issue45_unique_type_change_part2'),
10+
('testapp', '0005_test_unique_type_change_part2'),
1111
]
1212

1313
operations = [

testapp/migrations/0012_test_indexes_retained_part1.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class Migration(migrations.Migration):
77
('testapp', '0011_test_unique_constraints'),
88
]
99

10-
# Issue #58 test prep
10+
# Prep test for issue https://github.com/ESSolutions/django-mssql-backend/issues/58
1111
operations = [
1212
migrations.CreateModel(
1313
name='TestIndexesRetained',

testapp/migrations/0013_test_indexes_retained_part2.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ class Migration(migrations.Migration):
77
('testapp', '0012_test_indexes_retained_part1'),
88
]
99

10-
# Issue #58 test operations which should leave index intact
10+
# Run test for issue https://github.com/ESSolutions/django-mssql-backend/issues/58
11+
# where the following operations should leave indexes intact
1112
operations = [
1213
migrations.AlterField(
1314
model_name='testindexesretained',
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from django.db import migrations, models
2+
3+
4+
class Migration(migrations.Migration):
5+
6+
dependencies = [
7+
('testapp', '0013_test_indexes_retained_part2'),
8+
]
9+
10+
operations = [
11+
# Prep test for issue https://github.com/microsoft/mssql-django/issues/86
12+
migrations.CreateModel(
13+
name='M2MOtherModel',
14+
fields=[
15+
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
16+
('name', models.CharField(max_length=10)),
17+
],
18+
),
19+
migrations.CreateModel(
20+
name='TestRenameManyToManyFieldModel',
21+
fields=[
22+
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
23+
('others', models.ManyToManyField(to='testapp.M2MOtherModel')),
24+
],
25+
),
26+
]
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from django.db import migrations, models
2+
3+
4+
class Migration(migrations.Migration):
5+
6+
dependencies = [
7+
('testapp', '0014_test_rename_m2mfield_part1'),
8+
]
9+
10+
operations = [
11+
# Run test for issue https://github.com/microsoft/mssql-django/issues/86
12+
# Must be in a separate migration so that the unique index was created
13+
# (deferred after the previous migration) before we do the rename.
14+
migrations.RenameField(
15+
model_name='testrenamemanytomanyfieldmodel',
16+
old_name='others',
17+
new_name='others_renamed',
18+
),
19+
]

0 commit comments

Comments
 (0)