Issue #3406172 by mandclu: Provide a flag to allow updates to stored schema for fields

merge-requests/7170/head
Alex Pott 2024-03-24 11:29:27 +00:00
parent e0644f929b
commit fdfb3c6675
No known key found for this signature in database
GPG Key ID: BDA67E7EE836E5CE
2 changed files with 20 additions and 2 deletions

View File

@ -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.');
}

View File

@ -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);
}
}