You are here

function opigno_module_update_8009 in Opigno module 8

Update tables for opigno_activity entity.

File

./opigno_module.install, line 617
Opigno module app install/update functionality.

Code

function opigno_module_update_8009() {
  $table = 'opigno_activity';
  $table_revision = 'opigno_activity_revision';
  $table_field_data = 'opigno_activity_field_data';
  $table_field_revision = 'opigno_activity_field_revision';
  $backup_table = 'backup_opigno_activity';
  $connection = Database::getConnection();
  $schema = $connection
    ->schema();

  // Doesn't apply updates ff target tables are already exist.
  if ($schema
    ->tableExists($table_field_data) && $schema
    ->tableExists($table_field_revision)) {
    return;
  }

  // Make backup of main table.
  $schema
    ->renameTable($table, $backup_table);

  // Drop revision table.
  $schema
    ->dropTable($table_revision);

  // Insert new field in main table.
  $spec = [
    'type' => 'int',
    'size' => 'tiny',
    'not null' => TRUE,
    'default' => 1,
  ];
  $schema
    ->addField($backup_table, 'default_langcode', $spec);

  // Create new tables for opigno_module.
  $schema = \Drupal::database()
    ->schema();
  if (!$schema
    ->tableExists('opigno_activity')) {
    $entity_type_manager = \Drupal::entityTypeManager();
    $entity_definition_update_manager = \Drupal::entityDefinitionUpdateManager();
    $entity_type_manager
      ->clearCachedDefinitions();
    $definition = $entity_type_manager
      ->getDefinition('opigno_activity');
    $entity_definition_update_manager
      ->installEntityType($definition);
  }

  // Insert data back into main tables from backup.
  $backup = $connection
    ->select($backup_table, 'tr');
  $backup
    ->fields('tr', [
    'id',
    'vid',
    'type',
    'langcode',
    'uid',
    'uuid',
    'name',
    'status',
    'created',
    'changed',
    'default_langcode',
  ]);
  $results = $backup
    ->execute()
    ->fetchAll();
  foreach ($results as $result) {

    // Insert into $table.
    $connection
      ->insert($table)
      ->fields([
      'id' => $result->id,
      'vid' => $result->vid,
      'type' => $result->type,
      'uuid' => $result->uuid,
      'langcode' => $result->langcode,
    ])
      ->execute();

    // Insert into $table_revision.
    $connection
      ->insert($table_revision)
      ->fields([
      'id' => $result->id,
      'vid' => $result->vid,
      'langcode' => $result->langcode,
      'revision_created' => $result->created,
      'revision_user' => NULL,
      'revision_log_message' => NULL,
    ])
      ->execute();

    // Insert into $table_field_data.
    $connection
      ->insert($table_field_data)
      ->fields([
      'id' => $result->id,
      'vid' => $result->vid,
      'type' => $result->type,
      'langcode' => $result->langcode,
      'uid' => $result->uid,
      'name' => $result->name,
      'status' => $result->status,
      'created' => $result->created,
      'changed' => $result->changed,
      'default_langcode' => $result->default_langcode,
    ])
      ->execute();

    // Insert into $table_field_revision.
    $connection
      ->insert($table_field_revision)
      ->fields([
      'id' => $result->id,
      'vid' => $result->vid,
      'langcode' => $result->langcode,
      'uid' => $result->uid,
      'name' => $result->name,
      'status' => $result->status,
      'created' => $result->created,
      'changed' => $result->changed,
      'default_langcode' => $result->default_langcode,
    ])
      ->execute();
  }
}