From 453a4451b758e3ccce39c73f658875d9192ef084 Mon Sep 17 00:00:00 2001
From: Alex Pott <alex.a.pott@googlemail.com>
Date: Mon, 11 Aug 2014 08:59:58 -0500
Subject: [PATCH] Issue #2314289 by benjy: Track result of migrations and use
 it to properly define migrate_dependencies.

---
 .../migrate/config/schema/migrate.schema.yml  |  2 +-
 core/modules/migrate/src/Entity/Migration.php | 33 ++++++++++++++-----
 .../migrate/src/Entity/MigrationInterface.php | 24 ++++++++++++++
 .../modules/migrate/src/MigrateExecutable.php |  3 +-
 .../migrate/src/Tests/MigrateTestBase.php     |  5 +++
 .../install/migrate.migration.d6_block.yml    |  2 +-
 .../install/migrate.migration.d6_comment.yml  |  3 +-
 .../install/migrate.migration.d6_node.yml     |  3 +-
 ...migrate.migration.d6_user_picture_file.yml |  3 ++
 .../src/Tests/d6/MigrateBlockTest.php         |  5 ++-
 .../src/Tests/d6/MigrateNodeTestBase.php      | 16 +++++++++
 .../dependencies/MigrateDependenciesTest.php  | 13 +++++++-
 12 files changed, 94 insertions(+), 18 deletions(-)

diff --git a/core/modules/migrate/config/schema/migrate.schema.yml b/core/modules/migrate/config/schema/migrate.schema.yml
index b01bd4550c1c..fe4ea1a51f8b 100644
--- a/core/modules/migrate/config/schema/migrate.schema.yml
+++ b/core/modules/migrate/config/schema/migrate.schema.yml
@@ -1,7 +1,7 @@
 # Schema for the configuration files of the Migrate module.
 
 migrate.migration.*:
-  type: mapping
+  type: config_entity
   label: 'Migration'
   mapping:
     id:
diff --git a/core/modules/migrate/src/Entity/Migration.php b/core/modules/migrate/src/Entity/Migration.php
index da71c0be56e3..8c9388d36f67 100644
--- a/core/modules/migrate/src/Entity/Migration.php
+++ b/core/modules/migrate/src/Entity/Migration.php
@@ -326,15 +326,7 @@ public function checkRequirements() {
       $required_migrations = \Drupal::entityManager()->getStorage('migration')->loadMultiple($this->requirements);
       // Check if the dependencies are in good shape.
       foreach ($required_migrations as $required_migration) {
-        // If the dependent source migration has no IDs then no mappings can
-        // be recorded thus it is impossible to see whether the migration ran.
-        if (!$required_migration->getSourcePlugin()->getIds()) {
-          return FALSE;
-        }
-
-        // If the dependent migration has not processed any record, it means the
-        // dependency requirements are not met.
-        if (!$required_migration->getIdMap()->processedCount()) {
+        if (!$required_migration->isComplete()) {
           return FALSE;
         }
       }
@@ -346,4 +338,27 @@ public function checkRequirements() {
     return TRUE;
   }
 
+  /**
+   * {@inheritdoc}
+   */
+  public function setMigrationResult($result) {
+    $migrate_result_store = \Drupal::keyValue('migrate_result');
+    $migrate_result_store->set($this->id(), $result);
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function getMigrationResult() {
+    $migrate_result_store = \Drupal::keyValue('migrate_result');
+    return $migrate_result_store->get($this->id(), static::RESULT_INCOMPLETE);
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function isComplete() {
+    return $this->getMigrationResult() === static::RESULT_COMPLETED;
+  }
+
 }
diff --git a/core/modules/migrate/src/Entity/MigrationInterface.php b/core/modules/migrate/src/Entity/MigrationInterface.php
index 141bdf1eef95..415099389c62 100644
--- a/core/modules/migrate/src/Entity/MigrationInterface.php
+++ b/core/modules/migrate/src/Entity/MigrationInterface.php
@@ -102,4 +102,28 @@ public function getHighwater();
    */
   public function saveHighwater($highwater);
 
+  /**
+   * Check if this migration is complete.
+   *
+   * @return bool
+   *   TRUE if this migration is complete otherwise FALSE.
+   */
+  public function isComplete();
+
+  /**
+   * Set the migration result.
+   *
+   * @param int $result
+   *   One of the RESULT_* constants.
+   */
+  public function setMigrationResult($result);
+
+  /**
+   * Get the current migration result.
+   *
+   * @return int
+   *   The current migration result. Defaults to RESULT_INCOMPLETE.
+   */
+  public function getMigrationResult();
+
 }
diff --git a/core/modules/migrate/src/MigrateExecutable.php b/core/modules/migrate/src/MigrateExecutable.php
index 5b9d8bb86850..afcc745138e4 100644
--- a/core/modules/migrate/src/MigrateExecutable.php
+++ b/core/modules/migrate/src/MigrateExecutable.php
@@ -21,7 +21,7 @@ class MigrateExecutable {
   /**
    * The configuration of the migration to do.
    *
-   * @var \Drupal\migrate\Entity\MigrationInterface
+   * @var \Drupal\migrate\Entity\Migration
    */
   protected $migration;
 
@@ -333,6 +333,7 @@ public function import() {
      */
     #$this->progressMessage($return);
 
+    $this->migration->setMigrationResult($return);
     return $return;
   }
 
diff --git a/core/modules/migrate/src/Tests/MigrateTestBase.php b/core/modules/migrate/src/Tests/MigrateTestBase.php
index 39a23e2523bb..1ed738fe7ffe 100644
--- a/core/modules/migrate/src/Tests/MigrateTestBase.php
+++ b/core/modules/migrate/src/Tests/MigrateTestBase.php
@@ -116,6 +116,11 @@ protected function prepareIdMappings(array $id_mappings) {
     $migrations = entity_load_multiple('migration', array_keys($id_mappings));
     foreach ($id_mappings as $migration_id => $data) {
       $migration = $migrations[$migration_id];
+
+      // @TODO, rename prepareIdMappings() in https://drupal.org/node/2315489
+      // which will make the position of this more appropriate.
+      $migration->setMigrationResult(MigrationInterface::RESULT_COMPLETED);
+
       $id_map = $migration->getIdMap();
       $id_map->setMessage($this);
       $source_ids = $migration->getSourcePlugin()->getIds();
diff --git a/core/modules/migrate_drupal/config/install/migrate.migration.d6_block.yml b/core/modules/migrate_drupal/config/install/migrate.migration.d6_block.yml
index 4c15527b0a75..6d1f3985df75 100644
--- a/core/modules/migrate_drupal/config/install/migrate.migration.d6_block.yml
+++ b/core/modules/migrate_drupal/config/install/migrate.migration.d6_block.yml
@@ -84,6 +84,6 @@ process:
 destination:
   plugin: entity:block
 migration_dependencies:
-  optional:
+  required:
     - d6_menu
     - d6_custom_block
diff --git a/core/modules/migrate_drupal/config/install/migrate.migration.d6_comment.yml b/core/modules/migrate_drupal/config/install/migrate.migration.d6_comment.yml
index 5901c6736158..4cfea2f491fa 100644
--- a/core/modules/migrate_drupal/config/install/migrate.migration.d6_comment.yml
+++ b/core/modules/migrate_drupal/config/install/migrate.migration.d6_comment.yml
@@ -48,10 +48,9 @@ process:
 destination:
   plugin: entity:comment
 migration_dependencies:
-  optional:
-    - d6_comment_type
   required:
     - d6_node
+    - d6_comment_type
     - d6_user
     - d6_comment_entity_display
     - d6_comment_entity_form_display
diff --git a/core/modules/migrate_drupal/config/install/migrate.migration.d6_node.yml b/core/modules/migrate_drupal/config/install/migrate.migration.d6_node.yml
index fcf411aa14aa..cc210c81d994 100644
--- a/core/modules/migrate_drupal/config/install/migrate.migration.d6_node.yml
+++ b/core/modules/migrate_drupal/config/install/migrate.migration.d6_node.yml
@@ -33,8 +33,7 @@ destination:
 migration_dependencies:
   required:
     - d6_node_type
+    - d6_node_settings
     - d6_filter_format
-  optional:
     - d6_field_instance_widget_settings
     - d6_field_formatter_settings
-    - d6_node_settings
diff --git a/core/modules/migrate_drupal/config/install/migrate.migration.d6_user_picture_file.yml b/core/modules/migrate_drupal/config/install/migrate.migration.d6_user_picture_file.yml
index d142dbb79c44..8e3fd25a6ed6 100644
--- a/core/modules/migrate_drupal/config/install/migrate.migration.d6_user_picture_file.yml
+++ b/core/modules/migrate_drupal/config/install/migrate.migration.d6_user_picture_file.yml
@@ -22,3 +22,6 @@ migration_dependencies:
   # migration as an optional dependency to ensure it runs first.
   optional:
     - d6_file
+dependencies:
+  module:
+    - file
diff --git a/core/modules/migrate_drupal/src/Tests/d6/MigrateBlockTest.php b/core/modules/migrate_drupal/src/Tests/d6/MigrateBlockTest.php
index a22bacb27a29..70d926018553 100644
--- a/core/modules/migrate_drupal/src/Tests/d6/MigrateBlockTest.php
+++ b/core/modules/migrate_drupal/src/Tests/d6/MigrateBlockTest.php
@@ -52,7 +52,10 @@ public function setUp() {
         array(array(11), array(2)),
         array(array(12), array(1)),
         array(array(13), array(2)),
-      )
+      ),
+      'd6_menu' => array(
+        array(array('menu1'), array('menu')),
+      ),
     ));
 
     // Set Bartik and Seven as the default public and admin theme.
diff --git a/core/modules/migrate_drupal/src/Tests/d6/MigrateNodeTestBase.php b/core/modules/migrate_drupal/src/Tests/d6/MigrateNodeTestBase.php
index e51a2d113909..746023ec38c0 100644
--- a/core/modules/migrate_drupal/src/Tests/d6/MigrateNodeTestBase.php
+++ b/core/modules/migrate_drupal/src/Tests/d6/MigrateNodeTestBase.php
@@ -8,6 +8,7 @@
 namespace Drupal\migrate_drupal\Tests\d6;
 
 use Drupal\migrate_drupal\Tests\MigrateDrupalTestBase;
+use Drupal\migrate\Entity\MigrationInterface;
 
 /**
  * Base class for Node migration tests.
@@ -33,9 +34,24 @@ protected function setUp() {
         array(array(1), array('filtered_html')),
         array(array(2), array('full_html')),
       ),
+      'd6_field_instance_widget_settings' => array(
+        array(
+          array('page', 'field_test'),
+          array('node', 'page', 'default', 'test'),
+        ),
+      ),
+      'd6_field_formatter_settings' => array(
+        array(
+          array('page', 'default', 'node', 'field_test'),
+          array('node', 'page', 'default', 'field_test'),
+        ),
+      ),
     );
     $this->prepareIdMappings($id_mappings);
 
+    $migration = entity_load('migration', 'd6_node_settings');
+    $migration->setMigrationResult(MigrationInterface::RESULT_COMPLETED);
+
     // Create a test node.
     $node = entity_create('node', array(
       'type' => 'story',
diff --git a/core/modules/migrate_drupal/src/Tests/dependencies/MigrateDependenciesTest.php b/core/modules/migrate_drupal/src/Tests/dependencies/MigrateDependenciesTest.php
index e5168b0ea22c..8d69c64b44c2 100644
--- a/core/modules/migrate_drupal/src/Tests/dependencies/MigrateDependenciesTest.php
+++ b/core/modules/migrate_drupal/src/Tests/dependencies/MigrateDependenciesTest.php
@@ -30,7 +30,18 @@ public function testMigrateDependenciesOrder() {
     $migrations = entity_load_multiple('migration', $migration_items);
     $expected_order = array('d6_filter_format', 'd6_node', 'd6_comment');
     $this->assertEqual(array_keys($migrations), $expected_order);
-    $expected_requirements = array('d6_node', 'd6_node_type', 'd6_filter_format', 'd6_user', 'd6_comment_entity_display', 'd6_comment_entity_form_display');
+    $expected_requirements = array(
+      'd6_node',
+      'd6_node_type',
+      'd6_node_settings',
+      'd6_field_instance_widget_settings',
+      'd6_field_formatter_settings',
+      'd6_filter_format',
+      'd6_user',
+      'd6_comment_type',
+      'd6_comment_entity_display',
+      'd6_comment_entity_form_display',
+    );
     // Migration dependencies for comment include dependencies for node
     // migration as well.
     $actual_requirements = $migrations['d6_comment']->get('requirements');
-- 
GitLab