Created
June 11, 2016 16:03
-
-
Save vasi/6a9b2999a38d31eac5a7f5bbbaace147 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Helper for executing migrations in D8, without migrate_tools. | |
$args = drush_get_arguments(); | |
array_splice($args, 0, 2); | |
$cmd = array_shift($args); | |
switch ($cmd) { | |
case 'mi': | |
case 'mia': | |
$exec = function($executable, $migration) { | |
printf("Importing %s\n", $migration->id()); | |
$executable->import(); | |
}; | |
break; | |
case 'mr': | |
case 'mra': | |
$exec = function($executable, $migration) { | |
printf("Rolling back %s\n", $migration->id()); | |
$executable->rollback(); | |
}; | |
break; | |
default: | |
die("Unknown command $cmd"); | |
} | |
/** @var \Drupal\migrate\Plugin\MigrationPluginManager $manager */ | |
$manager = \Drupal::service('plugin.manager.migration'); | |
// Get requirements. | |
$migrations = []; | |
while (!empty($args)) { | |
$migration_id = array_shift($args); | |
$instances = $manager->createInstances($migration_id); | |
foreach ($instances as $migration) { | |
$migrations[$migration_id] = $migration; | |
if ($cmd === 'mia' || $cmd === 'mra') { | |
$args = array_merge($args, array_values($migration->get('requirements'))); | |
} | |
} | |
} | |
$migrations = $manager->buildDependencyMigration($migrations, []); | |
if ($cmd === 'mia') { | |
$migrations = array_reverse($migrations); | |
} | |
$message = new \Drupal\migrate\MigrateMessage(); | |
foreach ($migrations as $migration) { | |
$executable = new \Drupal\migrate\MigrateExecutable($migration, $message); | |
$exec($executable, $migration); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment