diff options
| author | Hasan Ramezani <hasan.r67@gmail.com> | 2019-09-28 02:46:18 +0200 |
|---|---|---|
| committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2019-10-17 12:50:53 +0200 |
| commit | ef4beafa2ce58a81a56ffd48a20a6153126b611a (patch) | |
| tree | ea90b74c6307f51accc0f5d13d5e69d1e96b4b64 /tests/schema | |
| parent | 6f82df69efa372fb4bddf272fff577850a09f1dc (diff) | |
Refs #28816 -- Prevented silencing data loss when decreasing CharField.max_length for ArrayField.base_field on PostgreSQL.
Diffstat (limited to 'tests/schema')
| -rw-r--r-- | tests/schema/tests.py | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/tests/schema/tests.py b/tests/schema/tests.py index 08a77000b2..9a2195d1d5 100644 --- a/tests/schema/tests.py +++ b/tests/schema/tests.py @@ -895,6 +895,54 @@ class SchemaTests(TransactionTestCase): with connection.schema_editor() as editor: editor.alter_field(Foo, old_field, new_field, strict=True) + @isolate_apps('schema') + @unittest.skipUnless(connection.vendor == 'postgresql', 'PostgreSQL specific') + def test_alter_array_field_decrease_base_field_length(self): + from django.contrib.postgres.fields import ArrayField + + class ArrayModel(Model): + field = ArrayField(CharField(max_length=16)) + + class Meta: + app_label = 'schema' + + with connection.schema_editor() as editor: + editor.create_model(ArrayModel) + self.isolated_local_models = [ArrayModel] + ArrayModel.objects.create(field=['x' * 16]) + old_field = ArrayModel._meta.get_field('field') + new_field = ArrayField(CharField(max_length=15)) + new_field.set_attributes_from_name('field') + new_field.model = ArrayModel + with connection.schema_editor() as editor: + msg = 'value too long for type character varying(15)' + with self.assertRaisesMessage(DataError, msg): + editor.alter_field(ArrayModel, old_field, new_field, strict=True) + + @isolate_apps('schema') + @unittest.skipUnless(connection.vendor == 'postgresql', 'PostgreSQL specific') + def test_alter_array_field_decrease_nested_base_field_length(self): + from django.contrib.postgres.fields import ArrayField + + class ArrayModel(Model): + field = ArrayField(ArrayField(CharField(max_length=16))) + + class Meta: + app_label = 'schema' + + with connection.schema_editor() as editor: + editor.create_model(ArrayModel) + self.isolated_local_models = [ArrayModel] + ArrayModel.objects.create(field=[['x' * 16]]) + old_field = ArrayModel._meta.get_field('field') + new_field = ArrayField(ArrayField(CharField(max_length=15))) + new_field.set_attributes_from_name('field') + new_field.model = ArrayModel + with connection.schema_editor() as editor: + msg = 'value too long for type character varying(15)' + with self.assertRaisesMessage(DataError, msg): + editor.alter_field(ArrayModel, old_field, new_field, strict=True) + def test_alter_textfield_to_null(self): """ #24307 - Should skip an alter statement on databases with |
