3 namespace Drupal\migrate_upgrade\Commands;
5 use Consolidation\AnnotatedCommand\AnnotationData;
6 use Consolidation\AnnotatedCommand\CommandData;
7 use Drupal\migrate_upgrade\MigrateUpgradeDrushRunner;
8 use Drush\Commands\DrushCommands;
9 use Drush\Exceptions\UserAbortException;
10 use Drupal\Core\State\StateInterface;
11 use Symfony\Component\Console\Command\Command;
12 use Symfony\Component\Console\Input\InputOption;
15 * Migrate Upgrade drush commands.
17 class MigrateUpgradeCommands extends DrushCommands {
22 * @var \Drupal\Core\State\StateInterface
27 * MigrateUpgradeCommands constructor.
29 * @param \Drupal\Core\State\StateInterface $state
32 public function __construct(StateInterface $state) {
33 $this->state = $state;
37 * Perform one or more upgrade processes.
39 * @command migrate:upgrade
41 * @usage migrate-upgrade --legacy-db-url='mysql://root:pass@127.0.0.1/d6'
42 * Upgrade a Drupal 6 database to Drupal 8
43 * @usage migrate-upgrade --legacy-db-key='drupal_7'
44 * Upgrade Drupal 7 database where the connection to Drupal 7 has already
45 * been created in settings.php ($databases['drupal_7'])
46 * @usage migrate-upgrade --legacy-db-url='mysql://root:pass@127.0.0.1/d7' --configure-only --migration-prefix=d7_custom_ --legacy-root=https://www.example.com
47 * Generate migrations for a custom migration from Drupal 7 to Drupal 8
49 * @validate-module-enabled migrate_upgrade
51 * @aliases migrate-upgrade, mup
54 * When an error occurs.
56 public function upgrade(array $options = []) {
57 $runner = new MigrateUpgradeDrushRunner($options);
60 if ($options['configure-only']) {
65 $this->state->set('migrate_drupal_ui.performed', \Drupal::time()->getRequestTime());
67 // Remove the global database state.
68 $this->state->delete('migrate.fallback_state_key');
72 * @hook validate migrate:upgrade
74 public function validatePassword(CommandData $commandData) {
75 $input = $commandData->input();
76 $db_url = $input->getOption('legacy-db-url');
77 $db_key = $input->getOption('legacy-db-key');
79 if (!$db_url && !$db_key) {
80 throw new \Exception('You must provide either a --legacy-db-url or --legacy-db-key.');
85 * @hook option migrate:upgrade
87 public function legacyDatabaseUrl(Command $command, AnnotationData $annotationData) {
91 InputOption::VALUE_OPTIONAL,
92 'A Drupal 6 style database URL. Required if you do not set legacy-db-key.'
97 * @hook option migrate:upgrade
99 public function legacyDatabaseKey(Command $command, AnnotationData $annotationData) {
103 InputOption::VALUE_OPTIONAL,
104 'A database connection key from settings.php. Use as an alternative to legacy-db-url.'
109 * @hook option migrate:upgrade
111 public function legacyDatabasePrefix(Command $command, AnnotationData $annotationData) {
115 InputOption::VALUE_OPTIONAL,
116 'Database prefix of the legacy Drupal installation.'
121 * @hook option migrate:upgrade
123 public function legacyRoot(Command $command, AnnotationData $annotationData) {
127 InputOption::VALUE_OPTIONAL,
128 'For files migrations. Site web address or file system root path (if files are local) of the legacy Drupal installation.'
133 * @hook option migrate:upgrade
135 public function configureOnly(Command $command, AnnotationData $annotationData) {
139 InputOption::VALUE_NONE,
140 'Set up the appropriate upgrade processes as migrate_plus config entities but do not perform them.'
145 * @hook option migrate:upgrade
147 public function migrationPrefix(Command $command, AnnotationData $annotationData) {
151 InputOption::VALUE_OPTIONAL,
152 'With configure-only, a prefix to apply to generated migration ids.',
158 * Rolls back and removes upgrade migrations.
160 * @throws UserAbortException
161 * If user chose to not perform the rollback.
163 * @command migrate:upgrade-rollback
164 * @usage migrate-upgrade-rollback
165 * Rolls back a previously-run upgrade. It will not rollback migrations
166 * exported as migrate_plus config entities.
167 * @validate-module-enabled migrate_upgrade
168 * @aliases migrate-upgrade-rollback, mupr
170 public function upgradeRollback() {
171 if ($date_performed = $this->state->get('migrate_drupal_ui.performed')) {
172 if ($this->io()->confirm(dt('All migrations will be rolled back. Are you sure?'))) {
173 $runner = new MigrateUpgradeDrushRunner();
175 $this->logger()->notice(dt('Rolling back the upgrades performed @date',
176 ['@date' => \Drupal::service('date.formatter')->format($date_performed)]));
178 $this->state->delete('migrate_drupal_ui.performed');
179 $this->logger()->notice(dt('Rolled back upgrades'));
182 throw new UserAbortException();
186 $this->logger()->warning(dt('No upgrade operation has been performed.'));