Commit 7df216be authored by Jon Minder's avatar Jon Minder
Browse files

Issue #3253251 by ultimike, ayalon: Support for database-based migrations

parent 5ffda9b0
Loading
Loading
Loading
Loading

.gitignore

0 → 100644
+1 −0
Changes for .gitignore: 1 added line, 0 removed lines.
Original line number Diff line number Diff line
.idea/*
+29 −2
Changes for src/Commands/MigrateOrphansCommands.php: 29 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -79,6 +79,8 @@ class MigrateOrphansCommands extends MigrateToolsCommands {
   *   Do not delete, just report the records
   * @option group
   *   A comma-separated list of migration groups to list
   * @option output-ids
   *   Outputs a list of source and destination ids of the orphans found.
   * @usage drush migrate-orphans-purge migration
   *   Removes orphans from migration
   *
@@ -88,7 +90,7 @@ class MigrateOrphansCommands extends MigrateToolsCommands {
   * @throws \Exception
   *  If there are not enough parameters to the command.
   */
  public function orphansPurge($migration_name = '', array $options = ['dry-run' => NULL, 'delete' => TRUE, 'group' => NULL]) {
  public function orphansPurge($migration_name = '', array $options = ['dry-run' => NULL, 'delete' => TRUE, 'group' => NULL, 'output-ids' => NULL]) {
    $migrations = $this->migrationsList($migration_name, $options);

    if (empty($migrations)) {
@@ -132,6 +134,19 @@ class MigrateOrphansCommands extends MigrateToolsCommands {
            $dataparser->next();
          }
        }
        elseif (is_subclass_of(get_class($source), 'Drupal\migrate_drupal\Plugin\migrate\source\DrupalSqlBase')) {
          $rows = $source->query()->execute();
          $ids = $source->getIds();
          if (count($ids) === 1) {
            $id = array_key_first($ids);
          }
          else {
            $this->logger->error('Migrate class ' . get_class($source) . ' not compatible (multiple IDs).');
          }
          foreach ($rows as $row) {
            unset($orphans[$row[$id]]);
          }
        }
        else {
          $this->logger->error('Migrate class ' . get_class($source) . ' not compatible.', 'error');
          return;
@@ -140,7 +155,19 @@ class MigrateOrphansCommands extends MigrateToolsCommands {
        /** @var \Drupal\migrate\Plugin\migrate\destination\Entity $destination */
        $destination = $migration->getDestinationPlugin();

        if (!empty($options['output-ids'])) {
          $this->logger->notice('Migration: ' . $migration->getPluginId());
          if (count($orphans)) {
            $this->logger->notice('Total orphans: ' . count($orphans));
            $this->output()->writeln('sourceid1, destid1');
            foreach ($orphans as $key => $value) {
              $this->output()->writeln($key . ', ' . $value);
            }
          }
          else {
            $this->logger->notice('No orphans found');
          }
        }

        if (!empty($orphans)) {
          $deleted = $disabled = FALSE;
@@ -173,7 +200,7 @@ class MigrateOrphansCommands extends MigrateToolsCommands {

                // Unpublish orphans
                foreach ($entities as $node) {
                  $node->status = 0;
                  $node->set('status', 0);
                  $node->save();
                }
                $disabled = TRUE;