You are here

function workspaces_post_update_move_association_data in Drupal 8

Move the workspace association data to an entity field and a custom table.

File

core/modules/workspaces/workspaces.post_update.php, line 30
Post update functions for the Workspaces module.

Code

function workspaces_post_update_move_association_data(&$sandbox) {
  $database = \Drupal::database();
  $entity_type_manager = \Drupal::entityTypeManager();

  // @see workspaces_update_8803()
  $tables = \Drupal::state()
    ->get('workspaces_update_8803.tables');
  if (!$tables) {
    return;
  }

  // If 'progress' is not set, this will be the first run of the batch.
  if (!isset($sandbox['progress'])) {
    $sandbox['progress'] = 0;
    $sandbox['current_id'] = -1;

    // Create a temporary table for the new workspace_association index.
    $schema = [
      'description' => 'Stores the association between entity revisions and their workspace.',
      'fields' => [
        'workspace' => [
          'type' => 'varchar_ascii',
          'length' => 128,
          'not null' => TRUE,
          'default' => '',
          'description' => 'The workspace ID.',
        ],
        'target_entity_type_id' => [
          'type' => 'varchar_ascii',
          'length' => EntityTypeInterface::ID_MAX_LENGTH,
          'not null' => TRUE,
          'default' => '',
          'description' => 'The ID of the associated entity type.',
        ],
        'target_entity_id' => [
          'type' => 'int',
          'unsigned' => TRUE,
          'not null' => TRUE,
          'description' => 'The ID of the associated entity.',
        ],
        'target_entity_revision_id' => [
          'type' => 'int',
          'unsigned' => TRUE,
          'not null' => TRUE,
          'description' => 'The revision ID of the associated entity.',
        ],
      ],
      'indexes' => [
        'target_entity_revision_id' => [
          'target_entity_revision_id',
        ],
      ],
      'primary key' => [
        'workspace',
        'target_entity_type_id',
        'target_entity_id',
      ],
    ];
    if ($database
      ->schema()
      ->tableExists('tmp_workspace_association')) {
      $database
        ->schema()
        ->dropTable('tmp_workspace_association');
    }
    $database
      ->schema()
      ->createTable('tmp_workspace_association', $schema);

    // Copy all the data from the base table of the 'workspace_association'
    // entity type to the temporary association table.
    $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 = $tables['revision_table'];
  $revision_field_name = 'revision_id';

  // Get the next entity association revision records to migrate.
  $step_size = Settings::get('entity_update_batch_size', 50);
  $workspace_association_records = $database
    ->select($table_name, 't')
    ->condition("t.{$revision_field_name}", $sandbox['current_id'], '>')
    ->fields('t')
    ->orderBy($revision_field_name, 'ASC')
    ->range(0, $step_size)
    ->execute()
    ->fetchAll();
  foreach ($workspace_association_records as $record) {

    // Set the workspace reference on the tracked entity revision.

    /** @var \Drupal\Core\Entity\ContentEntityInterface $revision */
    $revision = $entity_type_manager
      ->getStorage($record->target_entity_type_id)
      ->loadRevision($record->target_entity_revision_id);
    $revision
      ->set('workspace', $record->workspace);
    $revision
      ->setSyncing(TRUE);
    $revision
      ->save();
    $sandbox['progress']++;
    $sandbox['current_id'] = $record->{$revision_field_name};
  }

  // Get an updated count of workspace_association revisions that still need to
  // be migrated to the new storage.
  $missing = $database
    ->select($table_name, 't')
    ->condition("t.{$revision_field_name}", $sandbox['current_id'], '>')
    ->orderBy($revision_field_name, 'ASC')
    ->countQuery()
    ->execute()
    ->fetchField();
  $sandbox['#finished'] = $missing ? $sandbox['progress'] / ($sandbox['progress'] + (int) $missing) : 1;

  // Uninstall the 'workspace_association' entity type and rename the temporary
  // table.
  if ($sandbox['#finished'] == 1) {
    $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');
  }
}