From fdfb3c6675dc7f0b3c457e8fe6a0792dee7db665 Mon Sep 17 00:00:00 2001 From: Alex Pott Date: Sun, 24 Mar 2024 11:29:27 +0000 Subject: [PATCH] Issue #3406172 by mandclu: Provide a flag to allow updates to stored schema for fields --- .../Sql/SqlContentEntityStorageSchema.php | 4 ++-- ...SqlContentEntityStorageSchemaColumnTest.php | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorageSchema.php b/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorageSchema.php index b2eb730adad..1a794a145f3 100644 --- a/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorageSchema.php +++ b/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorageSchema.php @@ -1746,7 +1746,7 @@ class SqlContentEntityStorageSchema implements DynamicallyFieldableEntityStorage } } else { - if ($this->hasColumnChanges($storage_definition, $original)) { + if (empty($storage_definition->getSetting('column_changes_handled')) && $this->hasColumnChanges($storage_definition, $original)) { throw new FieldStorageDefinitionUpdateForbiddenException('The SQL storage cannot change the schema for an existing field (' . $storage_definition->getName() . ' in ' . $storage_definition->getTargetEntityTypeId() . ' entity) with data.'); } // There is data, so there are no column changes. Drop all the prior @@ -1839,7 +1839,7 @@ class SqlContentEntityStorageSchema implements DynamicallyFieldableEntityStorage } } else { - if ($this->hasColumnChanges($storage_definition, $original)) { + if (empty($storage_definition->getSetting('column_changes_handled')) && $this->hasColumnChanges($storage_definition, $original)) { throw new FieldStorageDefinitionUpdateForbiddenException('The SQL storage cannot change the schema for an existing field (' . $storage_definition->getName() . ' in ' . $storage_definition->getTargetEntityTypeId() . ' entity) with data.'); } diff --git a/core/modules/field/tests/src/Kernel/Entity/Update/SqlContentEntityStorageSchemaColumnTest.php b/core/modules/field/tests/src/Kernel/Entity/Update/SqlContentEntityStorageSchemaColumnTest.php index b466017a186..3a42642cdf1 100644 --- a/core/modules/field/tests/src/Kernel/Entity/Update/SqlContentEntityStorageSchemaColumnTest.php +++ b/core/modules/field/tests/src/Kernel/Entity/Update/SqlContentEntityStorageSchemaColumnTest.php @@ -112,4 +112,22 @@ class SqlContentEntityStorageSchemaColumnTest extends KernelTestBase { $entity_definition_update_manager->updateFieldStorageDefinition($field_storage_definition); } + /** + * Tests that schema changes are updated for fields with data with the flag. + */ + public function testColumnUpdateWithFlag() { + // Change the field type in the stored schema. + $schema = \Drupal::keyValue('entity.storage_schema.sql')->get('entity_test_rev.field_schema_data.test'); + $schema['entity_test_rev__test']['fields']['test_value']['type'] = 'varchar_ascii'; + \Drupal::keyValue('entity.storage_schema.sql')->set('entity_test_rev.field_schema_data.test', $schema); + + // Now attempt to run automatic updates. It should succeed if the + // column_changes_handled flag is passed. + $entity_definition_update_manager = \Drupal::entityDefinitionUpdateManager(); + $field_storage_definition = $entity_definition_update_manager->getFieldStorageDefinition('test', 'entity_test_rev'); + // Provide the flag to allow schema updates. + $field_storage_definition->setSetting('column_changes_handled', TRUE); + $entity_definition_update_manager->updateFieldStorageDefinition($field_storage_definition); + } + }