You are here

function opigno_module_update_8010 in Opigno module 8

Update tables for opigno_module entity.

File

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

Code

function opigno_module_update_8010() {

  // Delete old image fields for opigno_module entity.
  $definition_manager = \Drupal::entityDefinitionUpdateManager();
  $old_module_image = $definition_manager
    ->getFieldStorageDefinition('module_image', 'opigno_module');
  if ($old_module_image) {
    $definition_manager
      ->uninstallFieldStorageDefinition($old_module_image);
  }
  $old_badge_image = $definition_manager
    ->getFieldStorageDefinition('badge_image', 'opigno_module');
  if ($old_badge_image) {
    $definition_manager
      ->uninstallFieldStorageDefinition($old_badge_image);
  }
  $table = 'opigno_module';
  $table_revision = 'opigno_module_revision';
  $table_field_data = 'opigno_module_field_data';
  $table_field_revision = 'opigno_module_field_revision';
  $backup_table = 'backup_opigno_module';
  $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_module')) {
    $entity_type_manager = \Drupal::entityTypeManager();
    $entity_definition_update_manager = \Drupal::entityDefinitionUpdateManager();
    $entity_type_manager
      ->clearCachedDefinitions();
    $definition = $entity_type_manager
      ->getDefinition('opigno_module');
    $entity_definition_update_manager
      ->installEntityType($definition);
  }

  // Insert data back into main tables from backup.
  $results = $connection
    ->select($backup_table, 'bt')
    ->fields('bt')
    ->execute()
    ->fetchAll();
  foreach ($results as $result) {

    // Insert into $table.
    $connection
      ->insert($table)
      ->fields([
      'id' => $result->id,
      'vid' => $result->vid,
      '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();

    // Convert to array and unset unnecessary fields.
    $result = (array) $result;
    unset($result['uuid']);

    // Insert into $table_field_data.
    $connection
      ->insert($table_field_data)
      ->fields($result)
      ->execute();

    // Insert into $table_field_revision.
    $connection
      ->insert($table_field_revision)
      ->fields($result)
      ->execute();
  }

  // Update opigno_score_modules view.
  $config_path = drupal_get_path('module', 'opigno_module') . '/config/optional';

  /* @var Drupal\Core\Config\CachedStorage $config_storage */
  $storage = new FileStorage($config_path);
  $config_storage = \Drupal::service('config.storage');
  $data = $storage
    ->read('views.view.opigno_score_modules');
  $config_storage
    ->write('views.view.opigno_score_modules', $data);
}