|
1 | 1 | import logging |
2 | 2 |
|
3 | 3 | import django.db |
| 4 | +from django import VERSION |
4 | 5 | from django.apps import apps |
| 6 | +from django.db import models |
5 | 7 | from django.db.models import UniqueConstraint |
| 8 | +from django.db.utils import DEFAULT_DB_ALIAS, ConnectionHandler, ProgrammingError |
6 | 9 | from django.test import TestCase |
7 | 10 |
|
8 | 11 | from ..models import ( |
9 | | - TestIndexesRetainedRenamed |
| 12 | + TestIndexesRetainedRenamed, |
| 13 | + Choice, |
| 14 | + Question, |
10 | 15 | ) |
11 | 16 |
|
| 17 | +connections = ConnectionHandler() |
| 18 | + |
| 19 | +if (VERSION >= (3, 2)): |
| 20 | + from django.utils.connection import ConnectionProxy |
| 21 | + connection = ConnectionProxy(connections, DEFAULT_DB_ALIAS) |
| 22 | +else: |
| 23 | + from django.db import DefaultConnectionProxy |
| 24 | + connection = DefaultConnectionProxy() |
12 | 25 |
|
13 | 26 | logger = logging.getLogger('mssql.tests') |
14 | 27 |
|
@@ -139,3 +152,33 @@ def test_correct_indexes_exist(self): |
139 | 152 | '\n'.join(str(i) for i in col_indexes), |
140 | 153 | ) |
141 | 154 | logger.debug(' Found %s index(es) as expected', len(col_indexes)) |
| 155 | + |
| 156 | + |
| 157 | +class TestIndexesBeingDropped(TestCase): |
| 158 | + |
| 159 | + def test_unique_index_dropped(self): |
| 160 | + """ |
| 161 | + Issues https://github.com/microsoft/mssql-django/issues/110 |
| 162 | + and https://github.com/microsoft/mssql-django/issues/90 |
| 163 | + Unique indexes not being dropped when changing non-nullable |
| 164 | + foreign key with unique_together to nullable causing |
| 165 | + dependent on column error |
| 166 | + """ |
| 167 | + old_field = Choice._meta.get_field('question') |
| 168 | + new_field = models.ForeignKey( |
| 169 | + Question, null=False, on_delete=models.deletion.CASCADE |
| 170 | + ) |
| 171 | + new_field.set_attributes_from_name("question") |
| 172 | + with connection.schema_editor() as editor: |
| 173 | + editor.alter_field(Choice, old_field, new_field, strict=True) |
| 174 | + |
| 175 | + old_field = new_field |
| 176 | + new_field = models.ForeignKey( |
| 177 | + Question, null=True, on_delete=models.deletion.CASCADE |
| 178 | + ) |
| 179 | + new_field.set_attributes_from_name("question") |
| 180 | + try: |
| 181 | + with connection.schema_editor() as editor: |
| 182 | + editor.alter_field(Choice, old_field, new_field, strict=True) |
| 183 | + except ProgrammingError: |
| 184 | + self.fail("Unique indexes not being dropped") |
0 commit comments