You are here

function TransactionTest::testTransactionWithDdlStatement in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/system/src/Tests/Database/TransactionTest.php \Drupal\system\Tests\Database\TransactionTest::testTransactionWithDdlStatement()

Tests the compatibility of transactions with DDL statements.

File

core/modules/system/src/Tests/Database/TransactionTest.php, line 226
Contains \Drupal\system\Tests\Database\TransactionTest.

Class

TransactionTest
Tests the transaction abstraction system.

Namespace

Drupal\system\Tests\Database

Code

function testTransactionWithDdlStatement() {

  // First, test that a commit works normally, even with DDL statements.
  $transaction = db_transaction();
  $this
    ->insertRow('row');
  $this
    ->executeDDLStatement();
  unset($transaction);
  $this
    ->assertRowPresent('row');

  // Even in different order.
  $this
    ->cleanUp();
  $transaction = db_transaction();
  $this
    ->executeDDLStatement();
  $this
    ->insertRow('row');
  unset($transaction);
  $this
    ->assertRowPresent('row');

  // Even with stacking.
  $this
    ->cleanUp();
  $transaction = db_transaction();
  $transaction2 = db_transaction();
  $this
    ->executeDDLStatement();
  unset($transaction2);
  $transaction3 = db_transaction();
  $this
    ->insertRow('row');
  unset($transaction3);
  unset($transaction);
  $this
    ->assertRowPresent('row');

  // A transaction after a DDL statement should still work the same.
  $this
    ->cleanUp();
  $transaction = db_transaction();
  $transaction2 = db_transaction();
  $this
    ->executeDDLStatement();
  unset($transaction2);
  $transaction3 = db_transaction();
  $this
    ->insertRow('row');
  $transaction3
    ->rollback();
  unset($transaction3);
  unset($transaction);
  $this
    ->assertRowAbsent('row');

  // The behavior of a rollback depends on the type of database server.
  if (Database::getConnection()
    ->supportsTransactionalDDL()) {

    // For database servers that support transactional DDL, a rollback
    // of a transaction including DDL statements should be possible.
    $this
      ->cleanUp();
    $transaction = db_transaction();
    $this
      ->insertRow('row');
    $this
      ->executeDDLStatement();
    $transaction
      ->rollback();
    unset($transaction);
    $this
      ->assertRowAbsent('row');

    // Including with stacking.
    $this
      ->cleanUp();
    $transaction = db_transaction();
    $transaction2 = db_transaction();
    $this
      ->executeDDLStatement();
    unset($transaction2);
    $transaction3 = db_transaction();
    $this
      ->insertRow('row');
    unset($transaction3);
    $transaction
      ->rollback();
    unset($transaction);
    $this
      ->assertRowAbsent('row');
  }
  else {

    // For database servers that do not support transactional DDL,
    // the DDL statement should commit the transaction stack.
    $this
      ->cleanUp();
    $transaction = db_transaction();
    $this
      ->insertRow('row');
    $this
      ->executeDDLStatement();

    // Rollback the outer transaction.
    try {
      $transaction
        ->rollback();
      unset($transaction);

      // @TODO: an exception should be triggered here, but is not, because
      // "ROLLBACK" fails silently in MySQL if there is no transaction active.
      // $this->fail(t('Rolling back a transaction containing DDL should fail.'));
    } catch (TransactionNoActiveException $e) {
      $this
        ->pass('Rolling back a transaction containing DDL should fail.');
    }
    $this
      ->assertRowPresent('row');
  }
}