Commit e0b4b7ef authored by catch's avatar catch
Browse files

Issue #3240601 by alexpott, daffie, daniel.bosen: The standard logic we use in...

Issue #3240601 by alexpott, daffie, daniel.bosen: The standard logic we use in ::ensureTableExists() is wrong
parent 364ecc6c
Loading
Loading
Loading
Loading
+3 −4
Original line number Diff line number Diff line
@@ -165,20 +165,19 @@ protected function doCreate(array $batch) {
  protected function ensureTableExists() {
    try {
      $database_schema = $this->connection->schema();
      if (!$database_schema->tableExists(static::TABLE_NAME)) {
      $schema_definition = $this->schemaDefinition();
      $database_schema->createTable(static::TABLE_NAME, $schema_definition);
        return TRUE;
      }
    }
    // If another process has already created the batch table, attempting to
    // recreate it will throw an exception. In this case just catch the
    // exception and do nothing.
    catch (DatabaseException $e) {
      return TRUE;
    }
    catch (\Exception $e) {
      return FALSE;
    }
    return TRUE;
  }

  /**
   * Act on an exception when batch might be stale.
+8 −0
Original line number Diff line number Diff line
@@ -159,6 +159,10 @@ public function reset() {
   *
   * @return int[]
   *   List of invalidation counts keyed by the respective cache tag.
   *
   * @throws \Exception
   *   Thrown if the table could not be created or the database connection
   *   failed.
   */
  abstract protected function getTagInvalidationCounts(array $tags);

@@ -175,6 +179,10 @@ abstract protected function getDatabaseConnection();
   *
   * @param string[] $tags
   *   The set of tags for which to invalidate cache items.
   *
   * @throws \Exception
   *   Thrown if the table could not be created or the database connection
   *   failed.
   */
  abstract protected function doInvalidateTags(array $tags);

+11 −8
Original line number Diff line number Diff line
@@ -47,7 +47,7 @@ protected function doInvalidateTags(array $tags) {
      // core install where cache tags are invalidated before the table is
      // created.
      if (!$this->ensureTableExists()) {
        $this->catchException($e);
        throw $e;
      }
    }
  }
@@ -63,7 +63,7 @@ protected function getTagInvalidationCounts(array $tags) {
    catch (\Exception $e) {
      // If the table does not exist yet, create.
      if (!$this->ensureTableExists()) {
        $this->catchException($e);
        throw $e;
      }
    }
    return [];
@@ -75,22 +75,19 @@ protected function getTagInvalidationCounts(array $tags) {
  protected function ensureTableExists() {
    try {
      $database_schema = $this->connection->schema();
      // Create the cache tags table if it does not exist.
      if (!$database_schema->tableExists('cachetags')) {
      $schema_definition = $this->schemaDefinition();
      $database_schema->createTable('cachetags', $schema_definition);

        return TRUE;
      }
    }
    // If another process has already created the cachetags table, attempting to
    // recreate it will throw an exception. In this case just catch the
    // exception and do nothing.
    catch (DatabaseException $e) {
      return TRUE;
    }
    catch (\Exception $e) {
      return FALSE;
    }
    return TRUE;
  }

  /**
   * Defines the schema for the {cachetags} table.
@@ -131,8 +128,14 @@ public function schemaDefinition() {
   *   The exception.
   *
   * @throws \Exception
   *
   * @deprecated in drupal:9.3.0 and is removed from drupal:10.0.0. There is no
   *   replacement.
   *
   * @see https://www.drupal.org/node/3243014
   */
  protected function catchException(\Exception $e) {
    @trigger_error('\Drupal\Core\Cache\DatabaseCacheTagsChecksum::catchException is deprecated in drupal:9.3.0 and is removed from drupal:10.0.0. There is no replacement. See https://www.drupal.org/node/3243014', E_USER_DEPRECATED);
    if ($this->connection->schema()->tableExists('cachetags')) {
      throw $e;
    }
+3 −6
Original line number Diff line number Diff line
@@ -128,7 +128,7 @@ public function write($name, array $data) {
        return $this->doWrite($name, $data);
      }
      // Some other failure that we can not recover from.
      throw $e;
      throw new StorageException($e->getMessage(), 0, $e);
    }
  }

@@ -161,10 +161,7 @@ protected function doWrite($name, $data) {
   */
  protected function ensureTableExists() {
    try {
      if (!$this->connection->schema()->tableExists($this->table)) {
      $this->connection->schema()->createTable($this->table, static::schemaDefinition());
        return TRUE;
      }
    }
    // If another process has already created the config table, attempting to
    // recreate it will throw an exception. In this case just catch the
@@ -173,10 +170,10 @@ protected function ensureTableExists() {
      return TRUE;
    }
    catch (\Exception $e) {
      throw new StorageException($e->getMessage(), 0, $e);
    }
      return FALSE;
    }
    return TRUE;
  }

  /**
   * Defines the schema for the configuration table.
+3 −4
Original line number Diff line number Diff line
@@ -150,20 +150,19 @@ public function garbageCollection() {
  protected function ensureTableExists() {
    try {
      $database_schema = $this->connection->schema();
      if (!$database_schema->tableExists(static::TABLE_NAME)) {
      $schema_definition = $this->schemaDefinition();
      $database_schema->createTable(static::TABLE_NAME, $schema_definition);
        return TRUE;
      }
    }
    // If another process has already created the table, attempting to create
    // it will throw an exception. In this case just catch the exception and do
    // nothing.
    catch (DatabaseException $e) {
      return TRUE;
    }
    catch (\Exception $e) {
      return FALSE;
    }
    return TRUE;
  }

  /**
   * Act on an exception when flood might be stale.
Loading