MigrateUpgradeDrushRunner.php in Migrate Upgrade 8
File
src/MigrateUpgradeDrushRunner.php
View source
<?php
namespace Drupal\migrate_upgrade;
use Drupal\migrate\Entity\Migration;
use Drupal\migrate\Entity\MigrationInterface;
use Drupal\migrate\Event\MigrateEvents;
use Drupal\migrate\Event\MigrateIdMapMessageEvent;
use Drupal\migrate\MigrateExecutable;
use Drupal\Core\StringTranslation\StringTranslationTrait;
class MigrateUpgradeDrushRunner {
use MigrationCreationTrait;
use StringTranslationTrait;
protected $migrationList;
protected static $messages;
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);
}
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) {
$migration = Migration::load($migration_id);
drush_print(dt('Upgrading @migration', [
'@migration' => $migration_id,
]));
$executable = new MigrateExecutable($migration, static::$messages);
drush_op([
$executable,
'import',
]);
}
}
public function rollback() {
static::$messages = new DrushLogMigrateMessage();
$query = \Drupal::entityQuery('migration');
$names = $query
->execute();
$migrations = \Drupal::entityManager()
->getStorage('migration')
->loadMultiple($names);
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]);
}
}
$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([
$executable,
'rollback',
]);
$migration
->delete();
}
}
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);
}
}