| 4953 | @skipUnlessDBFeature("supports_collation_on_charfield") |
| 4954 | def test_alter_field_type_preserve_db_collation(self): |
| 4955 | collation = connection.features.test_collations.get("non_default") |
| 4956 | if not collation: |
| 4957 | self.skipTest("Language collations are not supported.") |
| 4958 | |
| 4959 | with connection.schema_editor() as editor: |
| 4960 | editor.create_model(Author) |
| 4961 | |
| 4962 | old_field = Author._meta.get_field("name") |
| 4963 | new_field = CharField(max_length=255, db_collation=collation) |
| 4964 | new_field.set_attributes_from_name("name") |
| 4965 | new_field.model = Author |
| 4966 | with connection.schema_editor() as editor: |
| 4967 | editor.alter_field(Author, old_field, new_field, strict=True) |
| 4968 | self.assertEqual( |
| 4969 | self.get_column_collation(Author._meta.db_table, "name"), |
| 4970 | collation, |
| 4971 | ) |
| 4972 | # Changing a field type should preserve the collation. |
| 4973 | old_field = new_field |
| 4974 | new_field = CharField(max_length=511, db_collation=collation) |
| 4975 | new_field.set_attributes_from_name("name") |
| 4976 | new_field.model = Author |
| 4977 | with connection.schema_editor() as editor: |
| 4978 | editor.alter_field(Author, new_field, old_field, strict=True) |
| 4979 | # Collation is preserved. |
| 4980 | self.assertEqual( |
| 4981 | self.get_column_collation(Author._meta.db_table, "name"), |
| 4982 | collation, |
| 4983 | ) |
| 4984 | |