Verified Commit 5b4ad871 authored by Andrei Mateescu's avatar Andrei Mateescu
Browse files

task: #3583849 Deprecate PgSql Connection::*Savepoint() methods

By: mondrake
By: andypost
By: amateescu
parent 8d78571b
Loading
Loading
Loading
Loading
Loading
+34 −7
Original line number Diff line number Diff line
@@ -86,6 +86,12 @@ class Connection extends DatabaseConnection implements SupportsTemporaryTablesIn
   * @see ::addSavepoint()
   * @see ::releaseSavepoint()
   * @see ::rollbackSavepoint()
   *
   * @deprecated in drupal:11.4.0 and is removed from drupal:13.0.0. Use
   *   TransactionManager to start a transaction then call ::commitOrRelease()
   *   or ::rollback() on it.
   *
   * @see https://www.drupal.org/node/3524461
   */
  protected array $savepoints = [];

@@ -215,26 +221,26 @@ public function query($query, array $args = [], $options = []) {
    // - Currently in a transaction.
    // - A 'mimic_implicit_commit' does not exist already.
    // - The query is not a savepoint query.
    $wrap_with_savepoint = $this->inTransaction() &&
    if (
      $this->inTransaction() &&
      !$this->transactionManager()->has('mimic_implicit_commit') &&
      !(is_string($query) && (
        stripos($query, 'ROLLBACK TO SAVEPOINT ') === 0 ||
        stripos($query, 'RELEASE SAVEPOINT ') === 0 ||
        stripos($query, 'SAVEPOINT ') === 0
      )
    );
    if ($wrap_with_savepoint) {
      ))
    ) {
      // Create a savepoint so we can rollback a failed query. This is so we can
      // mimic MySQL and SQLite transactions which don't fail if a single query
      // fails. This is important for tables that are created on demand. For
      // example, \Drupal\Core\Cache\DatabaseBackend.
      $this->addSavepoint();
      $savepoint = $this->startTransaction('mimic_implicit_commit');
      try {
        $return = parent::query($query, $args, $options);
        $this->releaseSavepoint();
        $savepoint->commitOrRelease();
      }
      catch (\Exception $e) {
        $this->rollbackSavepoint();
        $savepoint->rollback();
        throw $e;
      }
    }
@@ -379,8 +385,15 @@ public function getFullQualifiedTableName($table) {
   * @param string $savepoint_name
   *   A string representing the savepoint name. By default,
   *   "mimic_implicit_commit" is used.
   *
   * @deprecated in drupal:11.4.0 and is removed from drupal:13.0.0. Use
   *   TransactionManager to start a transaction then call ::commitOrRelease()
   *   or ::rollback() on it.
   *
   * @see https://www.drupal.org/node/3524461
   */
  public function addSavepoint($savepoint_name = 'mimic_implicit_commit') {
    @trigger_error(__METHOD__ . '() is deprecated in drupal:11.4.0 and is removed from drupal:13.0.0. Use TransactionManager to start a transaction then call ::commitOrRelease() or ::rollback() on it. See https://www.drupal.org/node/3524461', E_USER_DEPRECATED);
    if ($this->inTransaction()) {
      $this->savepoints[$savepoint_name] = $this->startTransaction($savepoint_name);
    }
@@ -392,8 +405,15 @@ public function addSavepoint($savepoint_name = 'mimic_implicit_commit') {
   * @param string $savepoint_name
   *   A string representing the savepoint name. By default,
   *   "mimic_implicit_commit" is used.
   *
   * @deprecated in drupal:11.4.0 and is removed from drupal:13.0.0. Use
   *   TransactionManager to start a transaction then call ::commitOrRelease()
   *   or ::rollback() on it.
   *
   * @see https://www.drupal.org/node/3524461
   */
  public function releaseSavepoint($savepoint_name = 'mimic_implicit_commit') {
    @trigger_error(__METHOD__ . '() is deprecated in drupal:11.4.0 and is removed from drupal:13.0.0. Use TransactionManager to start a transaction then call ::commitOrRelease() or ::rollback() on it. See https://www.drupal.org/node/3524461', E_USER_DEPRECATED);
    if ($this->inTransaction() && $this->transactionManager()->has($savepoint_name)) {
      unset($this->savepoints[$savepoint_name]);
    }
@@ -405,8 +425,15 @@ public function releaseSavepoint($savepoint_name = 'mimic_implicit_commit') {
   * @param string $savepoint_name
   *   A string representing the savepoint name. By default,
   *   "mimic_implicit_commit" is used.
   *
   * @deprecated in drupal:11.4.0 and is removed from drupal:13.0.0. Use
   *   TransactionManager to start a transaction then call ::commitOrRelease()
   *   or ::rollback() on it.
   *
   * @see https://www.drupal.org/node/3524461
   */
  public function rollbackSavepoint($savepoint_name = 'mimic_implicit_commit') {
    @trigger_error(__METHOD__ . '() is deprecated in drupal:11.4.0 and is removed from drupal:13.0.0. Use TransactionManager to start a transaction then call ::commitOrRelease() or ::rollback() on it. See https://www.drupal.org/node/3524461', E_USER_DEPRECATED);
    if ($this->inTransaction() && $this->transactionManager()->has($savepoint_name)) {
      $this->savepoints[$savepoint_name]->rollBack();
      unset($this->savepoints[$savepoint_name]);
+10 −5
Original line number Diff line number Diff line
@@ -13,17 +13,22 @@ class Delete extends QueryDelete {
   * {@inheritdoc}
   */
  public function execute() {
    $this->connection->addSavepoint();
    if ($this->connection->inTransaction()) {
      $savepoint = $this->connection->startTransaction('mimic_implicit_commit');
    }
    try {
      $result = parent::execute();
      if (isset($savepoint)) {
        $savepoint->commitOrRelease();
      }
      return $result;
    }
    catch (\Exception $e) {
      $this->connection->rollbackSavepoint();
      if (isset($savepoint)) {
        $savepoint->rollback();
      }
      throw $e;
    }
    $this->connection->releaseSavepoint();

    return $result;
  }

}
+9 −3
Original line number Diff line number Diff line
@@ -86,7 +86,9 @@ public function execute() {
    // mimic MySQL and SQLite transactions which don't fail if a single query
    // fails. This is important for tables that are created on demand. For
    // example, \Drupal\Core\Cache\DatabaseBackend.
    $this->connection->addSavepoint();
    if ($this->connection->inTransaction()) {
      $savepoint = $this->connection->startTransaction('mimic_implicit_commit');
    }
    try {
      $stmt->execute(NULL, $this->queryOptions);
      if (isset($table_information->serial_fields[0])) {
@@ -104,10 +106,14 @@ public function execute() {
        }
      }

      $this->connection->releaseSavepoint();
      if (isset($savepoint)) {
        $savepoint->commitOrRelease();
      }
    }
    catch (\Exception $e) {
      $this->connection->rollbackSavepoint();
      if (isset($savepoint)) {
        $savepoint->rollback();
      }
      $this->connection->exceptionHandler()->handleExecutionException($e, $stmt, [], $this->queryOptions);
    }

+19 −6
Original line number Diff line number Diff line
@@ -147,7 +147,10 @@ public function queryTableInformation($table) {
        'blob_fields' => [],
        'sequences' => [],
      ];
      $this->connection->addSavepoint();

      if ($this->connection->inTransaction()) {
        $savepoint = $this->connection->startTransaction('mimic_implicit_commit');
      }

      try {
        // The bytea columns and sequences for a table can be found in
@@ -171,10 +174,14 @@ public function queryTableInformation($table) {
        ]);
      }
      catch (\Exception $e) {
        $this->connection->rollbackSavepoint();
        if (isset($savepoint)) {
          $savepoint->rollback();
        }
        throw $e;
      }
      $this->connection->releaseSavepoint();
      if (isset($savepoint)) {
        $savepoint->commitOrRelease();
      }

      // If the table information does not yet exist in the PostgreSQL
      // metadata, then return the default table information here, so that it
@@ -260,7 +267,9 @@ public function queryFieldInformation($table, $field, $constraint_type = 'c') {
    $schema = $prefixInfo['schema'];
    $table_name = $prefixInfo['table'];

    $this->connection->addSavepoint();
    if ($this->connection->inTransaction()) {
      $savepoint = $this->connection->startTransaction('mimic_implicit_commit');
    }

    try {
      $checks = $this->connection->query("SELECT conname FROM pg_class cl INNER JOIN pg_constraint co ON co.conrelid = cl.oid INNER JOIN pg_attribute attr ON attr.attrelid = cl.oid AND attr.attnum = ANY (co.conkey) INNER JOIN pg_namespace ns ON cl.relnamespace = ns.oid WHERE co.contype = :constraint_type AND ns.nspname = :schema AND cl.relname = :table AND attr.attname = :column", [
@@ -271,11 +280,15 @@ public function queryFieldInformation($table, $field, $constraint_type = 'c') {
      ]);
    }
    catch (\Exception $e) {
      $this->connection->rollbackSavepoint();
      if (isset($savepoint)) {
        $savepoint->rollback();
      }
      throw $e;
    }

    $this->connection->releaseSavepoint();
    if (isset($savepoint)) {
      $savepoint->commitOrRelease();
    }

    $field_information = $checks->fetchCol();

+9 −3
Original line number Diff line number Diff line
@@ -143,15 +143,21 @@ public function addExpression($expression, $alias = NULL, $arguments = []) {
   * {@inheritdoc}
   */
  public function execute() {
    $this->connection->addSavepoint();
    if ($this->connection->inTransaction()) {
      $savepoint = $this->connection->startTransaction('mimic_implicit_commit');
    }
    try {
      $result = parent::execute();
    }
    catch (\Exception $e) {
      $this->connection->rollbackSavepoint();
      if (isset($savepoint)) {
        $savepoint->rollback();
      }
      throw $e;
    }
    $this->connection->releaseSavepoint();
    if (isset($savepoint)) {
      $savepoint->commitOrRelease();
    }

    return $result;
  }
Loading