From 7d2d9cca093ea78f22fe7058ad6287e98b85470c Mon Sep 17 00:00:00 2001 From: Lee Rowlands <lee.rowlands@previousnext.com.au> Date: Sat, 9 Mar 2019 13:11:41 +1000 Subject: [PATCH] Issue #3038707 by amateescu: The entity type definition is out of sync between SqlContentEntityStorage and SqlContentEntityStorageSchema during field storage CRUD operations --- .../Field/FieldStorageDefinitionListener.php | 34 +++++++++++++++++-- .../Entity/EntityDefinitionUpdateTest.php | 19 +++++++++++ 2 files changed, 50 insertions(+), 3 deletions(-) diff --git a/core/lib/Drupal/Core/Field/FieldStorageDefinitionListener.php b/core/lib/Drupal/Core/Field/FieldStorageDefinitionListener.php index 6ef363cf2d1b..e67629b75932 100644 --- a/core/lib/Drupal/Core/Field/FieldStorageDefinitionListener.php +++ b/core/lib/Drupal/Core/Field/FieldStorageDefinitionListener.php @@ -7,6 +7,7 @@ use Drupal\Core\Entity\EntityFieldManagerInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Entity\FieldableEntityStorageInterface; +use Drupal\Core\Entity\Sql\SqlContentEntityStorage; use Symfony\Component\EventDispatcher\EventDispatcherInterface; /** @@ -81,7 +82,16 @@ public function onFieldStorageDefinitionCreate(FieldStorageDefinitionInterface $ // @todo Forward this to all interested handlers, not only storage, once // iterating handlers is possible: https://www.drupal.org/node/2332857. - $storage = $this->entityTypeManager->getStorage($entity_type_id); + $storage = clone $this->entityTypeManager->getStorage($entity_type_id); + + // Entity type definition updates can change the schema by adding or + // removing entity tables (for example when switching an entity type from + // non-revisionable to revisionable), so CRUD operations on a field storage + // definition need to use the last installed entity type schema. + if ($storage instanceof SqlContentEntityStorage + && ($last_installed_entity_type = $this->entityLastInstalledSchemaRepository->getLastInstalledDefinition($entity_type_id))) { + $storage->setEntityType($last_installed_entity_type); + } if ($storage instanceof FieldStorageDefinitionListenerInterface) { $storage->onFieldStorageDefinitionCreate($storage_definition); @@ -101,7 +111,16 @@ public function onFieldStorageDefinitionUpdate(FieldStorageDefinitionInterface $ // @todo Forward this to all interested handlers, not only storage, once // iterating handlers is possible: https://www.drupal.org/node/2332857. - $storage = $this->entityTypeManager->getStorage($entity_type_id); + $storage = clone $this->entityTypeManager->getStorage($entity_type_id); + + // Entity type definition updates can change the schema by adding or + // removing entity tables (for example when switching an entity type from + // non-revisionable to revisionable), so CRUD operations on a field storage + // definition need to use the last installed entity type schema. + if ($storage instanceof SqlContentEntityStorage + && ($last_installed_entity_type = $this->entityLastInstalledSchemaRepository->getLastInstalledDefinition($entity_type_id))) { + $storage->setEntityType($last_installed_entity_type); + } if ($storage instanceof FieldStorageDefinitionListenerInterface) { $storage->onFieldStorageDefinitionUpdate($storage_definition, $original); @@ -121,7 +140,16 @@ public function onFieldStorageDefinitionDelete(FieldStorageDefinitionInterface $ // @todo Forward this to all interested handlers, not only storage, once // iterating handlers is possible: https://www.drupal.org/node/2332857. - $storage = $this->entityTypeManager->getStorage($entity_type_id); + $storage = clone $this->entityTypeManager->getStorage($entity_type_id); + + // Entity type definition updates can change the schema by adding or + // removing entity tables (for example when switching an entity type from + // non-revisionable to revisionable), so CRUD operations on a field storage + // definition need to use the last installed entity type schema. + if ($storage instanceof SqlContentEntityStorage + && ($last_installed_entity_type = $this->entityLastInstalledSchemaRepository->getLastInstalledDefinition($entity_type_id))) { + $storage->setEntityType($last_installed_entity_type); + } // Keep the field definition in the deleted fields repository so we can use // it later during field_purge_batch(), but only if the field has data. diff --git a/core/tests/Drupal/KernelTests/Core/Entity/EntityDefinitionUpdateTest.php b/core/tests/Drupal/KernelTests/Core/Entity/EntityDefinitionUpdateTest.php index f33d6a3ce87d..597d22fb7b71 100644 --- a/core/tests/Drupal/KernelTests/Core/Entity/EntityDefinitionUpdateTest.php +++ b/core/tests/Drupal/KernelTests/Core/Entity/EntityDefinitionUpdateTest.php @@ -676,6 +676,25 @@ public function testBundleFieldUpdateWithExistingData() { } } + /** + * Tests updating a bundle field when the entity type schema has changed. + */ + public function testBundleFieldUpdateWithEntityTypeSchemaUpdate() { + // Add the bundle field and run the update. + $this->addBundleField(); + $this->applyEntityUpdates(); + + // Update the entity type schema to revisionable but don't run the updates + // yet. + $this->updateEntityTypeToRevisionable(); + + // Perform a no-op update on the bundle field, which should work because + // both the storage and the storage schema are using the last installed + // entity type definition. + $entity_definition_update_manager = \Drupal::entityDefinitionUpdateManager(); + $entity_definition_update_manager->updateFieldStorageDefinition($entity_definition_update_manager->getFieldStorageDefinition('new_bundle_field', 'entity_test_update')); + } + /** * Tests creating and deleting a multi-field index when there are no existing entities. */ -- GitLab