Created
March 30, 2017 19:12
-
-
Save vasi/23da0a1f48fffe5cdaadc97da6aa2612 to your computer and use it in GitHub Desktop.
Drupal migrate dry run
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
On line 9, for Drupal 9 at least, I had to update the following to let it run.
From:
To:
Thanks for your script!