From ae0899be0d787fbfc5f5ab2b18c5a8219d822d2b Mon Sep 17 00:00:00 2001 From: Mariusz Felisiak Date: Thu, 22 Dec 2022 07:12:17 +0100 Subject: Fixed #34219 -- Preserved Char/TextField.db_collation when altering column type. This moves setting a database collation to the column type alteration as both must be set at the same time. This should also avoid another layer of the column type alteration when adding database comments support (#18468). --- tests/schema/tests.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) (limited to 'tests') diff --git a/tests/schema/tests.py b/tests/schema/tests.py index fee5ca9d2b..9b2b512c1b 100644 --- a/tests/schema/tests.py +++ b/tests/schema/tests.py @@ -4950,6 +4950,38 @@ class SchemaTests(TransactionTestCase): editor.alter_field(Author, new_field, old_field, strict=True) self.assertIsNone(self.get_column_collation(Author._meta.db_table, "name")) + @skipUnlessDBFeature("supports_collation_on_charfield") + def test_alter_field_type_preserve_db_collation(self): + collation = connection.features.test_collations.get("non_default") + if not collation: + self.skipTest("Language collations are not supported.") + + with connection.schema_editor() as editor: + editor.create_model(Author) + + old_field = Author._meta.get_field("name") + new_field = CharField(max_length=255, db_collation=collation) + new_field.set_attributes_from_name("name") + new_field.model = Author + with connection.schema_editor() as editor: + editor.alter_field(Author, old_field, new_field, strict=True) + self.assertEqual( + self.get_column_collation(Author._meta.db_table, "name"), + collation, + ) + # Changing a field type should preserve the collation. + old_field = new_field + new_field = CharField(max_length=511, db_collation=collation) + new_field.set_attributes_from_name("name") + new_field.model = Author + with connection.schema_editor() as editor: + editor.alter_field(Author, new_field, old_field, strict=True) + # Collation is preserved. + self.assertEqual( + self.get_column_collation(Author._meta.db_table, "name"), + collation, + ) + @skipUnlessDBFeature("supports_collation_on_charfield") def test_alter_primary_key_db_collation(self): collation = connection.features.test_collations.get("non_default") -- cgit v1.3