Unverified Commit 8616edd2 authored by Alex Pott's avatar Alex Pott
Browse files

Issue #2976098 by quietone, alexpott, Sivaji_Ganesh_Jojodae, joachim,...

Issue #2976098 by quietone, alexpott, Sivaji_Ganesh_Jojodae, joachim, benjifisher, Wim Leers, mikelutz, catch, dinarcon, scotwith1t, heddn: MigrateExecutable should add details for the migration & destination property to exceptions that cause a row failure
parent 57d91dbe
Loading
Loading
Loading
Loading
+4 −4
Original line number Diff line number Diff line
@@ -234,16 +234,16 @@ public function testFieldInstances() {
    }, iterator_to_array($migration->getIdMap()->getMessages()));
    $this->assertCount(8, $errors);
    sort($errors);
    $message = 'Can\'t migrate source field field_text_long_plain_filtered configured with both plain text and filtered text processing. See https://www.drupal.org/docs/8/upgrade/known-issues-when-upgrading-from-drupal-6-or-7-to-drupal-8#plain-text';
    $message = 'd7_field_instance:type: Can\'t migrate source field field_text_long_plain_filtered configured with both plain text and filtered text processing. See https://www.drupal.org/docs/8/upgrade/known-issues-when-upgrading-from-drupal-6-or-7-to-drupal-8#plain-text';
    $this->assertEquals($errors[0], $message);
    $this->assertEquals($errors[1], $message);
    $message = 'Can\'t migrate source field field_text_plain_filtered configured with both plain text and filtered text processing. See https://www.drupal.org/docs/8/upgrade/known-issues-when-upgrading-from-drupal-6-or-7-to-drupal-8#plain-text';
    $message = 'd7_field_instance:type: Can\'t migrate source field field_text_plain_filtered configured with both plain text and filtered text processing. See https://www.drupal.org/docs/8/upgrade/known-issues-when-upgrading-from-drupal-6-or-7-to-drupal-8#plain-text';
    $this->assertEquals($errors[2], $message);
    $this->assertEquals($errors[3], $message);
    $message = 'Can\'t migrate source field field_text_sum_plain of type text_with_summary configured with plain text processing. See https://www.drupal.org/docs/8/upgrade/known-issues-when-upgrading-from-drupal-6-or-7-to-drupal-8#plain-text';
    $message = 'd7_field_instance:type: Can\'t migrate source field field_text_sum_plain of type text_with_summary configured with plain text processing. See https://www.drupal.org/docs/8/upgrade/known-issues-when-upgrading-from-drupal-6-or-7-to-drupal-8#plain-text';
    $this->assertEquals($errors[4], $message);
    $this->assertEquals($errors[5], $message);
    $message = 'Can\'t migrate source field field_text_sum_plain_filtered of type text_with_summary configured with plain text processing. See https://www.drupal.org/docs/8/upgrade/known-issues-when-upgrading-from-drupal-6-or-7-to-drupal-8#plain-text';
    $message = 'd7_field_instance:type: Can\'t migrate source field field_text_sum_plain_filtered of type text_with_summary configured with plain text processing. See https://www.drupal.org/docs/8/upgrade/known-issues-when-upgrading-from-drupal-6-or-7-to-drupal-8#plain-text';
    $this->assertEquals($errors[6], $message);
    $this->assertEquals($errors[7], $message);
  }
+4 −4
Original line number Diff line number Diff line
@@ -182,10 +182,10 @@ public function testFields() {
    }, iterator_to_array($migration->getIdMap()->getMessages()));
    sort($errors);
    $this->assertCount(4, $errors);
    $this->assertEquals('Can\'t migrate source field field_text_long_plain_filtered configured with both plain text and filtered text processing. See https://www.drupal.org/docs/8/upgrade/known-issues-when-upgrading-from-drupal-6-or-7-to-drupal-8#plain-text', $errors[0]);
    $this->assertEquals('Can\'t migrate source field field_text_plain_filtered configured with both plain text and filtered text processing. See https://www.drupal.org/docs/8/upgrade/known-issues-when-upgrading-from-drupal-6-or-7-to-drupal-8#plain-text', $errors[1]);
    $this->assertEquals('Can\'t migrate source field field_text_sum_plain of type text_with_summary configured with plain text processing. See https://www.drupal.org/docs/8/upgrade/known-issues-when-upgrading-from-drupal-6-or-7-to-drupal-8#plain-text', $errors[2]);
    $this->assertEquals('Can\'t migrate source field field_text_sum_plain_filtered of type text_with_summary configured with plain text processing. See https://www.drupal.org/docs/8/upgrade/known-issues-when-upgrading-from-drupal-6-or-7-to-drupal-8#plain-text', $errors[3]);
    $this->assertEquals('d7_field:type: Can\'t migrate source field field_text_long_plain_filtered configured with both plain text and filtered text processing. See https://www.drupal.org/docs/8/upgrade/known-issues-when-upgrading-from-drupal-6-or-7-to-drupal-8#plain-text', $errors[0]);
    $this->assertEquals('d7_field:type: Can\'t migrate source field field_text_plain_filtered configured with both plain text and filtered text processing. See https://www.drupal.org/docs/8/upgrade/known-issues-when-upgrading-from-drupal-6-or-7-to-drupal-8#plain-text', $errors[1]);
    $this->assertEquals('d7_field:type: Can\'t migrate source field field_text_sum_plain of type text_with_summary configured with plain text processing. See https://www.drupal.org/docs/8/upgrade/known-issues-when-upgrading-from-drupal-6-or-7-to-drupal-8#plain-text', $errors[2]);
    $this->assertEquals('d7_field:type: Can\'t migrate source field field_text_sum_plain_filtered of type text_with_summary configured with plain text processing. See https://www.drupal.org/docs/8/upgrade/known-issues-when-upgrading-from-drupal-6-or-7-to-drupal-8#plain-text', $errors[3]);
  }

}
+147 −106
Original line number Diff line number Diff line
@@ -178,9 +178,7 @@ public function import() {
    }

    $this->migration->setStatus(MigrationInterface::STATUS_IMPORTING);
    $return = MigrationInterface::RESULT_COMPLETED;
    $source = $this->getSource();
    $id_map = $this->getIdMap();

    try {
      $source->rewind();
@@ -196,18 +194,38 @@ public function import() {
      return MigrationInterface::RESULT_FAILED;
    }

    // Get the process pipeline.
    $pipeline = FALSE;
    if ($source->valid()) {
      try {
        $pipeline = $this->migration->getProcessPlugins();
      }
      catch (MigrateException $e) {
        $row = $source->current();
        $this->sourceIdValues = $row->getSourceIdValues();
        $this->getIdMap()->saveIdMapping($row, [], $e->getStatus());
        $this->saveMessage($e->getMessage(), $e->getLevel());
      }
    }

    $return = MigrationInterface::RESULT_COMPLETED;
    if ($pipeline) {
      $id_map = $this->getIdMap();
      $destination = $this->migration->getDestinationPlugin();
      while ($source->valid()) {
        $row = $source->current();
        $this->sourceIdValues = $row->getSourceIdValues();

        try {
        $this->processRow($row);
          foreach ($pipeline as $destination_property_name => $plugins) {
            $this->processPipeline($row, $destination_property_name, $plugins, NULL);
          }
          $save = TRUE;
        }
        catch (MigrateException $e) {
          $this->getIdMap()->saveIdMapping($row, [], $e->getStatus());
        $this->saveMessage($e->getMessage(), $e->getLevel());
          $msg = sprintf("%s:%s: %s", $this->migration->getPluginId(), $destination_property_name, $e->getMessage());
          $this->saveMessage($msg, $e->getLevel());
          $save = FALSE;
        }
        catch (MigrateSkipRowException $e) {
@@ -215,18 +233,21 @@ public function import() {
            $id_map->saveIdMapping($row, [], MigrateIdMapInterface::STATUS_IGNORED);
          }
          if ($message = trim($e->getMessage())) {
          $this->saveMessage($message, MigrationInterface::MESSAGE_INFORMATIONAL);
            $msg = sprintf("%s:%s: %s", $this->migration->getPluginId(), $destination_property_name, $message);
            $this->saveMessage($msg, MigrationInterface::MESSAGE_INFORMATIONAL);
          }
          $save = FALSE;
        }

        if ($save) {
          try {
          $this->getEventDispatcher()->dispatch(new MigratePreRowSaveEvent($this->migration, $this->message, $row), MigrateEvents::PRE_ROW_SAVE);
            $this->getEventDispatcher()
              ->dispatch(new MigratePreRowSaveEvent($this->migration, $this->message, $row), MigrateEvents::PRE_ROW_SAVE);
            $destination_ids = $id_map->lookupDestinationIds($this->sourceIdValues);
            $destination_id_values = $destination_ids ? reset($destination_ids) : [];
            $destination_id_values = $destination->import($row, $destination_id_values);
          $this->getEventDispatcher()->dispatch(new MigratePostRowSaveEvent($this->migration, $this->message, $row, $destination_id_values), MigrateEvents::POST_ROW_SAVE);
            $this->getEventDispatcher()
              ->dispatch(new MigratePostRowSaveEvent($this->migration, $this->message, $row, $destination_id_values), MigrateEvents::POST_ROW_SAVE);
            if ($destination_id_values) {
              // We do not save an idMap entry for config.
              if ($destination_id_values !== TRUE) {
@@ -247,7 +268,8 @@ public function import() {
            $this->saveMessage($e->getMessage(), $e->getLevel());
          }
          catch (\Exception $e) {
          $this->getIdMap()->saveIdMapping($row, [], MigrateIdMapInterface::STATUS_FAILED);
            $this->getIdMap()
              ->saveIdMapping($row, [], MigrateIdMapInterface::STATUS_FAILED);
            $this->handleException($e);
          }
        }
@@ -280,6 +302,7 @@ public function import() {
          return MigrationInterface::RESULT_FAILED;
        }
      }
    }

    $this->getEventDispatcher()->dispatch(new MigrateImportEvent($this->migration, $this->message), MigrateEvents::POST_IMPORT);
    $this->migration->setStatus(MigrationInterface::STATUS_IDLE);
@@ -366,14 +389,35 @@ protected function getIdMap() {
   */
  public function processRow(Row $row, array $process = NULL, $value = NULL) {
    foreach ($this->migration->getProcessPlugins($process) as $destination => $plugins) {
      $this->processPipeline($row, $destination, $plugins, $value);
    }
  }

  /**
   * Runs a process pipeline.
   *
   * @param \Drupal\migrate\Row $row
   *   The $row to be processed.
   * @param string $destination
   *   The destination property name.
   * @param array $plugins
   *   The process pipeline plugins.
   * @param mixed $value
   *   (optional) Initial value of the pipeline for the destination.
   *
   * @see \Drupal\migrate\MigrateExecutableInterface::processRow
   *
   * @throws \Drupal\migrate\MigrateException
   */
  protected function processPipeline(Row $row, string $destination, array $plugins, $value) {
    $multiple = FALSE;
    /** @var \Drupal\migrate\Plugin\MigrateProcessInterface $plugin */
    foreach ($plugins as $plugin) {
      $definition = $plugin->getPluginDefinition();
      // Many plugins expect a scalar value but the current value of the
        // pipeline might be multiple scalars (this is set by the previous
        // plugin) and in this case the current value needs to be iterated
        // and each scalar separately transformed.
      // pipeline might be multiple scalars (this is set by the previous plugin)
      // and in this case the current value needs to be iterated and each scalar
      // separately transformed.
      if ($multiple && !$definition['handle_multiples']) {
        $new_value = [];
        if (!is_array($value)) {
@@ -414,9 +458,6 @@ public function processRow(Row $row, array $process = NULL, $value = NULL) {
        $row->setEmptyDestinationProperty($destination);
      }
    }
      // Reset the value.
      $value = NULL;
    }
  }

  /**
+2 −2
Original line number Diff line number Diff line
@@ -130,10 +130,10 @@ public function transform($value, MigrateExecutableInterface $migrate_executable
      $transformed = DateTimePlus::createFromFormat($fromFormat, $value, $from_timezone, $settings)->format($toFormat, ['timezone' => $to_timezone]);
    }
    catch (\InvalidArgumentException $e) {
      throw new MigrateException(sprintf("Format date plugin could not transform '%s' using the format '%s' for destination '%s'. Error: %s", $value, $fromFormat, $destination_property, $e->getMessage()), $e->getCode(), $e);
      throw new MigrateException(sprintf("Format date plugin could not transform '%s' using the format '%s'. Error: %s", $value, $fromFormat, $e->getMessage()), $e->getCode(), $e);
    }
    catch (\UnexpectedValueException $e) {
      throw new MigrateException(sprintf("Format date plugin could not transform '%s' using the format '%s' for destination '%s'. Error: %s", $value, $fromFormat, $destination_property, $e->getMessage()), $e->getCode(), $e);
      throw new MigrateException(sprintf("Format date plugin could not transform '%s' using the format '%s'. Error: %s", $value, $fromFormat, $e->getMessage()), $e->getCode(), $e);
    }

    return $transformed;
+3 −1
Original line number Diff line number Diff line
@@ -75,7 +75,9 @@ public function testExceptionThrow() {
    $messages = $id_map_plugin->getMessages(['url' => $invalid_url])->fetchAll();
    $this->assertCount(1, $messages);
    $message = reset($messages);
    $this->assertEquals("Client error: `GET $invalid_url` resulted in a `404 Not Found` response ($invalid_url)", $message->message);

    $id = $migration->getPluginId();
    $this->assertEquals("$id:uri: Client error: `GET $invalid_url` resulted in a `404 Not Found` response ($invalid_url)", $message->message);
    $this->assertEquals(MigrationInterface::MESSAGE_ERROR, $message->level);

    // Check that the second row was migrated successfully.
Loading