Issue #2999478 by heddn, mikelutz: Refactor MigrateExecutable::rollback()

8.7.x
Alex Pott 2018-09-19 10:43:26 +01:00
parent be64fa993e
commit 2973442335
No known key found for this signature in database
GPG Key ID: 31905460D4A69276
1 changed files with 20 additions and 8 deletions

View File

@ -105,7 +105,7 @@ class MigrateExecutable implements MigrateExecutableInterface {
public function __construct(MigrationInterface $migration, MigrateMessageInterface $message = NULL, EventDispatcherInterface $event_dispatcher = NULL) { public function __construct(MigrationInterface $migration, MigrateMessageInterface $message = NULL, EventDispatcherInterface $event_dispatcher = NULL) {
$this->migration = $migration; $this->migration = $migration;
$this->message = $message ?: new MigrateMessage(); $this->message = $message ?: new MigrateMessage();
$this->migration->getIdMap()->setMessage($this->message); $this->getIdMap()->setMessage($this->message);
$this->eventDispatcher = $event_dispatcher; $this->eventDispatcher = $event_dispatcher;
// Record the memory limit in bytes // Record the memory limit in bytes
$limit = trim(ini_get('memory_limit')); $limit = trim(ini_get('memory_limit'));
@ -182,7 +182,7 @@ class MigrateExecutable implements MigrateExecutableInterface {
$this->migration->setStatus(MigrationInterface::STATUS_IMPORTING); $this->migration->setStatus(MigrationInterface::STATUS_IMPORTING);
$return = MigrationInterface::RESULT_COMPLETED; $return = MigrationInterface::RESULT_COMPLETED;
$source = $this->getSource(); $source = $this->getSource();
$id_map = $this->migration->getIdMap(); $id_map = $this->getIdMap();
try { try {
$source->rewind(); $source->rewind();
@ -204,7 +204,7 @@ class MigrateExecutable implements MigrateExecutableInterface {
$save = TRUE; $save = TRUE;
} }
catch (MigrateException $e) { catch (MigrateException $e) {
$this->migration->getIdMap()->saveIdMapping($row, [], $e->getStatus()); $this->getIdMap()->saveIdMapping($row, [], $e->getStatus());
$this->saveMessage($e->getMessage(), $e->getLevel()); $this->saveMessage($e->getMessage(), $e->getLevel());
$save = FALSE; $save = FALSE;
} }
@ -241,11 +241,11 @@ class MigrateExecutable implements MigrateExecutableInterface {
} }
} }
catch (MigrateException $e) { catch (MigrateException $e) {
$this->migration->getIdMap()->saveIdMapping($row, [], $e->getStatus()); $this->getIdMap()->saveIdMapping($row, [], $e->getStatus());
$this->saveMessage($e->getMessage(), $e->getLevel()); $this->saveMessage($e->getMessage(), $e->getLevel());
} }
catch (\Exception $e) { catch (\Exception $e) {
$this->migration->getIdMap()->saveIdMapping($row, [], MigrateIdMapInterface::STATUS_FAILED); $this->getIdMap()->saveIdMapping($row, [], MigrateIdMapInterface::STATUS_FAILED);
$this->handleException($e); $this->handleException($e);
} }
} }
@ -299,11 +299,12 @@ class MigrateExecutable implements MigrateExecutableInterface {
$return = MigrationInterface::RESULT_COMPLETED; $return = MigrationInterface::RESULT_COMPLETED;
$this->migration->setStatus(MigrationInterface::STATUS_ROLLING_BACK); $this->migration->setStatus(MigrationInterface::STATUS_ROLLING_BACK);
$id_map = $this->migration->getIdMap(); $id_map = $this->getIdMap();
$destination = $this->migration->getDestinationPlugin(); $destination = $this->migration->getDestinationPlugin();
// Loop through each row in the map, and try to roll it back. // Loop through each row in the map, and try to roll it back.
foreach ($id_map as $map_row) { $id_map->rewind();
while ($id_map->valid()) {
$destination_key = $id_map->currentDestination(); $destination_key = $id_map->currentDestination();
if ($destination_key) { if ($destination_key) {
$map_row = $id_map->getRowByDestination($destination_key); $map_row = $id_map->getRowByDestination($destination_key);
@ -323,6 +324,7 @@ class MigrateExecutable implements MigrateExecutableInterface {
$source_key = $id_map->currentSource(); $source_key = $id_map->currentSource();
$id_map->delete($source_key); $id_map->delete($source_key);
} }
$id_map->next();
// Check for memory exhaustion. // Check for memory exhaustion.
if (($return = $this->checkStatus()) != MigrationInterface::RESULT_COMPLETED) { if (($return = $this->checkStatus()) != MigrationInterface::RESULT_COMPLETED) {
@ -344,6 +346,16 @@ class MigrateExecutable implements MigrateExecutableInterface {
return $return; return $return;
} }
/**
* Get the ID map from the current migration.
*
* @return \Drupal\migrate\Plugin\MigrateIdMapInterface
* The ID map.
*/
protected function getIdMap() {
return $this->migration->getIdMap();
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
@ -416,7 +428,7 @@ class MigrateExecutable implements MigrateExecutableInterface {
* {@inheritdoc} * {@inheritdoc}
*/ */
public function saveMessage($message, $level = MigrationInterface::MESSAGE_ERROR) { public function saveMessage($message, $level = MigrationInterface::MESSAGE_ERROR) {
$this->migration->getIdMap()->saveMessage($this->sourceIdValues, $message, $level); $this->getIdMap()->saveMessage($this->sourceIdValues, $message, $level);
} }
/** /**