You are here

function multiversion_update_8001 in Multiversion 8

Add workspace field to data_field and revision_field tables and migrate data.

File

./multiversion.install, line 93

Code

function multiversion_update_8001() {
  $connection = Database::getConnection();
  if ($connection instanceof Connection) {
    $schema = $connection
      ->schema();
    $entity_manager = \Drupal::service('entity.manager');
    $manager = \Drupal::service('multiversion.manager');

    // Find all supported entities.
    $entities = $manager
      ->getSupportedEntityTypes();

    // Field can't be NOT NULL on an table with existing data.
    $field = [
      'type' => 'int',
      'unsigned' => TRUE,
    ];

    // Loop through each one.
    foreach ($entities as $entity_type => $entity) {
      $entity_keys = $entity
        ->getKeys();

      // Get the field names used as keys.
      $field_id = $entity_keys['id'];
      $field_revision = $entity_keys['revision'];

      // Get the tables name used for base table and revision table.
      $table_base = $entity
        ->isTranslatable() ? $entity
        ->getDataTable() : $entity
        ->getBaseTable();
      $table_revision = $entity
        ->isTranslatable() ? $entity
        ->getRevisionDataTable() : $entity
        ->getRevisionTable();

      // Block content definition doesn't include the revision field table.
      // So get it.
      $tables = $entity_manager
        ->getStorage($entity_type)
        ->getTableMapping()
        ->getTableNames();
      if (!$table_revision && in_array($entity_type . '_field_revision', $tables)) {
        $table_revision = $entity_type . '_field_revision';
      }
      $results = [];

      // Pull data from the old table.
      $old_data_table = $entity_type . '__workspace';
      if ($schema
        ->tableExists($old_data_table)) {
        $results = $connection
          ->select($old_data_table)
          ->fields($old_data_table, [
          'entity_id',
          'workspace_target_id',
        ])
          ->execute()
          ->fetchAll();
      }
      if ($schema
        ->tableExists($table_base)) {
        if (!$schema
          ->fieldExists($table_base, 'workspace')) {

          // Add new field to the base table.
          $schema
            ->addField($table_base, 'workspace', $field);
        }
        foreach ($results as $result) {

          // Add the value to the new column.
          $connection
            ->update($table_base)
            ->fields([
            'workspace' => $result->workspace_target_id,
          ])
            ->condition($field_id, $result->entity_id, '=')
            ->execute();
        }
      }
      if ($schema
        ->tableExists($old_data_table)) {

        // Drop old table.
        $schema
          ->dropTable($old_data_table);
      }
      $results = [];

      // Pull data from old table.
      $old_revision_table = $entity_type . '_revision__workspace';
      if ($schema
        ->tableExists($old_revision_table)) {
        $results = $connection
          ->select($old_revision_table)
          ->fields($old_revision_table, [
          'entity_id,',
          'revision_id',
          'workspace_target_id',
        ])
          ->execute();
      }
      if ($schema
        ->tableExists($table_revision)) {
        if (!$schema
          ->fieldExists($table_revision, 'workspace')) {

          // Add new field to the field revision table.
          $schema
            ->addField($table_revision, 'workspace', $field);
        }
        foreach ($results as $result) {

          // Add data to the revision table.
          $connection
            ->update($table_revision)
            ->fields([
            'workspace' => $result->workspace_target_id,
          ])
            ->condition($field_id, $result->entity_id, '=')
            ->condition($field_revision, $result->revision_id)
            ->execute();
        }
      }
      if ($schema
        ->tableExists($old_revision_table)) {

        // Drop old table.
        $schema
          ->dropTable($old_revision_table);
      }
    }
  }
}