You are here

function drush_entity_update in Entity Update 8

Same name and namespace in other branches
  1. 2.0.x drush/entity_update.drush8.inc \drush_entity_update()

Call back function of entity-update.

File

./entity_update.drush.inc, line 79
Entity update module (entity_update).

Code

function drush_entity_update($type = NULL) {

  // Show entities to update.
  if (drush_get_option('show')) {
    EntityCheck::showEntityStatusCli();
    return;
  }

  // Clean entity backup database.
  if (drush_get_option('clean')) {
    EntityUpdate::cleanupEntityBackup();
    drush_print("Entity backup data removed.");
    return;
  }

  // Restore all entities from database.
  if (drush_get_option('rescue')) {
    if (drush_confirm('Are you sure you want create entities from backup database ? ')) {
      $res = EntityUpdate::entityUpdateDataRestore();
      drush_log('End of entities recreate process', $res ? 'ok' : 'error');
    }
    return;
  }

  // Check mandatory options.
  $options = [
    'basic',
    'all',
    'bkpdel',
  ];
  if (!$type && !_drush_entity_update_checkoptions($options)) {
    drush_log('No option specified, please type "drush help upe" for help or refer to the documentation.', 'cancel');
    return;
  }
  drush_print(' - If you use this module, you are conscience what you are doing. You are the responsible of your work');
  drush_print(' - Please backup your database before any action');
  drush_print(' - Please Read the documentation before any action');
  drush_print(' - Do not use this module on multi sites.');
  drush_print(' - Before a new update, Remove old backuped data if any (Using : drush upe --clean).');

  // Backup database.
  if (!drush_get_option('nobackup')) {
    $db_backup_file = "backup_" . date("ymd-his") . ".sql.gz";
    drush_print("Backup database to : {$db_backup_file}");
    drush_print("To restore, run : gunzip < {$db_backup_file} | drush sqlc ");
    @exec('drush cr');
    @exec('drush sql-dump --gzip > ' . $db_backup_file);
  }

  // Basic entity update.
  if (drush_get_option('basic')) {
    if (drush_confirm('Are you sure you want update entities ?')) {
      $res = EntityUpdate::basicUpdate(drush_get_option('force'));
      drush_log('Basic entities update', $res ? 'ok' : 'error');
    }
    return;
  }

  // Copy and delete entities.
  if (drush_get_option('bkpdel')) {
    if (drush_confirm('Are you sure you want copy entities to update in to backup database and delete entities ?')) {
      $res = EntityUpdate::entityUpdateDataBackupDel(EntityUpdate::getEntityTypesToUpdate($type), $type);
      drush_log('End of entities copy and delete process', $res ? 'ok' : 'error');
    }
    return;
  }

  // Update all entities.
  if (drush_get_option('all')) {
    if ($type) {
      drush_log("The option --all and a type has specified, please remove a one.", 'cancel');
      return;
    }
    if (drush_confirm('Are you sure you want update all entities ?')) {
      $res = EntityUpdate::safeUpdateMain();
      drush_log('End of entities update process', $res ? 'ok' : 'error');
    }
    return;
  }
  elseif ($type) {

    // Update a selected entity type.
    try {
      if ($entity_type = entity_update_get_entity_type($type)) {

        // Update the entity type.
        $res = EntityUpdate::safeUpdateMain($entity_type);
        drush_log('End of entities update process for : ' . $type, $res ? 'ok' : 'error');
        return;
      }
    } catch (Exception $e) {
      drush_log($e
        ->getMessage(), 'error');
    }
    drush_log("Entity type update Error : {$type}", 'error');
    return;
  }
}