summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorMariusz Felisiak <felisiak.mariusz@gmail.com>2022-12-22 07:12:17 +0100
committerGitHub <noreply@github.com>2022-12-22 07:12:17 +0100
commitae0899be0d787fbfc5f5ab2b18c5a8219d822d2b (patch)
treef20955a6a6569046d0257ad86295762e10ed1c62 /tests
parent3b24a3fa337bedc11352a17952cb9c3f77f9b9f2 (diff)
downloaddjango-ae0899be0d787fbfc5f5ab2b18c5a8219d822d2b.tar.gz
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).
Diffstat (limited to 'tests')
-rw-r--r--tests/schema/tests.py32
1 files changed, 32 insertions, 0 deletions
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
@@ -4951,6 +4951,38 @@ class SchemaTests(TransactionTestCase):
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")
if not collation: