diff --git a/core/modules/migrate/src/Plugin/migrate/id_map/Sql.php b/core/modules/migrate/src/Plugin/migrate/id_map/Sql.php index 2b6e30c..2801c6d 100644 --- a/core/modules/migrate/src/Plugin/migrate/id_map/Sql.php +++ b/core/modules/migrate/src/Plugin/migrate/id_map/Sql.php @@ -12,6 +12,8 @@ use Drupal\migrate\MigrateException; use Drupal\migrate\MigrateMessageInterface; use Drupal\migrate\Plugin\MigrateIdMapInterface; +use Drupal\migrate\Plugin\MigrationPluginManager; +use Drupal\migrate\Plugin\MigrationPluginManagerInterface; use Drupal\migrate\Row; use Drupal\migrate\Event\MigrateEvents; use Drupal\migrate\Event\MigrateMapSaveEvent; @@ -42,6 +44,13 @@ class Sql extends PluginBase implements MigrateIdMapInterface, ContainerFactoryP protected $eventDispatcher; /** + * The migration plugin manager to have access to the list of migrations. + * + * @var \Drupal\migrate\Plugin\MigrationPluginManagerInterface + */ + protected $migrationPluginManager; + + /** * The migration map table name. * * @var string @@ -153,10 +162,11 @@ class Sql extends PluginBase implements MigrateIdMapInterface, ContainerFactoryP * @param \Drupal\migrate\Plugin\MigrationInterface $migration * The migration to do. */ - public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, EventDispatcherInterface $event_dispatcher) { + public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, EventDispatcherInterface $event_dispatcher, MigrationPluginManagerInterface $migration_plugin_manager) { parent::__construct($configuration, $plugin_id, $plugin_definition); $this->migration = $migration; $this->eventDispatcher = $event_dispatcher; + $this->migrationPluginManager = $migration_plugin_manager; } /** @@ -168,7 +178,8 @@ public static function create(ContainerInterface $container, array $configuratio $plugin_id, $plugin_definition, $migration, - $container->get('event_dispatcher') + $container->get('event_dispatcher'), + $container->get('plugin.manager.migration') ); } @@ -928,18 +939,47 @@ public function valid() { * {@inheritdoc} */ public function getMaxId($field) { - if (!$this->getDatabase()->schema()->tableExists($this->mapTableName())) { - return 0; + $sql_field = $this->destinationIdFields()[$field]; + $migration_id = $this->migration->id(); + + // List of mapping tables to look in for the highest ID. + $map_tables = [ + $migration_id => $this->mapTableName(), + ]; + + list($base_id, $bundle) = explode(':', $migration_id); + + // If there's a bundle, it means we have a derived migration and we need to + // find all the mapping tables from the related derived migrations. + if ($bundle) { + $migrations = $this->migrationPluginManager->getDefinitions(); + foreach ($migrations as $migration_id => $migration) { + if ($migration['id'] == $base_id) { + // Get this derived migration's mapping table and add it to the list + // of mapping tables to look in for the highest ID. + $migration['id'] = $migration_id; + $stub = $this->migrationPluginManager->createStubMigration($migration); + $map_tables[$migration_id] = $stub->getIdMap()->mapTableName(); + } + } } - $sqlField = $this->destinationIdFields()[$field]; + // Look in all the derived migrations' mapping tables for their highest ID. + $ids = []; + foreach ($map_tables as $map_table) { + if (!$this->getDatabase()->schema()->tableExists($map_table)) { + return 0; + } + + $query = $this->getDatabase()->select($map_table, 'map') + ->fields('map', [$sql_field]) + ->orderBy($sql_field, 'DESC') + ->range(0, 1); + $ids[] = $query->execute()->fetchField(); + } - $query = $this->getDatabase()->select($this->mapTableName(), 'map') - ->fields('map', [$sqlField]) - ->orderBy($sqlField, 'DESC') - ->range(0, 1); - $found = $query->execute()->fetchField(); - return (int) $found; + // Return the highest of the highest IDs. + return max($ids); } } diff --git a/core/modules/migrate_drupal_ui/tests/src/Functional/MigrateUpgradeTestBase.php b/core/modules/migrate_drupal_ui/tests/src/Functional/MigrateUpgradeTestBase.php index b88fbb1..82b83d2 100644 --- a/core/modules/migrate_drupal_ui/tests/src/Functional/MigrateUpgradeTestBase.php +++ b/core/modules/migrate_drupal_ui/tests/src/Functional/MigrateUpgradeTestBase.php @@ -175,7 +175,7 @@ protected function runPostMigrationTests() { foreach (array_keys(\Drupal::entityTypeManager()->getDefinitions()) as $entity_type) { $real_count = \Drupal::entityQuery($entity_type)->count()->execute(); $expected_count = isset($expected_counts[$entity_type]) ? $expected_counts[$entity_type] : 0; - $this->assertSame($expected_count, $real_count, "Found $real_count $entity_type entities, expected $expected_count."); + $this->assertEqual($expected_count, $real_count, "Found $real_count $entity_type entities, expected $expected_count."); } $plugin_manager = \Drupal::service('plugin.manager.migration');