diff options
| author | Markus Holtermann <info@markusholtermann.eu> | 2017-04-07 15:54:40 +0200 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2017-05-01 12:22:29 -0400 |
| commit | 6afede82192067efecedb039c29eb301816d5fb5 (patch) | |
| tree | a62e03404d25929549564ef3c2ad91ba6c7ff5f7 /tests | |
| parent | 211d2bf3f21fee22c868cd39452a133ad1e16c4d (diff) | |
[1.11.x] Fixed #28052 -- Prevented dropping Meta.indexes when changing db_index to False.
Thanks Marc Tamlyn for the report and Ian Foote/Tim Graham for review.
Backport of 663e48947ff8ef3e6a6275bda2d1ee1b0de13be3 from master
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/schema/models.py | 7 | ||||
| -rw-r--r-- | tests/schema/tests.py | 49 |
2 files changed, 52 insertions, 4 deletions
diff --git a/tests/schema/models.py b/tests/schema/models.py index 7d13c775a3..2930c81797 100644 --- a/tests/schema/models.py +++ b/tests/schema/models.py @@ -34,6 +34,13 @@ class AuthorWithEvenLongerName(models.Model): apps = new_apps +class AuthorWithIndexedName(models.Model): + name = models.CharField(max_length=255, db_index=True) + + class Meta: + apps = new_apps + + class Book(models.Model): author = models.ForeignKey(Author, models.CASCADE) title = models.CharField(max_length=100, db_index=True) diff --git a/tests/schema/tests.py b/tests/schema/tests.py index 73c1371cfe..4aa322b2ff 100644 --- a/tests/schema/tests.py +++ b/tests/schema/tests.py @@ -28,10 +28,11 @@ from .fields import ( CustomManyToManyField, InheritedManyToManyField, MediumBlobField, ) from .models import ( - Author, AuthorWithDefaultHeight, AuthorWithEvenLongerName, Book, - BookForeignObj, BookWeak, BookWithLongName, BookWithO2O, BookWithoutAuthor, - BookWithSlug, IntegerPK, Node, Note, NoteRename, Tag, TagIndexed, - TagM2MTest, TagUniqueRename, Thing, UniqueTest, new_apps, + Author, AuthorWithDefaultHeight, AuthorWithEvenLongerName, + AuthorWithIndexedName, Book, BookForeignObj, BookWeak, BookWithLongName, + BookWithO2O, BookWithoutAuthor, BookWithSlug, IntegerPK, Node, Note, + NoteRename, Tag, TagIndexed, TagM2MTest, TagUniqueRename, Thing, + UniqueTest, new_apps, ) @@ -1632,6 +1633,46 @@ class SchemaTests(TransactionTestCase): editor.remove_index(Author, index) self.assertNotIn('name', self.get_indexes(Author._meta.db_table)) + def test_remove_db_index_doesnt_remove_custom_indexes(self): + """ + Changing db_index to False doesn't remove indexes from Meta.indexes. + """ + with connection.schema_editor() as editor: + editor.create_model(AuthorWithIndexedName) + # Ensure the table has its index + self.assertIn('name', self.get_indexes(AuthorWithIndexedName._meta.db_table)) + + # Add the custom index + index = Index(fields=['-name'], name='author_name_idx') + author_index_name = index.name + with connection.schema_editor() as editor: + db_index_name = editor._create_index_name( + model=AuthorWithIndexedName, + column_names=('name',), + ) + if connection.features.uppercases_column_names: + author_index_name = author_index_name.upper() + db_index_name = db_index_name.upper() + try: + AuthorWithIndexedName._meta.indexes = [index] + with connection.schema_editor() as editor: + editor.add_index(AuthorWithIndexedName, index) + old_constraints = self.get_constraints(AuthorWithIndexedName._meta.db_table) + self.assertIn(author_index_name, old_constraints) + self.assertIn(db_index_name, old_constraints) + # Change name field to db_index=False + old_field = AuthorWithIndexedName._meta.get_field('name') + new_field = CharField(max_length=255) + new_field.set_attributes_from_name('name') + with connection.schema_editor() as editor: + editor.alter_field(AuthorWithIndexedName, old_field, new_field, strict=True) + new_constraints = self.get_constraints(AuthorWithIndexedName._meta.db_table) + self.assertNotIn(db_index_name, new_constraints) + # The index from Meta.indexes is still in the database. + self.assertIn(author_index_name, new_constraints) + finally: + AuthorWithIndexedName._meta.indexes = [] + def test_order_index(self): """ Indexes defined with ordering (ASC/DESC) defined on column |
