5 * Contains \Drupal\Console\Command\Migrate\ExecuteCommand.
8 namespace Drupal\Console\Command\Migrate;
10 use Symfony\Component\Console\Input\InputArgument;
11 use Symfony\Component\Console\Input\InputOption;
12 use Symfony\Component\Console\Input\InputInterface;
13 use Symfony\Component\Console\Output\OutputInterface;
14 use Drupal\Core\Database\Database;
15 use Drupal\migrate\MigrateExecutable;
16 use Drupal\Console\Utils\MigrateExecuteMessageCapture;
17 use Drupal\Console\Command\Shared\MigrationTrait;
18 use Drupal\Console\Command\Shared\DatabaseTrait;
19 use Drupal\Console\Core\Command\Shared\CommandTrait;
20 use Drupal\Console\Core\Style\DrupalStyle;
21 use Drupal\migrate\Plugin\MigrationInterface;
22 use Drupal\State\StateInterface;
23 use Symfony\Component\Console\Command\Command;
24 use Drupal\migrate\Plugin\MigrationPluginManagerInterface;
26 class ExecuteCommand extends Command
32 protected $migrateConnection;
35 * @var MigrationPluginManagerInterface $pluginManagerMigration
37 protected $pluginManagerMigration;
40 * DebugCommand constructor.
42 * @param MigrationPluginManagerInterface $pluginManagerMigration
44 public function __construct(MigrationPluginManagerInterface $pluginManagerMigration)
46 $this->pluginManagerMigration = $pluginManagerMigration;
47 parent::__construct();
50 protected function configure()
53 ->setName('migrate:execute')
54 ->setDescription($this->trans('commands.migrate.execute.description'))
55 ->addArgument('migration-ids', InputArgument::IS_ARRAY, $this->trans('commands.migrate.execute.arguments.id'))
59 InputOption::VALUE_REQUIRED,
60 $this->trans('commands.migrate.execute.options.site-url')
65 InputOption::VALUE_REQUIRED,
66 $this->trans('commands.migrate.setup.migrations.options.db-type')
71 InputOption::VALUE_REQUIRED,
72 $this->trans('commands.migrate.execute.options.db-host')
77 InputOption::VALUE_REQUIRED,
78 $this->trans('commands.migrate.execute.options.db-name')
83 InputOption::VALUE_REQUIRED,
84 $this->trans('commands.migrate.execute.options.db-user')
89 InputOption::VALUE_OPTIONAL,
90 $this->trans('commands.migrate.execute.options.db-pass')
95 InputOption::VALUE_OPTIONAL,
96 $this->trans('commands.migrate.execute.options.db-prefix')
101 InputOption::VALUE_REQUIRED,
102 $this->trans('commands.migrate.execute.options.db-port')
107 InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
108 $this->trans('commands.migrate.execute.options.exclude'),
114 InputOption::VALUE_OPTIONAL,
115 $this->trans('commands.migrate.execute.options.source-base_path')
123 protected function interact(InputInterface $input, OutputInterface $output)
125 $io = new DrupalStyle($input, $output);
127 $validator_required = function ($value) {
128 if (!strlen(trim($value))) {
129 throw new \Exception('The option can not be empty');
136 $site_url = $input->getOption('site-url');
138 $site_url = $io->ask(
139 $this->trans('commands.migrate.execute.questions.site-url'),
140 'http://www.example.com',
143 $input->setOption('site-url', $site_url);
147 $db_type = $input->getOption('db-type');
149 $db_type = $this->dbDriverTypeQuestion($io);
150 $input->setOption('db-type', $db_type);
154 $db_host = $input->getOption('db-host');
156 $db_host = $this->dbHostQuestion($io);
157 $input->setOption('db-host', $db_host);
161 $db_name = $input->getOption('db-name');
163 $db_name = $this->dbNameQuestion($io);
164 $input->setOption('db-name', $db_name);
168 $db_user = $input->getOption('db-user');
170 $db_user = $this->dbUserQuestion($io);
171 $input->setOption('db-user', $db_user);
175 $db_pass = $input->getOption('db-pass');
177 $db_pass = $this->dbPassQuestion($io);
178 $input->setOption('db-pass', $db_pass);
182 $db_prefix = $input->getOption('db-prefix');
184 $db_prefix = $this->dbPrefixQuestion($io);
185 $input->setOption('db-prefix', $db_prefix);
189 $db_port = $input->getOption('db-port');
191 $db_port = $this->dbPortQuestion($io);
192 $input->setOption('db-port', $db_port);
195 $this->registerMigrateDB($input, $io);
196 $this->migrateConnection = $this->getDBConnection($io, 'default', 'upgrade');
198 if (!$drupal_version = $this->getLegacyDrupalVersion($this->migrateConnection)) {
199 $io->error($this->trans('commands.migrate.setup.migrations.questions.not-drupal'));
203 $database = $this->getDBInfo();
204 $version_tag = 'Drupal ' . $drupal_version;
207 $migrations_list = $this->getMigrations($version_tag);
209 // --migration-id prefix
210 $migration_id = $input->getArgument('migration-ids');
212 if (!in_array('all', $migration_id)) {
213 $migrations = $migrations_list;
215 $migrations = array_keys($this->getMigrations($version_tag));
218 if (!$migration_id) {
219 $migrations_ids = [];
222 $migration_id = $io->choiceNoList(
223 $this->trans('commands.migrate.execute.questions.id'),
224 array_keys($migrations_list),
228 if (empty($migration_id) || $migration_id == 'all') {
229 // Only add all if it's the first option
230 if (empty($migrations_ids) && $migration_id == 'all') {
231 $migrations_ids[] = $migration_id;
235 $migrations_ids[] = $migration_id;
239 $input->setArgument('migration-ids', $migrations_ids);
242 // --migration-id prefix
243 $exclude_ids = $input->getOption('exclude');
245 unset($migrations_list['all']);
247 $exclude_id = $io->choiceNoList(
248 $this->trans('commands.migrate.execute.questions.exclude-id'),
249 array_keys($migrations_list),
254 if (empty($exclude_id)) {
257 unset($migrations_list[$exclude_id]);
258 $exclude_ids[] = $exclude_id;
261 $input->setOption('exclude', $exclude_ids);
264 // --source-base_path
265 $sourceBasepath = $input->getOption('source-base_path');
266 if (!$sourceBasepath) {
267 $sourceBasepath = $io->ask(
268 $this->trans('commands.migrate.setup.questions.source-base_path'),
271 $input->setOption('source-base_path', $sourceBasepath);
278 protected function execute(InputInterface $input, OutputInterface $output)
280 $io = new DrupalStyle($input, $output);
281 $migration_ids = $input->getArgument('migration-ids');
282 $exclude_ids = $input->getOption('exclude');
284 $sourceBasepath = $input->getOption('source-base_path');
285 $configuration['source']['constants']['source_base_path'] = rtrim($sourceBasepath, '/') . '/';
288 // If migrations weren't provided finish execution
289 if (empty($migration_ids)) {
293 if (!$this->migrateConnection) {
294 $this->registerMigrateDB($input, $io);
295 $this->migrateConnection = $this->getDBConnection($io, 'default', 'upgrade');
298 if (!$drupal_version = $this->getLegacyDrupalVersion($this->migrateConnection)) {
299 $io->error($this->trans('commands.migrate.setup.migrations.questions.not-drupal'));
303 $version_tag = 'Drupal ' . $drupal_version;
305 if (!in_array('all', $migration_ids)) {
306 $migrations = $migration_ids;
308 $migrations = array_keys($this->getMigrations($version_tag));
311 if (!empty($exclude_ids)) {
312 // Remove exclude migration from migration script
313 $migrations = array_diff($migrations, $exclude_ids);
316 if (count($migrations) == 0) {
317 $io->error($this->trans('commands.migrate.execute.messages.no-migrations'));
321 foreach ($migrations as $migration_id) {
324 $this->trans('commands.migrate.execute.messages.processing'),
329 $migration_service = $this->pluginManagerMigration->createInstance($migration_id, $configuration);
331 if ($migration_service) {
332 $messages = new MigrateExecuteMessageCapture();
333 $executable = new MigrateExecutable($migration_service, $messages);
334 $migration_status = $executable->import();
335 switch ($migration_status) {
336 case MigrationInterface::RESULT_COMPLETED:
339 $this->trans('commands.migrate.execute.messages.imported'),
344 case MigrationInterface::RESULT_INCOMPLETE:
347 $this->trans('commands.migrate.execute.messages.importing-incomplete'),
352 case MigrationInterface::RESULT_STOPPED:
355 $this->trans('commands.migrate.execute.messages.import-stopped'),
360 case MigrationInterface::RESULT_FAILED:
363 $this->trans('commands.migrate.execute.messages.import-fail'),
368 case MigrationInterface::RESULT_SKIPPED:
371 $this->trans('commands.migrate.execute.messages.import-skipped'),
376 case MigrationInterface::RESULT_DISABLED:
377 // Skip silently if disabled.
381 $io->error($this->trans('commands.migrate.execute.messages.fail-load'));