You are here

public function MigrateToolsCommands::rollback in Migrate Tools 8.4

Same name and namespace in other branches
  1. 8.5 src/Commands/MigrateToolsCommands.php \Drupal\migrate_tools\Commands\MigrateToolsCommands::rollback()

Rollback one or more migrations.

@command migrate:rollback

@option all Process all migrations. @option group A comma-separated list of migration groups to rollback @option tag ID of the migration tag to rollback @option feedback Frequency of progress messages, in items processed @option idlist Comma-separated list of IDs to rollback @option idlist-delimiter The delimiter for records @option skip-progress-bar Skip displaying a progress bar. @option continue-on-failure When a rollback fails, continue processing remaining migrations.

@default $options []

@usage migrate:rollback --all Perform all migrations @usage migrate:rollback --group=beer Rollback all migrations in the beer group @usage migrate:rollback --tag=user Rollback all migrations with the user tag @usage migrate:rollback --group=beer --tag=user Rollback all migrations in the beer group and with the user tag @usage migrate:rollback beer_term,beer_node Rollback imported terms and nodes @usage migrate:rollback beer_user --idlist=5 Rollback imported user record with source ID 5 @validate-module-enabled migrate_tools

@aliases mr, migrate-rollback

Parameters

string $migration_names: Name of migration(s) to rollback. Delimit multiple using commas.

array $options: Additional options for the command.

Throws

\Exception If there are not enough parameters to the command.

File

src/Commands/MigrateToolsCommands.php, line 367

Class

MigrateToolsCommands
Migrate Tools drush commands.

Namespace

Drupal\migrate_tools\Commands

Code

public function rollback($migration_names = '', array $options = [
  'all' => FALSE,
  'group' => self::REQ,
  'tag' => self::REQ,
  'feedback' => self::REQ,
  'idlist' => self::REQ,
  'idlist-delimiter' => MigrateTools::DEFAULT_ID_LIST_DELIMITER,
  'skip-progress-bar' => FALSE,
  'continue-on-failure' => FALSE,
]) {
  $group_names = $options['group'];
  $tag_names = $options['tag'];
  $all = $options['all'];
  if (!$all && !$group_names && !$migration_names && !$tag_names) {
    throw new \Exception(dt('You must specify --all, --group, --tag, or one or more migration names separated by commas'));
  }
  $migrations = $this
    ->migrationsList($migration_names, $options);
  if (empty($migrations)) {
    $this
      ->logger()
      ->error(dt('No migrations found.'));
  }

  // Take it one group at a time,
  // rolling back the migrations within each group.
  $has_failure = FALSE;
  foreach ($migrations as $migration_list) {

    // Roll back in reverse order.
    $migration_list = array_reverse($migration_list);
    foreach ($migration_list as $migration_id => $migration) {
      if ($options['skip-progress-bar']) {
        $migration
          ->set('skipProgressBar', TRUE);
      }

      // Initialize the Synmfony Console progress bar.
      \Drupal::service('migrate_tools.migration_drush_command_progress')
        ->initializeProgress($this
        ->output(), $migration);
      $executable = new MigrateExecutable($migration, $this
        ->getMigrateMessage(), $options);

      // drush_op() provides --simulate support.
      $result = drush_op([
        $executable,
        'rollback',
      ]);
      if ($result == MigrationInterface::RESULT_FAILED) {
        $has_failure = TRUE;
      }
    }
  }

  // If any rollbacks failed, throw an exception to generate exit status.
  if ($has_failure) {
    $error_message = dt('!name migration failed.', [
      '!name' => $migration_id,
    ]);
    if ($options['continue-on-failure']) {
      $this
        ->logger()
        ->error($error_message);
    }
    else {

      // Nudge Drush to use a non-zero exit code.
      throw new \Exception($error_message);
    }
  }
}