workspaces.post_update.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. /**
  3. * @file
  4. * Post update functions for the Workspaces module.
  5. */
  6. use Drupal\Core\Entity\Entity\EntityFormDisplay;
  7. use Drupal\Core\Entity\EntityTypeInterface;
  8. use Drupal\Core\Site\Settings;
  9. /**
  10. * Clear caches due to access changes.
  11. */
  12. function workspaces_post_update_access_clear_caches() {
  13. }
  14. /**
  15. * Remove the default workspace.
  16. */
  17. function workspaces_post_update_remove_default_workspace() {
  18. if ($workspace = \Drupal::entityTypeManager()->getStorage('workspace')->load('live')) {
  19. $workspace->delete();
  20. }
  21. }
  22. /**
  23. * Move the workspace association data to an entity field and a custom table.
  24. */
  25. function workspaces_post_update_move_association_data(&$sandbox) {
  26. $database = \Drupal::database();
  27. $entity_type_manager = \Drupal::entityTypeManager();
  28. // @see workspaces_update_8803()
  29. $tables = \Drupal::state()->get('workspaces_update_8803.tables');
  30. if (!$tables) {
  31. return;
  32. }
  33. // If 'progress' is not set, this will be the first run of the batch.
  34. if (!isset($sandbox['progress'])) {
  35. $sandbox['progress'] = 0;
  36. $sandbox['current_id'] = -1;
  37. // Create a temporary table for the new workspace_association index.
  38. $schema = [
  39. 'description' => 'Stores the association between entity revisions and their workspace.',
  40. 'fields' => [
  41. 'workspace' => [
  42. 'type' => 'varchar_ascii',
  43. 'length' => 128,
  44. 'not null' => TRUE,
  45. 'default' => '',
  46. 'description' => 'The workspace ID.',
  47. ],
  48. 'target_entity_type_id' => [
  49. 'type' => 'varchar_ascii',
  50. 'length' => EntityTypeInterface::ID_MAX_LENGTH,
  51. 'not null' => TRUE,
  52. 'default' => '',
  53. 'description' => 'The ID of the associated entity type.',
  54. ],
  55. 'target_entity_id' => [
  56. 'type' => 'int',
  57. 'unsigned' => TRUE,
  58. 'not null' => TRUE,
  59. 'description' => 'The ID of the associated entity.',
  60. ],
  61. 'target_entity_revision_id' => [
  62. 'type' => 'int',
  63. 'unsigned' => TRUE,
  64. 'not null' => TRUE,
  65. 'description' => 'The revision ID of the associated entity.',
  66. ],
  67. ],
  68. 'indexes' => [
  69. 'target_entity_revision_id' => ['target_entity_revision_id'],
  70. ],
  71. 'primary key' => ['workspace', 'target_entity_type_id', 'target_entity_id'],
  72. ];
  73. if ($database->schema()->tableExists('tmp_workspace_association')) {
  74. $database->schema()->dropTable('tmp_workspace_association');
  75. }
  76. $database->schema()->createTable('tmp_workspace_association', $schema);
  77. // Copy all the data from the base table of the 'workspace_association'
  78. // entity type to the temporary association table.
  79. $select = $database->select($tables['base_table'])
  80. ->fields($tables['base_table'], ['workspace', 'target_entity_type_id', 'target_entity_id', 'target_entity_revision_id']);
  81. $database->insert('tmp_workspace_association')->from($select)->execute();
  82. }
  83. $table_name = $tables['revision_table'];
  84. $revision_field_name = 'revision_id';
  85. // Get the next entity association revision records to migrate.
  86. $step_size = Settings::get('entity_update_batch_size', 50);
  87. $workspace_association_records = $database->select($table_name, 't')
  88. ->condition("t.$revision_field_name", $sandbox['current_id'], '>')
  89. ->fields('t')
  90. ->orderBy($revision_field_name, 'ASC')
  91. ->range(0, $step_size)
  92. ->execute()
  93. ->fetchAll();
  94. foreach ($workspace_association_records as $record) {
  95. // Set the workspace reference on the tracked entity revision.
  96. /** @var \Drupal\Core\Entity\ContentEntityInterface $revision */
  97. $revision = $entity_type_manager->getStorage($record->target_entity_type_id)->loadRevision($record->target_entity_revision_id);
  98. $revision->set('workspace', $record->workspace);
  99. $revision->setSyncing(TRUE);
  100. $revision->save();
  101. $sandbox['progress']++;
  102. $sandbox['current_id'] = $record->{$revision_field_name};
  103. }
  104. // Get an updated count of workspace_association revisions that still need to
  105. // be migrated to the new storage.
  106. $missing = $database->select($table_name, 't')
  107. ->condition("t.$revision_field_name", $sandbox['current_id'], '>')
  108. ->orderBy($revision_field_name, 'ASC')
  109. ->countQuery()
  110. ->execute()
  111. ->fetchField();
  112. $sandbox['#finished'] = $missing ? $sandbox['progress'] / ($sandbox['progress'] + (int) $missing) : 1;
  113. // Uninstall the 'workspace_association' entity type and rename the temporary
  114. // table.
  115. if ($sandbox['#finished'] == 1) {
  116. $database->schema()->dropTable($tables['base_table']);
  117. $database->schema()->dropTable($tables['revision_table']);
  118. $database->schema()->renameTable('tmp_workspace_association', 'workspace_association');
  119. \Drupal::state()->delete('workspaces_update_8803.tables');
  120. }
  121. }
  122. /**
  123. * Add the workspace 'parent' field to the 'deploy' form display.
  124. */
  125. function workspaces_post_update_update_deploy_form_display() {
  126. if ($form_display = EntityFormDisplay::load('workspace.workspace.deploy')) {
  127. $form_display->removeComponent('parent')->save();
  128. }
  129. }
  130. /**
  131. * Removes the workspace association entity and field schema data.
  132. */
  133. function workspaces_post_update_remove_association_schema_data() {
  134. // Delete the entity and field schema data.
  135. $keys = [
  136. 'workspace_association.entity_schema_data',
  137. 'workspace_association.field_schema_data.id',
  138. 'workspace_association.field_schema_data.revision_id',
  139. 'workspace_association.field_schema_data.uuid',
  140. 'workspace_association.field_schema_data.revision_default',
  141. 'workspace_association.field_schema_data.target_entity_id',
  142. 'workspace_association.field_schema_data.target_entity_revision_id',
  143. 'workspace_association.field_schema_data.target_entity_type_id',
  144. 'workspace_association.field_schema_data.workspace',
  145. ];
  146. \Drupal::keyValue('entity.storage_schema.sql')->deleteMultiple($keys);
  147. }