From 73dc06a6c40251a639a2ec3e0927a57b13a0c53b Mon Sep 17 00:00:00 2001 From: catch Date: Tue, 17 Dec 2019 09:44:10 +0000 Subject: [PATCH] Issue #3099986 by alexpott, amateescu: Move part of workspaces_post_update_move_association_data() to a hook_update_N --- core/modules/workspaces/workspaces.install | 21 ++++++++++++++ .../workspaces/workspaces.post_update.php | 28 ++++++------------- 2 files changed, 30 insertions(+), 19 deletions(-) diff --git a/core/modules/workspaces/workspaces.install b/core/modules/workspaces/workspaces.install index 3ea33234185..acf3b21fd3d 100644 --- a/core/modules/workspaces/workspaces.install +++ b/core/modules/workspaces/workspaces.install @@ -6,6 +6,7 @@ */ use Drupal\Core\Entity\EntityTypeInterface; +use Drupal\Core\Entity\ContentEntityNullStorage; use Drupal\Core\Field\BaseFieldDefinition; use Drupal\workspaces\Entity\Workspace; @@ -174,3 +175,23 @@ function workspaces_update_8802() { $entity_definition_update_manager->installFieldStorageDefinition('parent', 'workspace', 'workspaces', $storage_definition); } + +/** + * Remove the Workspace Association entity storage if necessary. + */ +function workspaces_update_8803() { + $entity_definition_update_manager = \Drupal::entityDefinitionUpdateManager(); + $entity_type = $entity_definition_update_manager->getEntityType('workspace_association'); + + // We can't migrate the workspace association data if the entity type is not + // using its default storage. + // @see workspaces_post_update_move_association_data() + if ($entity_type && $entity_type->getHandlerClasses()['storage'] === 'Drupal\workspaces\WorkspaceAssociationStorage') { + \Drupal::state()->set('workspaces_update_8803.tables', [ + 'base_table' => $entity_type->getBaseTable(), + 'revision_table' => $entity_type->getRevisionTable(), + ]); + $entity_type->setStorageClass(ContentEntityNullStorage::class); + $entity_definition_update_manager->uninstallEntityType($entity_type); + } +} diff --git a/core/modules/workspaces/workspaces.post_update.php b/core/modules/workspaces/workspaces.post_update.php index 2c5d162ebd7..b32be7d4247 100644 --- a/core/modules/workspaces/workspaces.post_update.php +++ b/core/modules/workspaces/workspaces.post_update.php @@ -5,10 +5,8 @@ * Post update functions for the Workspaces module. */ -use Drupal\Core\Entity\ContentEntityNullStorage; use Drupal\Core\Entity\Entity\EntityFormDisplay; use Drupal\Core\Entity\EntityTypeInterface; -use Drupal\Core\Entity\Sql\SqlContentEntityStorage; use Drupal\Core\Site\Settings; /** @@ -31,20 +29,14 @@ function workspaces_post_update_remove_default_workspace() { */ function workspaces_post_update_move_association_data(&$sandbox) { $database = \Drupal::database(); - $entity_definition_update_manager = \Drupal::entityDefinitionUpdateManager(); $entity_type_manager = \Drupal::entityTypeManager(); - $entity_type = $entity_definition_update_manager->getEntityType('workspace_association'); - // We can't migrate the workspace association data if the entity type is not - // using its default storage. - if ($entity_type->getHandlerClasses()['storage'] !== 'Drupal\workspaces\WorkspaceAssociationStorage') { + // @see workspaces_update_8803() + $tables = \Drupal::state()->get('workspaces_update_8803.tables'); + if (!$tables) { return; } - // Since the custom storage class doesn't exist anymore, we have to use core's - // default storage. - $entity_type->setStorageClass(SqlContentEntityStorage::class); - // If 'progress' is not set, this will be the first run of the batch. if (!isset($sandbox['progress'])) { $sandbox['progress'] = 0; @@ -93,12 +85,12 @@ function workspaces_post_update_move_association_data(&$sandbox) { // Copy all the data from the base table of the 'workspace_association' // entity type to the temporary association table. - $select = $database->select($entity_type->getBaseTable()) - ->fields($entity_type->getBaseTable(), ['workspace', 'target_entity_type_id', 'target_entity_id', 'target_entity_revision_id']); + $select = $database->select($tables['base_table']) + ->fields($tables['base_table'], ['workspace', 'target_entity_type_id', 'target_entity_id', 'target_entity_revision_id']); $database->insert('tmp_workspace_association')->from($select)->execute(); } - $table_name = $entity_type->getRevisionTable(); + $table_name = $tables['revision_table']; $revision_field_name = 'revision_id'; // Get the next entity association revision records to migrate. @@ -136,12 +128,10 @@ function workspaces_post_update_move_association_data(&$sandbox) { // Uninstall the 'workspace_association' entity type and rename the temporary // table. if ($sandbox['#finished'] == 1) { - $entity_type->setStorageClass(ContentEntityNullStorage::class); - $entity_definition_update_manager->uninstallEntityType($entity_type); - $database->schema()->dropTable('workspace_association'); - $database->schema()->dropTable('workspace_association_revision'); - + $database->schema()->dropTable($tables['base_table']); + $database->schema()->dropTable($tables['revision_table']); $database->schema()->renameTable('tmp_workspace_association', 'workspace_association'); + \Drupal::state()->delete('workspaces_update_8803.tables'); } }