Verified Commit 5637daf3 authored by Alex Pott's avatar Alex Pott
Browse files

Issue #3262395 by benjifisher, alexpott, quietone, heddn:...

Issue #3262395 by benjifisher, alexpott, quietone, heddn: $migration_dependencies has inconsistent structure
parent bcbd7baa
Loading
Loading
Loading
Loading
+30 −2
Changes for core/modules/migrate/src/Plugin/Migration.php: 30 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -369,6 +369,10 @@ public function __construct(array $configuration, $plugin_id, $plugin_definition
      @trigger_error("The key 'trackLastImported' is deprecated in drupal:10.1.0 and is removed from drupal:11.0.0. There is no replacement. See https://www.drupal.org/node/3282894", E_USER_DEPRECATED);
    }

    $this->migration_dependencies = ($this->migration_dependencies ?: []) + ['required' => [], 'optional' => []];
    if (count($this->migration_dependencies) !== 2 || !is_array($this->migration_dependencies['required']) || !is_array($this->migration_dependencies['optional'])) {
      @trigger_error("Invalid migration dependencies for {$this->id()} is deprecated in drupal:10.1.0 and will cause an error in drupal:11.0.0. See https://www.drupal.org/node/3266691", E_USER_DEPRECATED);
    }
  }

  /**
@@ -632,6 +636,12 @@ public function set($property_name, $value) {
      // Invalidate the destination plugin.
      unset($this->destinationPlugin);
    }
    elseif ($property_name === 'migration_dependencies') {
      $value = ($value ?: []) + ['required' => [], 'optional' => []];
      if (count($value) !== 2 || !is_array($value['required']) || !is_array($value['optional'])) {
        @trigger_error("Invalid migration dependencies for {$this->id()} is deprecated in drupal:10.1.0 and will cause an error in drupal:11.0.0. See https://www.drupal.org/node/3266691", E_USER_DEPRECATED);
      }
    }
    $this->{$property_name} = $value;
    return $this;
  }
@@ -694,16 +704,34 @@ public function setTrackLastImported($track_last_imported) {
  }

  /**
   * {@inheritdoc}
   * Get the dependencies for this migration.
   *
   * @param bool $expand
   *   Will issue a deprecation in Drupal 10 if set to FALSE. See
   *   https://www.drupal.org/node/3266691.
   *
   * @return array
   *   The dependencies for this migrations.
   */
  public function getMigrationDependencies() {
  public function getMigrationDependencies(bool $expand = FALSE) {
    if (!$expand) {
      @trigger_error('Calling Migration::getMigrationDependencies() without expanding the plugin IDs is deprecated in drupal:10.1.0 and is removed from drupal:11.0.0. In most cases, use getMigrationDependencies(TRUE). See https://www.drupal.org/node/3266691', E_USER_DEPRECATED);
    }
    // @todo Before Drupal 11.0.0, remove ::set() and these checks.
    // @see https://www.drupal.org/project/drupal/issues/3262395
    $this->migration_dependencies = ($this->migration_dependencies ?: []) + ['required' => [], 'optional' => []];
    if (count($this->migration_dependencies) !== 2 || !is_array($this->migration_dependencies['required']) || !is_array($this->migration_dependencies['optional'])) {
      throw new InvalidPluginDefinitionException($this->id(), "Invalid migration dependencies configuration for migration {$this->id()}");
    }
    $this->migration_dependencies['optional'] = array_unique(array_merge($this->migration_dependencies['optional'], $this->findMigrationDependencies($this->process)));
    if (!$expand) {
      return $this->migration_dependencies;
    }
    return array_map(
      [$this->migrationPluginManager, 'expandPluginIds'],
      $this->migration_dependencies
    );
  }

  /**
   * Find migration dependencies from migration_lookup and sub_process plugins.
+15 −15
Changes for core/modules/migrate/src/Plugin/MigrationPluginManager.php: 15 added lines, 15 removed lines.
Original line number Diff line number Diff line
@@ -104,6 +104,13 @@ public function createInstances($migration_id, array $configuration = []) {

    $factory = $this->getFactory();
    $migration_ids = (array) $migration_id;

    // We need to expand any derivative migrations. Derivative migrations are
    // calculated by migration derivers such as D6NodeDeriver. This allows
    // migrations to depend on the base id and then have a dependency on all
    // derivative migrations. For example, d6_comment depends on d6_node but
    // after we've expanded the dependencies it will depend on d6_node:page,
    // d6_node:story and so on, for other derivative migrations.
    $plugin_ids = $this->expandPluginIds($migration_ids);

    $instances = [];
@@ -111,8 +118,10 @@ public function createInstances($migration_id, array $configuration = []) {
      $instances[$plugin_id] = $factory->createInstance($plugin_id, $configuration[$plugin_id] ?? []);
    }

    // @todo Remove loop when the ability to call ::getMigrationDependencies()
    //   without expanding plugins is removed.
    foreach ($instances as $migration) {
      $migration->set('migration_dependencies', array_map([$this, 'expandPluginIds'], $migration->getMigrationDependencies()));
      $migration->set('migration_dependencies', $migration->getMigrationDependencies(TRUE));
    }

    // Sort the migrations based on their dependencies.
@@ -130,22 +139,13 @@ public function createInstancesByTag($tag) {
  }

  /**
   * Expand derivative migration dependencies.
   *
   * We need to expand any derivative migrations. Derivative migrations are
   * calculated by migration derivers such as D6NodeDeriver. This allows
   * migrations to depend on the base id and then have a dependency on all
   * derivative migrations. For example, d6_comment depends on d6_node but after
   * we've expanded the dependencies it will depend on d6_node:page,
   * d6_node:story and so on, for other derivative migrations.
   *
   * @return array
   *   An array of expanded plugin ids.
   * {@inheritdoc}
   */
  protected function expandPluginIds(array $migration_ids) {
  public function expandPluginIds(array $migration_ids) {
    $plugin_ids = [];
    $all_ids = array_keys($this->getDefinitions());
    foreach ($migration_ids as $id) {
      $plugin_ids += preg_grep('/^' . preg_quote($id, '/') . PluginBase::DERIVATIVE_SEPARATOR . '/', array_keys($this->getDefinitions()));
      $plugin_ids += preg_grep('/^' . preg_quote($id, '/') . PluginBase::DERIVATIVE_SEPARATOR . '/', $all_ids);
      if ($this->hasDefinition($id)) {
        $plugin_ids[] = $id;
      }
@@ -169,7 +169,7 @@ public function buildDependencyMigration(array $migrations, array $dynamic_ids)
      $id = $migration->id();
      $requirements[$id] = [];
      $dependency_graph[$id]['edges'] = [];
      $migration_dependencies = $migration->getMigrationDependencies();
      $migration_dependencies = $migration->getMigrationDependencies(TRUE);

      if (isset($migration_dependencies['required'])) {
        foreach ($migration_dependencies['required'] as $dependency) {
+11 −0
Changes for core/modules/migrate/src/Plugin/MigrationPluginManagerInterface.php: 11 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -28,6 +28,17 @@ interface MigrationPluginManagerInterface extends PluginManagerInterface {
   */
  public function createInstances($id, array $configuration = []);

  /**
   * Expand derivative migration dependencies.
   *
   * @param string[] $migration_ids
   *   A list of plugin IDs.
   *
   * @return array
   *   An array of expanded plugin ids.
   */
  public function expandPluginIds(array $migration_ids);

  /**
   * Creates a stub migration plugin from a definition array.
   *
+7 −0
Changes for core/modules/migrate/tests/modules/migrate_expected_migrations_test/migrate_expected_migrations_test.info.yml: 7 added lines, 0 removed lines.
Original line number Diff line number Diff line
name: 'Migration Expected Migrations Test'
type: module
description: 'Provides test migrations to test getMigrationDependencies.'
package: Testing
version: VERSION
dependencies:
  - drupal:migrate
+7 −0
Changes for core/modules/migrate/tests/modules/migrate_expected_migrations_test/migrations/m1.yml: 7 added lines, 0 removed lines.
Original line number Diff line number Diff line
id: m1
label: "Sample Migration 1"
source:
  plugin: empty
process: {}
destination:
  plugin: 'null'
Loading