rollback example

protected function setUp(): void
    {
        parent::setUp();
        $this->connection = $this->getContainer()->get(Connection::class);

        $this->writer = $this->getContainer()->get(EntityWriter::class);
        $this->customerDefinition = $this->getContainer()->get(CustomerDefinition::class);
        $this->repository = $this->getContainer()->get('customer.repository');

        $this->connection->rollBack();
        $this->rollback();
        $this->migrate();
        $this->connection->beginTransaction();
    }

    protected function tearDown(): void
    {
        $this->connection->rollBack();
        $this->connection->executeStatement(' ALTER TABLE `customer` DROP COLUMN doubleOptInRegistration, DROP COLUMN doubleOptInEmailSentDate, DROP COLUMN doubleOptInConfirmDate; ');
              $translation = $entity->getTranslation($langcode);
              if (!$translation->isDefaultTranslation()) {
                $entity->removeTranslation($langcode);
                $entity->save();
              }
            }
          }
        }
      }
    }
    else {
      parent::rollback($destination_identifier);
    }
  }

  /** * {@inheritdoc} */
  public function getHighestId() {
    $values = $this->storage->getQuery()
      ->accessCheck(FALSE)
      ->sort($this->getKey('id'), 'DESC')
      ->range(0, 1)
      

  protected function executeRollback($migration) {
    if (is_string($migration)) {
      $this->migration = $this->getMigration($migration);
    }
    else {
      $this->migration = $migration;
    }
    (new MigrateExecutable($this->migration, $this))->rollback();
  }

}
/** @var \Drupal\taxonomy\Entity\Term $term */
      $term = Term::load($row['id']);
      $this->assertNotEmpty($term);
      $map_row = $term_id_map->getRowBySource(['id' => $row['id']]);
      $this->assertNotNull($map_row['destid1']);
    }

    // Add a failed row to test if this can be rolled back without errors.     $this->mockFailure($term_migration['id' => '4', 'vocab' => '2', 'name' => 'FAIL']);

    // Rollback and verify the entities are gone.     $term_executable->rollback();
    foreach ($term_data_rows as $row) {
      $term = Term::load($row['id']);
      if (in_array($row['id']$preserved_term_ids)) {
        $this->assertNotNull($term);
      }
      else {
        $this->assertNull($term);
      }
      $map_row = $term_id_map->getRowBySource(['id' => $row['id']]);
      $this->assertFalse($map_row);
    }
    

  protected function executeRollback($migration) {
    if (is_string($migration)) {
      $this->migration = $this->getMigration($migration);
    }
    else {
      $this->migration = $migration;
    }
    (new MigrateExecutable($this->migration, $this))->rollback();
  }

}
$entity->getRevisionId(),
    ];
  }

  /** * {@inheritdoc} */
  public function rollback(array $destination_identifier) {
    // We want to delete the entity and all the translations so use     // Entity:rollback because EntityContentBase::rollback will not remove the     // default translation.     Entity::rollback($destination_identifier);
  }

}
$langcode = $row['language'];
      $id = 'taxonomy.vocabulary.' . $row['id'];
      /** @var \Drupal\language\Config\LanguageConfigOverride $config_translation */
      $config_translation = $language_manager->getLanguageConfigOverride($langcode$id);
      $this->assertSame($row['translation']$config_translation->get('name'));
      $map_row = $vocabulary_i18n_id_map->getRowBySource(['id' => $row['id'], 'language' => $row['language']]);
      $this->assertNotNull($map_row['destid1']);
    }

    // Perform the rollback and confirm the translation was deleted and the map     // table row removed.     $vocabulary_i18n_executable->rollback();
    foreach ($vocabulary_i18n_data_rows as $row) {
      $langcode = $row['language'];
      $id = 'taxonomy.vocabulary.' . $row['id'];
      /** @var \Drupal\language\Config\LanguageConfigOverride $config_translation */
      $config_translation = $language_manager->getLanguageConfigOverride($langcode$id);
      $this->assertNull($config_translation->get('name'));
      $map_row = $vocabulary_i18n_id_map->getRowBySource(['id' => $row['id'], 'language' => $row['language']]);
      $this->assertFalse($map_row);
    }

    // Confirm the original vocabulary still exists.
break;
            case 'sqlsrv':
                $sql = "CREATE TABLE $this->table ($this->idCol VARCHAR(128) NOT NULL PRIMARY KEY, $this->dataCol VARBINARY(MAX) NOT NULL, $this->expiryCol INTEGER NOT NULL, $this->timeCol INTEGER NOT NULL)";
                break;
            default:
                throw new DomainException(sprintf('Creating the session table is currently not implemented for PDO driver "%s".', $this->driver));
        }

        try {
            $this->pdo->exec($sql);
        } catch (PDOException $e) {
            $this->rollback();

            throw $e;
        }
    }

    /** * Returns true when the current session exists but expired according to session.gc_maxlifetime. * * Can be used to distinguish between a new session and one that expired due to inactivity. * * @return bool Whether current session expired */

      $entity_id = implode('.', $id_values);
      $language = $destination_identifier['langcode'];

      $config = $this->storage->load($entity_id)->getConfigDependencyName();
      $config_override = $this->languageManager->getLanguageConfigOverride($language$config);
      // Rollback the translation.       $config_override->delete();
    }
    else {
      $destination_identifier = implode('.', $destination_identifier);
      parent::rollback([$destination_identifier]);
    }
  }

}
use KernelTestBehaviour;

    private Connection $connection;

    protected function setUp(): void
    {
        $this->connection = $this->getContainer()->get(Connection::class);
    }

    public function testMigration(): void
    {
        $this->rollback();

        static::assertFalse($this->tableExists(OrderTransactionCaptureDefinition::ENTITY_NAME));
        static::assertFalse($this->tableExists(OrderTransactionCaptureRefundDefinition::ENTITY_NAME));
        static::assertFalse($this->tableExists(OrderTransactionCaptureRefundPositionDefinition::ENTITY_NAME));

        $this->execute();

        static::assertTrue($this->tableExists(OrderTransactionCaptureDefinition::ENTITY_NAME));
        static::assertTrue($this->tableExists(OrderTransactionCaptureRefundDefinition::ENTITY_NAME));
        static::assertTrue($this->tableExists(OrderTransactionCaptureRefundPositionDefinition::ENTITY_NAME));
    }

    

class Migration1689084023AdminElasticsearchIndexTaskTest extends TestCase
{
    private Connection $connection;

    protected function setUp(): void
    {
        parent::setUp();

        $this->connection = KernelLifecycleManager::getConnection();
        $this->rollback();
    }

    public function testMigration(): void
    {
        $migration = new Migration1689084023AdminElasticsearchIndexTask();
        $migration->update($this->connection);

        $schemaManager = $this->connection->createSchemaManager();
        $columns = $schemaManager->listTableColumns('admin_elasticsearch_index_task');

        static::assertNotEmpty($columns);
        
'sqlite' => "CREATE TABLE $this->table ($this->idCol TEXT NOT NULL PRIMARY KEY, $this->dataCol BLOB NOT NULL, $this->lifetimeCol INTEGER NOT NULL, $this->timeCol INTEGER NOT NULL)",
            'pgsql' => "CREATE TABLE $this->table ($this->idCol VARCHAR(128) NOT NULL PRIMARY KEY, $this->dataCol BYTEA NOT NULL, $this->lifetimeCol INTEGER NOT NULL, $this->timeCol INTEGER NOT NULL)",
            'oci' => "CREATE TABLE $this->table ($this->idCol VARCHAR2(128) NOT NULL PRIMARY KEY, $this->dataCol BLOB NOT NULL, $this->lifetimeCol INTEGER NOT NULL, $this->timeCol INTEGER NOT NULL)",
            'sqlsrv' => "CREATE TABLE $this->table ($this->idCol VARCHAR(128) NOT NULL PRIMARY KEY, $this->dataCol VARBINARY(MAX) NOT NULL, $this->lifetimeCol INTEGER NOT NULL, $this->timeCol INTEGER NOT NULL)",
            default => throw new \DomainException(sprintf('Creating the session table is currently not implemented for PDO driver "%s".', $this->driver)),
        };

        try {
            $this->pdo->exec($sql);
            $this->pdo->exec("CREATE INDEX {$this->lifetimeCol}_idx ON $this->table ($this->lifetimeCol)");
        } catch (\PDOException $e) {
            $this->rollback();

            throw $e;
        }
    }

    /** * Returns true when the current session exists but expired according to session.gc_maxlifetime. * * Can be used to distinguish between a new session and one that expired due to inactivity. */
    public function isSessionExpired(): bool
    {
return true;
        }

        return false;
    }

    /** * Rollback Transaction */
    protected function _transRollback(): bool
    {
        if ($this->connID->rollback()) {
            $this->connID->autocommit(true);

            return true;
        }

        return false;
    }
}
$rollback_actions[$idx] = $this->destination->rollbackAction();
    }

    $this->assertTranslations(1, 'en');
    $this->assertTranslations(2, 'fr', ['en']);
    $this->assertTranslations(3, 'en', ['fr']);
    $this->assertTranslations(4, 'fr');

    // Rollback the rows.     foreach ($destination_rows as $idx => $destination_row) {
      if ($rollback_actions[$idx] == MigrateIdMapInterface::ROLLBACK_DELETE) {
        $this->destination->rollback($destination_row);
      }
    }

    // No change, update of existing translation.     $this->assertTranslations(1, 'en');
    // Remove added translation.     $this->assertTranslations(2, 'fr');
    // No change, update of existing translation.     $this->assertTranslations(3, 'en', ['fr']);
    // No change, can't remove default translation.     $this->assertTranslations(4, 'fr');
  }
public function getIds() {
    $ids['targetEntityType']['type'] = 'string';
    $ids['mode']['type'] = 'string';
    return $ids;
  }

  /** * {@inheritdoc} */
  public function rollback(array $destination_identifier) {
    $destination_identifier = implode('.', $destination_identifier);
    parent::rollback([$destination_identifier]);
  }

}
Home | Imprint | This part of the site doesn't use cookies.