diff --git a/migrate_tools.drush.inc b/migrate_tools.drush.inc index 40f9285..34b99f1 100644 --- a/migrate_tools.drush.inc +++ b/migrate_tools.drush.inc @@ -75,6 +75,7 @@ function migrate_tools_drush_command() { 'tag' => 'ID of the migration tag to rollback', 'feedback' => 'Frequency of progress messages, in items processed', 'idlist' => 'Comma-separated list of IDs to import', + 'missing-from-source' => 'Rollback only items missing from the source', ], 'arguments' => [ 'migration' => 'Name of migration(s) to rollback. Delimit multiple using commas.', @@ -87,6 +88,7 @@ function migrate_tools_drush_command() { 'migrate-rollback --group=beer --tag=user' => 'Rollback all migrations in the beer group and with the user tag', 'migrate-rollback beer_term,beer_node' => 'Rollback imported terms and nodes', 'migrate-rollback beer_user --idlist=5' => 'Rollback imported user record with source ID 5', + 'migrate-rollback --missing-from-source beer_term' => 'Rollback only the imported terms that are no longer available from the source', ], 'drupal dependencies' => ['migrate_tools'], 'aliases' => ['mr'], @@ -323,6 +325,7 @@ function drush_migrate_tools_migrate_rollback($migration_names = '') { $group_names = drush_get_option('group'); $tag_names = drush_get_option('tag'); $all = drush_get_option('all'); + $missing_only = drush_get_option('missing-from-source'); $options = []; if (!$all && !$group_names && !$migration_names && !$tag_names) { drush_set_error('MIGRATE_ERROR', dt('You must specify --all, --group, --tag, or one or more migration names separated by commas')); @@ -343,13 +346,14 @@ function drush_migrate_tools_migrate_rollback($migration_names = '') { } // Take it one group at a time, rolling back the migrations within each group. + $operation = $missing_only ? 'rollbackMissingItems' : 'rollback'; foreach ($migrations as $group_id => $migration_list) { // Roll back in reverse order. $migration_list = array_reverse($migration_list); foreach ($migration_list as $migration_id => $migration) { $executable = new MigrateExecutable($migration, $log, $options); // drush_op() provides --simulate support. - drush_op([$executable, 'rollback']); + drush_op([$executable, $operation]); } } } diff --git a/src/Commands/MigrateToolsCommands.php b/src/Commands/MigrateToolsCommands.php index 6f91e14..a9bf80f 100644 --- a/src/Commands/MigrateToolsCommands.php +++ b/src/Commands/MigrateToolsCommands.php @@ -309,6 +309,7 @@ class MigrateToolsCommands extends DrushCommands { * @option feedback Frequency of progress messages, in items processed * @option idlist Comma-separated list of IDs to rollback * @option idlist-delimiter The delimiter for records, defaults to ':' + * @option missing-from-source Rollback only items missing from the source * * @usage migrate:rollback --all * Perform all migrations @@ -322,6 +323,9 @@ class MigrateToolsCommands extends DrushCommands { * Rollback imported terms and nodes * @usage migrate:rollback beer_user --idlist=5 * Rollback imported user record with source ID 5 + * @usage migrate-rollback --missing-from-source beer_term + * Rollback only the imported terms that are no longer available from the + * source * @validate-module-enabled migrate_tools * * @aliases mr, migrate-rollback @@ -329,7 +333,7 @@ class MigrateToolsCommands extends DrushCommands { * @throws \Exception * If there are not enough parameters to the command. */ - public function rollback($migration_names = '', array $options = ['all' => NULL, 'group' => NULL, 'tag' => NULL, 'feedback' => NULL, 'idlist' => NULL, 'idlist-delimiter' => ':']) { + public function rollback($migration_names = '', array $options = ['all' => NULL, 'group' => NULL, 'tag' => NULL, 'feedback' => NULL, 'idlist' => NULL, 'idlist-delimiter' => ':', 'missing-from-source' => NULL]) { $group_names = $options['group']; $tag_names = $options['tag']; $all = $options['all']; @@ -349,6 +353,8 @@ class MigrateToolsCommands extends DrushCommands { $this->logger()->error(dt('No migrations found.')); } + $operation = $options['missing-from-source'] ? 'rollbackMissingItems' : 'rollback'; + // Take it one group at a time, // rolling back the migrations within each group. foreach ($migrations as $group_id => $migration_list) { @@ -361,7 +367,7 @@ class MigrateToolsCommands extends DrushCommands { $additional_options ); // drush_op() provides --simulate support. - drush_op([$executable, 'rollback']); + drush_op([$executable, $operation]); } } }