Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[migrate:*] Updated commands to process migrations #2493

Merged
merged 1 commit into from
Jul 5, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 6 additions & 2 deletions config/services/migrate.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
services:
migrate_setup:
class: Drupal\Console\Command\Migrate\SetupCommand
migrate_execute:
class: Drupal\Console\Command\Migrate\ExecuteCommand
tags:
- { name: console.command }
migrate_debug:
class: Drupal\Console\Command\Migrate\DebugCommand
tags:
- { name: console.command }
35 changes: 0 additions & 35 deletions src/Command/ContainerAwareCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,41 +36,6 @@ protected function getContainer()
return $this->getKernelHelper()->getKernel()->getContainer();
}

/**
* @param bool $tag
* @param bool $flatList
*
* @return array list of modules
*/
public function getMigrations($tag = false, $flatList = false)
{
$entityType_manager = $this->getService('entity_type.manager');
$migration_storage = $entityType_manager->getStorage('migration');

$entity_query_service = $this->getEntityQuery();
$query = $entity_query_service->get('migration');

if ($tag) {
$query->condition('migration_tags.*', $tag);
}

$results = $query->execute();

$migration_entities = $migration_storage->loadMultiple($results);

$migrations = array();
foreach ($migration_entities as $migration) {
if ($flatList) {
$migrations[$migration->id()] = ucwords($migration->label());
} else {
$migrations[$migration->id()]['tags'] = implode(', ', $migration->migration_tags);
$migrations[$migration->id()]['description'] = ucwords($migration->label());
}
}

return $migrations;
}

public function getRestDrupalConfig()
{
$configFactory = $this->getConfigFactory();
Expand Down
18 changes: 11 additions & 7 deletions src/Command/Migrate/DebugCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Drupal\Console\Command\ContainerAwareCommand;
use Drupal\Console\Command\Shared\MigrationTrait;
use Drupal\Console\Style\DrupalStyle;
use Drupal\Console\Annotation\DrupalCommand;
use Symfony\Component\Console\Command\Command;
use Drupal\Console\Command\Shared\ContainerAwareCommandTrait;

/**
* @DrupalCommand(
Expand All @@ -21,8 +23,11 @@
* }
* )
*/
class DebugCommand extends ContainerAwareCommand
{
class DebugCommand extends Command
{
use MigrationTrait;
use ContainerAwareCommandTrait;

protected function configure()
{
$this
Expand All @@ -33,16 +38,15 @@ protected function configure()
InputArgument::OPTIONAL,
$this->trans('commands.migrate.debug.arguments.tag')
);

$this->addDependency('migrate');
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$io = new DrupalStyle($input, $output);
$drupal_version = $input->getArgument('tag');

$drupal_version = 'Drupal ' . $input->getArgument('tag');
$migrations = $this->getMigrations($drupal_version);


$tableHeader = [
$this->trans('commands.migrate.debug.messages.id'),
Expand Down
96 changes: 57 additions & 39 deletions src/Command/Migrate/ExecuteCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,32 @@
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Drupal\Console\Command\ContainerAwareCommand;
use Drupal\Core\Database\Database;
use Drupal\migrate\Entity\MigrationInterface;
use Drupal\migrate\MigrateExecutable;
use Drupal\Console\Utils\MigrateExecuteMessageCapture;
use Drupal\Console\Command\Shared\MigrationTrait;
use Drupal\Console\Command\Shared\DatabaseTrait;
use Drupal\Console\Command\Shared\ContainerAwareCommandTrait;
use Drupal\Console\Style\DrupalStyle;
use Drupal\migrate\Plugin\MigrationInterface;
use Drupal\State\StateInterface;
use Symfony\Component\Console\Command\Command;

class ExecuteCommand extends ContainerAwareCommand
class ExecuteCommand extends Command
{
use DatabaseTrait;
use MigrationTrait;
use ContainerAwareCommandTrait;

protected $migrateConnection;

/**
* @DrupalCommand(
* dependencies = {
* "migrate"
* }
* )
*/
protected function configure()
{
$this
Expand Down Expand Up @@ -86,8 +98,6 @@ protected function configure()
$this->trans('commands.migrate.execute.options.exclude'),
array()
);

$this->addDependency('migrate');
}

/**
Expand Down Expand Up @@ -119,10 +129,10 @@ protected function interact(InputInterface $input, OutputInterface $output)
// --db-type option
$db_type = $input->getOption('db-type');
if (!$db_type) {
$db_type = $this->dbTypeQuestion($io);
$db_type = $this->dbDriverTypeQuestion($io);
$input->setOption('db-type', $db_type);
}

// --db-host option
$db_host = $input->getOption('db-host');
if (!$db_host) {
Expand Down Expand Up @@ -164,29 +174,32 @@ protected function interact(InputInterface $input, OutputInterface $output)
$db_port = $this->dbPortQuestion($io);
$input->setOption('db-port', $db_port);
}

$this->registerMigrateDB($input, $io);
$this->migrateConnection = $this->getDBConnection($io, 'default', 'migrate');
$this->registerMigrateDB($input, $output);
$this->migrateConnection = $this->getDBConnection($output,'default','upgrade');

if (!$drupal_version = $this->getLegacyDrupalVersion($this->migrateConnection)) {
$io->error(
$this->trans('commands.migrate.setup.migrations.questions.not-drupal')
);
$io->error($this->trans('commands.migrate.setup.migrations.questions.not-drupal'));
return;
}


$database = $this->getDBInfo();
$version_tag = 'Drupal ' . $drupal_version;
// Get migrations available
$migrations_list = $this->getMigrations($version_tag, true);


// Get migrations
$migrations_list = $this->getMigrations($version_tag);

if (!in_array('all', $migration_ids)) {
$migrations = $migration_ids;
} else {
$migrations = array_keys($this->getMigrations($version_tag));
}
// --migration-id prefix
$migration_id = $input->getArgument('migration-ids');

if (!$migration_id) {
// $migrations_list['all'] = 'all';
$migrations_ids = [];

// var_export($migrations_list);


while (true) {
$migration_id = $io->choiceNoList(
$this->trans('commands.migrate.execute.questions.id'),
Expand All @@ -207,7 +220,7 @@ protected function interact(InputInterface $input, OutputInterface $output)

$input->setArgument('migration-ids', $migrations_ids);
}

// --migration-id prefix
$exclude_ids = $input->getOption('exclude');
if (!$exclude_ids) {
Expand All @@ -230,20 +243,15 @@ protected function interact(InputInterface $input, OutputInterface $output)
$input->setOption('exclude', $exclude_ids);
}
}

/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$io = new DrupalStyle($input, $output);

$migration_ids = $input->getArgument('migration-ids');
$exclude_ids = $input->getOption('exclude');
if (!empty($exclude_ids)) {
// Remove exclude migration from migration script
$migration_ids = array_diff($migration_ids, $exclude_ids);
}

// If migrations weren't provided finish execution
if (empty($migration_ids)) {
Expand All @@ -252,40 +260,49 @@ protected function execute(InputInterface $input, OutputInterface $output)

if (!$this->migrateConnection) {
$this->registerMigrateDB($input, $output);
$this->migrateConnection = $this->getDBConnection($output, 'default', 'migrate');
$this->migrateConnection = $this->getDBConnection($output,'default','upgrade');

}

if (!$drupal_version = $this->getLegacyDrupalVersion($this->migrateConnection)) {
$io->error($this->trans('commands.migrate.setup.migrations.questions.not-drupal'));
return;
}

$version_tag = 'Drupal ' . $drupal_version;

if (!in_array('all', $migration_ids)) {
$migrations = $migration_ids;
} else {
$migrations = array_keys($this->getMigrations($version_tag));
}

$entityTypeManager = $this->getService('entity_type.manager');
$migration_storage = $entityTypeManager->getStorage('migration');
}

if (!empty($exclude_ids)) {
// Remove exclude migration from migration script
$migrations = array_diff($migrations, $exclude_ids);
}

if (count($migrations) == 0) {
$io->error($this->trans('commands.migrate.execute.messages.no-migrations'));
return;
}

foreach ($migrations as $migration_id) {
$io->info(
sprintf(
$this->trans('commands.migrate.execute.messages.processing'),
$migration_id
)
);
$migration = $migration_storage->load($migration_id);

if ($migration) {
$migration_service = $this->getDrupalService('plugin.manager.migration');

$migration_service = $migration_service->createInstance($migration_id);

if ($migration_service) {
$messages = new MigrateExecuteMessageCapture();
$executable = new MigrateExecutable($migration, $messages);
$executable = new MigrateExecutable($migration_service, $messages);
$migration_status = $executable->import();
switch ($migration_status) {
case MigrationInterface::RESULT_COMPLETED:
Expand Down Expand Up @@ -336,5 +353,6 @@ protected function execute(InputInterface $input, OutputInterface $output)
$io->error($this->trans('commands.migrate.execute.messages.fail-load'));
}
}
}
}

}