Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@vasi
Created March 30, 2017 19:12
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save vasi/23da0a1f48fffe5cdaadc97da6aa2612 to your computer and use it in GitHub Desktop.
Save vasi/23da0a1f48fffe5cdaadc97da6aa2612 to your computer and use it in GitHub Desktop.
Drupal migrate dry run
<?php
use Drupal\migrate_plus\Event\MigrateEvents as MigratePlusEvents;
use Drupal\migrate_plus\Event\MigratePrepareRowEvent;
use Drupal\migrate\Plugin\MigrateIdMapInterface;
function dry_run_migration($migration_id) {
// Get the migration source.
$migrationManager = \Drupal::service('plugin.manager.config_entity_migration');
$migration = $migrationManager->createInstance($migration_id);
$source = $migration->getSourcePlugin();
// Usually, the source will automatically skip rows that haven't changed,
// since we don't have to do anything with them.
// But to detect deletions, we need to see every single row--so we mark each
// row as 'changed', to make sure they're not skipped.
$dispatcher = \Drupal::service('event_dispatcher');
$dispatcher->addListener(MigratePlusEvents::PREPARE_ROW,
function(MigratePrepareRowEvent $event) {
$row = $event->getRow();
if ($row->getIdMap()) {
$row->setIdMap([
'source_row_status' => MigrateIdMapInterface::STATUS_NEEDS_UPDATE
] + $row->getIdMap());
}
}
);
// Look at each row to see if it's new or updated.
$seen = [];
foreach ($source as $row) {
$ids = implode(', ', array_values($row->getSourceIdValues()));
if (empty($row->getIdMap()['original_hash'])) {
printf("NEW: $ids\n");
}
elseif ($row->changed()) {
printf("UPDATED: $ids\n");
}
// Mark this row as seen.
$seen[$ids] = TRUE;
}
// Anything that we previously migrated, but wasn't in our source,
// must have been deleted.
$id_map = $migration->getIdMap();
foreach ($migration->getIdMap() as $row) {
if (empty($id_map->currentDestination())) {
// When previously processing this row, nothing was imported.
continue;
}
$ids = implode(', ', array_values($id_map->currentSource()));
if (empty($seen[$ids])) {
printf("DELETED: $ids\n");
}
}
}
dry_run_migration('my_test_migration');
@jpmc
Copy link

jpmc commented Apr 18, 2022

On line 9, for Drupal 9 at least, I had to update the following to let it run.

From:

$migrationManager = \Drupal::service('plugin.manager.config_entity_migration');

To:

$migrationManager = \Drupal::service('plugin.manager.migration');

Thanks for your script!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment