You are here

class MigrateUpgradeDrushRunner in Migrate Upgrade 8

Same name and namespace in other branches
  1. 8.3 src/MigrateUpgradeDrushRunner.php \Drupal\migrate_upgrade\MigrateUpgradeDrushRunner
  2. 8.2 src/MigrateUpgradeDrushRunner.php \Drupal\migrate_upgrade\MigrateUpgradeDrushRunner

Hierarchy

Expanded class hierarchy of MigrateUpgradeDrushRunner

1 file declares its use of MigrateUpgradeDrushRunner
migrate_upgrade.drush.inc in ./migrate_upgrade.drush.inc
Command-line tools to aid performing and developing upgrade migrations.

File

src/MigrateUpgradeDrushRunner.php, line 16
Contains \Drupal\migrate_upgrade\MigrateUpgradeDrushRunner.

Namespace

Drupal\migrate_upgrade
View source
class MigrateUpgradeDrushRunner {
  use MigrationCreationTrait;
  use StringTranslationTrait;

  /**
   * The list of migrations to run and their configuration.
   *
   * @var array
   */
  protected $migrationList;

  /**
   * MigrateMessage instance to display messages during the migration process.
   *
   * @var \Drupal\migrate_upgrade\DrushLogMigrateMessage
   */
  protected static $messages;

  /**
   * From the provided source information, instantiate the appropriate migrations
   * in the active configuration.
   *
   * @throws \Exception
   */
  public function configure() {
    $db_url = drush_get_option('legacy-db-url');
    $db_spec = drush_convert_db_from_db_url($db_url);
    $db_prefix = drush_get_option('legacy-db-prefix');
    $db_spec['prefix'] = $db_prefix;
    $migration_templates = $this
      ->getMigrationTemplates($db_spec, drush_get_option('legacy-root'));
    $this->migrationList = $this
      ->createMigrations($migration_templates);
  }

  /**
   * Run the configured migrations.
   */
  public function import() {
    static::$messages = new DrushLogMigrateMessage();
    if (drush_get_option('debug')) {
      \Drupal::service('event_dispatcher')
        ->addListener(MigrateEvents::IDMAP_MESSAGE, [
        get_class(),
        'onIdMapMessage',
      ]);
    }
    foreach ($this->migrationList as $migration_id) {

      /** @var MigrationInterface $migration */
      $migration = Migration::load($migration_id);
      drush_print(dt('Upgrading @migration', [
        '@migration' => $migration_id,
      ]));
      $executable = new MigrateExecutable($migration, static::$messages);

      // drush_op() provides --simulate support.
      drush_op([
        $executable,
        'import',
      ]);
    }
  }

  /**
   * Rolls back the configured migrations.
   */
  public function rollback() {
    static::$messages = new DrushLogMigrateMessage();
    $query = \Drupal::entityQuery('migration');
    $names = $query
      ->execute();

    // Order the migrations according to their dependencies.

    /** @var MigrationInterface[] $migrations */
    $migrations = \Drupal::entityManager()
      ->getStorage('migration')
      ->loadMultiple($names);

    // Assume we want all those tagged 'Drupal %'.
    foreach ($migrations as $migration_id => $migration) {
      $keep = FALSE;
      $tags = $migration
        ->get('migration_tags');
      foreach ($tags as $tag) {
        if (strpos($tag, 'Drupal ') === 0) {
          $keep = TRUE;
          break;
        }
      }
      if (!$keep) {
        unset($migrations[$migration_id]);
      }
    }

    // Roll back in reverse order.
    $this->migrationList = array_reverse($migrations);
    foreach ($this->migrationList as $migration_id => $migration) {
      drush_print(dt('Rolling back @migration', [
        '@migration' => $migration_id,
      ]));
      $executable = new MigrateExecutable($migration, static::$messages);

      // drush_op() provides --simulate support.
      drush_op([
        $executable,
        'rollback',
      ]);
      $migration
        ->delete();
    }
  }

  /**
   * Display any messages being logged to the ID map.
   *
   * @param \Drupal\migrate\Event\MigrateIdMapMessageEvent $event
   *   The message event.
   */
  public static function onIdMapMessage(MigrateIdMapMessageEvent $event) {
    if ($event
      ->getLevel() == MigrationInterface::MESSAGE_NOTICE || $event
      ->getLevel() == MigrationInterface::MESSAGE_INFORMATIONAL) {
      $type = 'status';
    }
    else {
      $type = 'error';
    }
    $source_id_string = implode(',', $event
      ->getSourceIdValues());
    $message = t('Source ID @source_id: @message', [
      '@source_id' => $source_id_string,
      '@message' => $event
        ->getMessage(),
    ]);
    static::$messages
      ->display($message, $type);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MigrateUpgradeDrushRunner::$messages protected static property MigrateMessage instance to display messages during the migration process.
MigrateUpgradeDrushRunner::$migrationList protected property The list of migrations to run and their configuration.
MigrateUpgradeDrushRunner::configure public function From the provided source information, instantiate the appropriate migrations in the active configuration.
MigrateUpgradeDrushRunner::import public function Run the configured migrations.
MigrateUpgradeDrushRunner::onIdMapMessage public static function Display any messages being logged to the ID map.
MigrateUpgradeDrushRunner::rollback public function Rolls back the configured migrations.
MigrationCreationTrait::createMigrations protected function Saves the migrations for import from the provided template connection.
MigrationCreationTrait::getConnection protected function Gets the database connection for the source Drupal database.
MigrationCreationTrait::getLegacyDrupalVersion protected function Determines what version of Drupal the source database contains.
MigrationCreationTrait::getMigrations protected function Gets the migrations for import.
MigrationCreationTrait::getMigrationTemplates protected function Sets up the relevant migrations for import from a database connection.
MigrationCreationTrait::getSystemData protected function Gets the system data from the system table of the source Drupal database.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.