summaryrefslogtreecommitdiff
path: root/tests/schema/tests.py
diff options
context:
space:
mode:
authorFederico Frenguelli <synasius@gmail.com>2015-11-07 17:08:03 +0100
committerTim Graham <timograham@gmail.com>2015-12-10 16:19:36 -0500
commit722fae4b5159b2810e252e3385936f86f04eb09b (patch)
treeecd1f4617d72085b871a13e9b1d43a9ef7cef21b /tests/schema/tests.py
parent364d7d969039bd14d3072a5ed035b88b44d855fd (diff)
[1.9.x] Fixed #25412 -- Fixed missing PostgreSQL index on Char/TextField when using AlterField.
Thanks to Emanuele Palazzetti for the help. Backport of 3a36c8079544c83dcdea4e52181efcd2d1e86b9c from master
Diffstat (limited to 'tests/schema/tests.py')
-rw-r--r--tests/schema/tests.py68
1 files changed, 68 insertions, 0 deletions
diff --git a/tests/schema/tests.py b/tests/schema/tests.py
index eff310dccb..b8bd883d9e 100644
--- a/tests/schema/tests.py
+++ b/tests/schema/tests.py
@@ -1707,3 +1707,71 @@ class SchemaTests(TransactionTestCase):
new_field.set_attributes_from_name("info")
with connection.schema_editor() as editor:
editor.add_field(Author, new_field)
+
+ @unittest.skipUnless(connection.vendor == 'postgresql', "PostgreSQL specific")
+ def test_alter_field_add_index_to_charfield(self):
+ # Create the table
+ with connection.schema_editor() as editor:
+ editor.create_model(Author)
+ # Ensure the table is there and has no index
+ self.assertNotIn('name', self.get_indexes(Author._meta.db_table))
+ # Alter to add the index
+ old_field = Author._meta.get_field('name')
+ new_field = CharField(max_length=255, db_index=True)
+ new_field.set_attributes_from_name('name')
+ with connection.schema_editor() as editor:
+ editor.alter_field(Author, old_field, new_field, strict=True)
+ # Check that all the constraints are there
+ constraints = self.get_constraints(Author._meta.db_table)
+ name_indexes = []
+ for name, details in constraints.items():
+ if details['columns'] == ['name']:
+ name_indexes.append(name)
+ self.assertEqual(2, len(name_indexes), 'Indexes are missing for name column')
+ # Check that one of the indexes ends with `_like`
+ like_index = [x for x in name_indexes if x.endswith('_like')]
+ self.assertEqual(1, len(like_index), 'Index with the operator class is missing for the name column')
+ # Remove the index
+ with connection.schema_editor() as editor:
+ editor.alter_field(Author, new_field, old_field, strict=True)
+ # Ensure the name constraints where dropped
+ constraints = self.get_constraints(Author._meta.db_table)
+ name_indexes = []
+ for details in constraints.values():
+ if details['columns'] == ['name']:
+ name_indexes.append(details)
+ self.assertEqual(0, len(name_indexes), 'Indexes were not dropped for the name column')
+
+ @unittest.skipUnless(connection.vendor == 'postgresql', "PostgreSQL specific")
+ def test_alter_field_add_index_to_textfield(self):
+ # Create the table
+ with connection.schema_editor() as editor:
+ editor.create_model(Note)
+ # Ensure the table is there and has no index
+ self.assertNotIn('info', self.get_indexes(Note._meta.db_table))
+ # Alter to add the index
+ old_field = Note._meta.get_field('info')
+ new_field = TextField(db_index=True)
+ new_field.set_attributes_from_name('info')
+ with connection.schema_editor() as editor:
+ editor.alter_field(Note, old_field, new_field, strict=True)
+ # Check that all the constraints are there
+ constraints = self.get_constraints(Note._meta.db_table)
+ info_indexes = []
+ for name, details in constraints.items():
+ if details['columns'] == ['info']:
+ info_indexes.append(name)
+ self.assertEqual(2, len(info_indexes), 'Indexes are missing for info column')
+ # Check that one of the indexes ends with `_like`
+ like_index = [x for x in info_indexes if x.endswith('_like')]
+ self.assertEqual(1, len(like_index), 'Index with the operator class is missing for the info column')
+ # Remove the index
+ with connection.schema_editor() as editor:
+ editor.alter_field(Note, new_field, old_field, strict=True)
+ # Ensure the info constraints where dropped
+ constraints = self.get_constraints(Note._meta.db_table)
+ info_indexes = []
+ for details in constraints.values():
+ if details['columns'] == ['info']:
+ info_indexes.append(details)
+ self.assertEqual(0, len(info_indexes), 'Indexes were not dropped for the info column')