Issue #3038707 by amateescu: The entity type definition is out of sync between SqlContentEntityStorage and SqlContentEntityStorageSchema during field storage CRUD operations

(cherry picked from commit 7d2d9cca09)
8.7.x
Lee Rowlands 2019-03-09 13:11:41 +10:00
parent e716af79b9
commit d26ee6aabd
No known key found for this signature in database
GPG Key ID: 2B829A3DF9204DC4
2 changed files with 50 additions and 3 deletions

View File

@ -7,6 +7,7 @@ use Drupal\Core\Entity\EntityLastInstalledSchemaRepositoryInterface;
use Drupal\Core\Entity\EntityFieldManagerInterface; use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\FieldableEntityStorageInterface; use Drupal\Core\Entity\FieldableEntityStorageInterface;
use Drupal\Core\Entity\Sql\SqlContentEntityStorage;
use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\EventDispatcher\EventDispatcherInterface;
/** /**
@ -81,7 +82,16 @@ class FieldStorageDefinitionListener implements FieldStorageDefinitionListenerIn
// @todo Forward this to all interested handlers, not only storage, once // @todo Forward this to all interested handlers, not only storage, once
// iterating handlers is possible: https://www.drupal.org/node/2332857. // 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) { if ($storage instanceof FieldStorageDefinitionListenerInterface) {
$storage->onFieldStorageDefinitionCreate($storage_definition); $storage->onFieldStorageDefinitionCreate($storage_definition);
@ -101,7 +111,16 @@ class FieldStorageDefinitionListener implements FieldStorageDefinitionListenerIn
// @todo Forward this to all interested handlers, not only storage, once // @todo Forward this to all interested handlers, not only storage, once
// iterating handlers is possible: https://www.drupal.org/node/2332857. // 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) { if ($storage instanceof FieldStorageDefinitionListenerInterface) {
$storage->onFieldStorageDefinitionUpdate($storage_definition, $original); $storage->onFieldStorageDefinitionUpdate($storage_definition, $original);
@ -121,7 +140,16 @@ class FieldStorageDefinitionListener implements FieldStorageDefinitionListenerIn
// @todo Forward this to all interested handlers, not only storage, once // @todo Forward this to all interested handlers, not only storage, once
// iterating handlers is possible: https://www.drupal.org/node/2332857. // 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 // 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. // it later during field_purge_batch(), but only if the field has data.

View File

@ -676,6 +676,25 @@ class EntityDefinitionUpdateTest extends EntityKernelTestBase {
} }
} }
/**
* 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. * Tests creating and deleting a multi-field index when there are no existing entities.
*/ */