diff --git a/core/lib/Drupal/Core/Entity/RevisionableSchemaConverter.php b/core/lib/Drupal/Core/Entity/RevisionableSchemaConverter.php index cd7fe68..0f73a77 100644 --- a/core/lib/Drupal/Core/Entity/RevisionableSchemaConverter.php +++ b/core/lib/Drupal/Core/Entity/RevisionableSchemaConverter.php @@ -81,7 +81,7 @@ public function convertSchema($entity_type_id, $options = []) { ]; $options = array_merge($default_options, $options); $this->updateEntityType($entity_type_id, $options); - //$this->createTables($entity_type_id, $options); + $this->createTables($entity_type_id, $options); $this->installRevisionableFields($entity_type_id, $options); } @@ -157,14 +157,27 @@ public function copyData(EntityTypeInterface $entity_type, array &$sandbox) { * Options to update the entity type with. */ protected function updateEntityType($entity_type_id, $options) { - $entity_type = $this->entityDefinitionUpdateManager->getEntityType($entity_type_id); - $keys = $entity_type->getKeys(); + $last_entity_type = $this->lastInstalledSchemaRepository->getLastInstalledDefinition($entity_type_id); + $keys = $last_entity_type->getKeys(); $keys['revision'] = $options['revision']; - $entity_type->set('entity_keys', $keys); - $entity_type->set('revision_table', $options['revision_table']); - $entity_type->set('revision_data_table', $options['revision_data_table']); - $entity_type->data_migation = TRUE; - $this->entityDefinitionUpdateManager->updateEntityType($entity_type); + $last_entity_type->set('entity_keys', $keys); + $last_entity_type->set('revision_table', $options['revision_table']); + $last_entity_type->set('revision_data_table', $options['revision_data_table']); + $this->lastInstalledSchemaRepository->setLastInstalledDefinition($last_entity_type); + } + + /** + * Creates missing tables. + * + * @param string $entity_type_id + * ID of the entity type to update. + */ + protected function createTables($entity_type_id) { + $storage = $this->entityTypeManager->getStorage($entity_type_id); + $entity_type = $this->entityDefinitionUpdateManager->getEntityType($entity_type_id); + if ($storage instanceof EntityTypeListenerInterface) { + $storage->onEntityTypeCreateMissingTables($entity_type); + } } /** diff --git a/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php b/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php index e115bd2..dc075e2 100644 --- a/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php +++ b/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php @@ -1374,6 +1374,15 @@ public function onEntityTypeCreate(EntityTypeInterface $entity_type) { /** * {@inheritdoc} */ + public function onEntityTypeCreateMissingTables(EntityTypeInterface $entity_type) { + $this->wrapSchemaException(function () use ($entity_type) { + $this->getStorageSchema()->onEntityTypeCreateMissingTables($entity_type); + }); + } + + /** + * {@inheritdoc} + */ public function onEntityTypeUpdate(EntityTypeInterface $entity_type, EntityTypeInterface $original) { // Ensure we have an updated entity type definition. $this->entityType = $entity_type; diff --git a/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorageSchema.php b/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorageSchema.php index ec3ebaf..7c2fea7 100644 --- a/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorageSchema.php +++ b/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorageSchema.php @@ -289,6 +289,39 @@ public function onEntityTypeCreate(EntityTypeInterface $entity_type) { /** * {@inheritdoc} */ + public function onEntityTypeCreateMissingTables(EntityTypeInterface $entity_type) { + $this->checkEntityType($entity_type); + $schema_handler = $this->database->schema(); + + // Create entity tables. + $schema = $this->getEntitySchema($entity_type, TRUE); + foreach ($schema as $table_name => $table_schema) { + if (!$schema_handler->tableExists($table_name)) { + $schema_handler->createTable($table_name, $table_schema); + } + } + + // Create dedicated field tables. + $field_storage_definitions = $this->entityManager->getFieldStorageDefinitions($entity_type->id()); + $table_mapping = $this->storage->getTableMapping($field_storage_definitions); + foreach ($field_storage_definitions as $field_storage_definition) { + if ($table_mapping->requiresDedicatedTableStorage($field_storage_definition)) { + $this->createDedicatedTableSchema($field_storage_definition); + } + elseif ($table_mapping->allowsSharedTableStorage($field_storage_definition)) { + // The shared tables are already fully created, but we need to save the + // per-field schema definitions for later use. + $this->createSharedTableSchema($field_storage_definition, TRUE); + } + } + + // Save data about entity indexes and keys. + $this->saveEntitySchemaData($entity_type, $schema); + } + + /** + * {@inheritdoc} + */ public function onEntityTypeUpdate(EntityTypeInterface $entity_type, EntityTypeInterface $original) { $this->checkEntityType($entity_type); $this->checkEntityType($original);