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\migrate\Plugin\MigrationInterface;
20 use Drupal\State\StateInterface;
21 use Drupal\Console\Core\Command\Command;
22 use Drupal\migrate\Plugin\MigrationPluginManagerInterface;
23 use Drupal\Console\Annotations\DrupalCommand;
27 * extension = "migrate",
28 * extensionType = "module"
31 class ExecuteCommand extends Command
36 protected $migrateConnection;
39 * @var MigrationPluginManagerInterface $pluginManagerMigration
41 protected $pluginManagerMigration;
44 * DebugCommand constructor.
46 * @param MigrationPluginManagerInterface $pluginManagerMigration
48 public function __construct(
49 MigrationPluginManagerInterface $pluginManagerMigration
51 $this->pluginManagerMigration = $pluginManagerMigration;
52 parent::__construct();
55 protected function configure()
58 ->setName('migrate:execute')
59 ->setDescription($this->trans('commands.migrate.execute.description'))
60 ->addArgument('migration-ids', InputArgument::IS_ARRAY, $this->trans('commands.migrate.execute.arguments.id'))
64 InputOption::VALUE_REQUIRED,
65 $this->trans('commands.migrate.execute.options.site-url')
70 InputOption::VALUE_REQUIRED,
71 $this->trans('commands.migrate.execute.migrations.options.db-type')
76 InputOption::VALUE_REQUIRED,
77 $this->trans('commands.migrate.execute.options.db-host')
82 InputOption::VALUE_REQUIRED,
83 $this->trans('commands.migrate.execute.options.db-name')
88 InputOption::VALUE_REQUIRED,
89 $this->trans('commands.migrate.execute.options.db-user')
94 InputOption::VALUE_OPTIONAL,
95 $this->trans('commands.migrate.execute.options.db-pass')
100 InputOption::VALUE_OPTIONAL,
101 $this->trans('commands.migrate.execute.options.db-prefix')
106 InputOption::VALUE_REQUIRED,
107 $this->trans('commands.migrate.execute.options.db-port')
112 InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
113 $this->trans('commands.migrate.execute.options.exclude'),
119 InputOption::VALUE_OPTIONAL,
120 $this->trans('commands.migrate.execute.options.source-base-path')
122 ->setAliases(['mie']);
129 protected function interact(InputInterface $input, OutputInterface $output)
131 $validator_required = function ($value) {
132 if (!strlen(trim($value))) {
133 throw new \Exception('The option can not be empty');
140 $site_url = $input->getOption('site-url');
142 $site_url = $this->getIo()->ask(
143 $this->trans('commands.migrate.execute.questions.site-url'),
144 'http://www.example.com',
147 $input->setOption('site-url', $site_url);
151 $db_type = $input->getOption('db-type');
153 $db_type = $this->dbDriverTypeQuestion();
154 $input->setOption('db-type', $db_type);
158 $db_host = $input->getOption('db-host');
160 $db_host = $this->dbHostQuestion();
161 $input->setOption('db-host', $db_host);
165 $db_name = $input->getOption('db-name');
167 $db_name = $this->dbNameQuestion();
168 $input->setOption('db-name', $db_name);
172 $db_user = $input->getOption('db-user');
174 $db_user = $this->dbUserQuestion();
175 $input->setOption('db-user', $db_user);
179 $db_pass = $input->getOption('db-pass');
181 $db_pass = $this->dbPassQuestion();
182 $input->setOption('db-pass', $db_pass);
186 $db_prefix = $input->getOption('db-prefix');
188 $db_prefix = $this->dbPrefixQuestion();
189 $input->setOption('db-prefix', $db_prefix);
193 $db_port = $input->getOption('db-port');
195 $db_port = $this->dbPortQuestion();
196 $input->setOption('db-port', $db_port);
199 $this->registerMigrateDB();
200 $this->migrateConnection = $this->getDBConnection('default', 'upgrade');
202 if (!$drupal_version = $this->getLegacyDrupalVersion($this->migrateConnection)) {
203 $this->getIo()->error($this->trans('commands.migrate.setup.migrations.questions.not-drupal'));
207 $database = $this->getDBInfo();
208 $version_tag = 'Drupal ' . $drupal_version;
211 $migrations_list = $this->getMigrations($version_tag);
213 // --migration-id prefix
214 $migration_id = $input->getArgument('migration-ids');
216 if (!in_array('all', $migration_id)) {
217 $migrations = $migrations_list;
219 $migrations = array_keys($this->getMigrations($version_tag));
222 if (!$migration_id) {
223 $migrations_ids = [];
226 $migration_id = $this->getIo()->choiceNoList(
227 $this->trans('commands.migrate.execute.questions.id'),
228 array_keys($migrations_list),
232 if (empty($migration_id) || $migration_id == 'all') {
233 // Only add all if it's the first option
234 if (empty($migrations_ids) && $migration_id == 'all') {
235 $migrations_ids[] = $migration_id;
239 $migrations_ids[] = $migration_id;
243 $input->setArgument('migration-ids', $migrations_ids);
246 // --migration-id prefix
247 $exclude_ids = $input->getOption('exclude');
249 unset($migrations_list['all']);
251 $exclude_id = $this->getIo()->choiceNoList(
252 $this->trans('commands.migrate.execute.questions.exclude-id'),
253 array_keys($migrations_list),
258 if (empty($exclude_id) || is_numeric($exclude_id)) {
261 unset($migrations_list[$exclude_id]);
262 $exclude_ids[] = $exclude_id;
265 $input->setOption('exclude', $exclude_ids);
268 // --source-base_path
269 $sourceBasepath = $input->getOption('source-base_path');
270 if (!$sourceBasepath) {
271 $sourceBasepath = $this->getIo()->ask(
272 $this->trans('commands.migrate.setup.questions.source-base-path'),
275 $input->setOption('source-base_path', $sourceBasepath);
282 protected function execute(InputInterface $input, OutputInterface $output)
284 $migration_ids = $input->getArgument('migration-ids');
285 $exclude_ids = $input->getOption('exclude');
287 $sourceBasepath = $input->getOption('source-base_path');
288 $configuration['source']['constants']['source_base_path'] = rtrim($sourceBasepath, '/') . '/';
291 // If migrations weren't provided finish execution
292 if (empty($migration_ids)) {
296 if (!$this->migrateConnection) {
297 $this->registerMigrateDB();
298 $this->migrateConnection = $this->getDBConnection('default', 'upgrade');
301 if (!$drupal_version = $this->getLegacyDrupalVersion($this->migrateConnection)) {
302 $this->getIo()->error($this->trans('commands.migrate.setup.migrations.questions.not-drupal'));
306 $version_tag = 'Drupal ' . $drupal_version;
308 if (!in_array('all', $migration_ids)) {
309 $migrations = $migration_ids;
311 $migrations = array_keys($this->getMigrations($version_tag));
314 if (!empty($exclude_ids)) {
315 // Remove exclude migration from migration script
316 $migrations = array_diff($migrations, $exclude_ids);
319 if (count($migrations) == 0) {
320 $this->getIo()->error($this->trans('commands.migrate.execute.messages.no-migrations'));
324 foreach ($migrations as $migration_id) {
325 $this->getIo()->info(
327 $this->trans('commands.migrate.execute.messages.processing'),
332 $migration_service = $this->pluginManagerMigration->createInstance($migration_id, $configuration);
334 if ($migration_service) {
335 $messages = new MigrateExecuteMessageCapture();
336 $executable = new MigrateExecutable($migration_service, $messages);
337 $migration_status = $executable->import();
338 switch ($migration_status) {
339 case MigrationInterface::RESULT_COMPLETED:
340 $this->getIo()->info(
342 $this->trans('commands.migrate.execute.messages.imported'),
347 case MigrationInterface::RESULT_INCOMPLETE:
348 $this->getIo()->info(
350 $this->trans('commands.migrate.execute.messages.importing-incomplete'),
355 case MigrationInterface::RESULT_STOPPED:
356 $this->getIo()->error(
358 $this->trans('commands.migrate.execute.messages.import-stopped'),
363 case MigrationInterface::RESULT_FAILED:
364 $this->getIo()->error(
366 $this->trans('commands.migrate.execute.messages.import-fail'),
371 case MigrationInterface::RESULT_SKIPPED:
372 $this->getIo()->error(
374 $this->trans('commands.migrate.execute.messages.import-skipped'),
379 case MigrationInterface::RESULT_DISABLED:
380 // Skip silently if disabled.
384 $this->getIo()->error($this->trans('commands.migrate.execute.messages.fail-load'));