View source  
  <?php
namespace Drupal\migrate_upgrade\Commands;
use Consolidation\AnnotatedCommand\AnnotationData;
use Consolidation\AnnotatedCommand\CommandData;
use Consolidation\OutputFormatters\StructuredData\RowsOfFields;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Drupal\migrate_upgrade\MigrateUpgradeDrushRunner;
use Drush\Commands\DrushCommands;
use Drush\Exceptions\UserAbortException;
use Drupal\Core\State\StateInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
class MigrateUpgradeCommands extends DrushCommands {
  
  protected $state;
  
  protected $logger;
  
  public function __construct(StateInterface $state, LoggerChannelFactoryInterface $logger_factory) {
    parent::__construct();
    $this->state = $state;
    $this->logger = $logger_factory
      ->get('drush');
  }
  
  public function upgrade(array $options = []) {
    $runner = new MigrateUpgradeDrushRunner($this->logger, $options);
    $runner
      ->configure();
    if ($options['configure-only']) {
      $result = new RowsOfFields($runner
        ->export());
    }
    else {
      $result = new RowsOfFields($runner
        ->import());
      $this->state
        ->set('migrate_drupal_ui.performed', \Drupal::time()
        ->getRequestTime());
    }
    
    $this->state
      ->delete('migrate.fallback_state_key');
    return $result;
  }
  
  public function validatePassword(CommandData $commandData) {
    $input = $commandData
      ->input();
    $db_url = $input
      ->getOption('legacy-db-url');
    $db_key = $input
      ->getOption('legacy-db-key');
    if (!$db_url && !$db_key) {
      throw new \Exception('You must provide either a --legacy-db-url or --legacy-db-key.');
    }
  }
  
  public function legacyDatabaseUrl(Command $command, AnnotationData $annotationData) {
    $command
      ->addOption('legacy-db-url', '', InputOption::VALUE_OPTIONAL, 'A Drupal 6 style database URL. Required if you do not set legacy-db-key.');
  }
  
  public function legacyDatabaseKey(Command $command, AnnotationData $annotationData) {
    $command
      ->addOption('legacy-db-key', '', InputOption::VALUE_OPTIONAL, 'A database connection key from settings.php. Use as an alternative to legacy-db-url.');
  }
  
  public function legacyDatabasePrefix(Command $command, AnnotationData $annotationData) {
    $command
      ->addOption('legacy-db-prefix', '', InputOption::VALUE_OPTIONAL, 'Database prefix of the legacy Drupal installation.');
  }
  
  public function legacyRoot(Command $command, AnnotationData $annotationData) {
    $command
      ->addOption('legacy-root', '', InputOption::VALUE_OPTIONAL, 'For files migrations. Site web address or file system root path (if files are local) of the legacy Drupal installation.');
  }
  
  public function configureOnly(Command $command, AnnotationData $annotationData) {
    $command
      ->addOption('configure-only', '', InputOption::VALUE_NONE, 'Set up the appropriate upgrade processes as migrate_plus config entities but do not perform them.');
  }
  
  public function migrationPrefix(Command $command, AnnotationData $annotationData) {
    $command
      ->addOption('migration-prefix', '', InputOption::VALUE_OPTIONAL, 'With configure-only, a prefix to apply to generated migration ids.', 'upgrade_');
  }
  
  public function initUpgrade(InputInterface $input, AnnotationData $annotationData) {
    
    if (!$input
      ->getOption('configure-only')) {
      $annotationData
        ->set('field-labels', 'original: Original migrations' . PHP_EOL . 'generated: Executed migrations');
    }
  }
  
  public function upgradeRollback() {
    if ($date_performed = $this->state
      ->get('migrate_drupal_ui.performed')) {
      if ($this
        ->io()
        ->confirm(dt('All migrations will be rolled back. Are you sure?'))) {
        $runner = new MigrateUpgradeDrushRunner($this->logger);
        $this
          ->logger()
          ->notice(dt('Rolling back the upgrades performed @date', [
          '@date' => \Drupal::service('date.formatter')
            ->format($date_performed),
        ]));
        $runner
          ->rollback();
        $this->state
          ->delete('migrate_drupal_ui.performed');
        $this
          ->logger()
          ->notice(dt('Rolled back upgrades'));
      }
      else {
        throw new UserAbortException();
      }
    }
    else {
      $this
        ->logger()
        ->warning(dt('No upgrade operation has been performed.'));
    }
  }
}