diff --git a/core/lib/Drupal/Core/Cache/DatabaseBackend.php b/core/lib/Drupal/Core/Cache/DatabaseBackend.php index f0a9648..49e830b 100644 --- a/core/lib/Drupal/Core/Cache/DatabaseBackend.php +++ b/core/lib/Drupal/Core/Cache/DatabaseBackend.php @@ -234,20 +234,34 @@ public function setMultiple(array $items) { $values[] = $fields; } - // Use an upsert query which is atomic and optimized for multiple-row - // merges. - $query = $this->connection - ->upsert($this->bin) - ->key('cid') - ->fields(array('cid', 'expire', 'created', 'tags', 'checksum', 'data', 'serialized')); - foreach ($values as $fields) { - // Only pass the values since the order of $fields matches the order of - // the insert fields. This is a performance optimization to avoid - // unnecessary loops within the method. - $query->values(array_values($fields)); - } - - $query->execute(); + // Use a transaction so that the database can write the changes in a single + // commit. The transaction is started after calculating the tag checksums + // since that can create a table and this causes an exception when using + // PostgreSQL. + $transaction = $this->connection->startTransaction(); + + try { + // Delete all items first so we can do one insert. Rather than multiple + // merge queries. + $this->deleteMultiple(array_keys($items)); + + $query = $this->connection + ->insert($this->bin) + ->fields(array('cid', 'expire', 'created', 'tags', 'checksum', 'data', 'serialized')); + foreach ($values as $fields) { + // Only pass the values since the order of $fields matches the order of + // the insert fields. This is a performance optimization to avoid + // unnecessary loops within the method. + $query->values(array_values($fields)); + } + + $query->execute(); + } + catch (\Exception $e) { + $transaction->rollback(); + // @todo Log something here or just re throw? + throw $e; + } } /** diff --git a/core/lib/Drupal/Core/Database/Driver/mysql/Upsert.php b/core/lib/Drupal/Core/Database/Driver/mysql/Upsert.php index b258a66..d2da688 100644 --- a/core/lib/Drupal/Core/Database/Driver/mysql/Upsert.php +++ b/core/lib/Drupal/Core/Database/Driver/mysql/Upsert.php @@ -30,7 +30,7 @@ public function __toString() { $query .= implode(', ', $values); // Updating the unique / primary key is not necessary. - unset($insert_fields[$this->key]); + unset($insert_fields[$this->keys]); $update = []; foreach ($insert_fields as $field) { diff --git a/core/lib/Drupal/Core/Database/Driver/pgsql/Upsert.php b/core/lib/Drupal/Core/Database/Driver/pgsql/Upsert.php index b64869c..3d3f31f 100644 --- a/core/lib/Drupal/Core/Database/Driver/pgsql/Upsert.php +++ b/core/lib/Drupal/Core/Database/Driver/pgsql/Upsert.php @@ -32,7 +32,7 @@ public function execute() { $insert_fields_escaped = array_map(function($f) { return $this->connection->escapeField($f); }, $insert_fields); $table = $this->connection->escapeTable($this->table); - $unique_key = $this->connection->escapeField($this->key); + $unique_key = $this->connection->escapeField($this->keys); // We have to execute multiple queries, therefore we wrap everything in a // transaction so that it is atomic where possible. @@ -85,8 +85,8 @@ public function execute() { // WHERE table_name.id IS NULL; $select = $this->connection->select($temp_table, 'temp_table', $this->queryOptions) ->fields('temp_table', $insert_fields); - $select->leftJoin($this->table, 'actual_table', 'actual_table.' . $this->key . ' = temp_table.' . $this->key); - $select->isNull('actual_table.' . $this->key); + $select->leftJoin($this->table, 'actual_table', 'actual_table.' . $this->keys . ' = temp_table.' . $this->keys); + $select->isNull('actual_table.' . $this->keys); $this->connection->insert($this->table, $this->queryOptions) ->from($select) diff --git a/core/lib/Drupal/Core/Database/Query/Upsert.php b/core/lib/Drupal/Core/Database/Query/Upsert.php index fbbcbf4..b10d3ea 100644 --- a/core/lib/Drupal/Core/Database/Query/Upsert.php +++ b/core/lib/Drupal/Core/Database/Query/Upsert.php @@ -21,11 +21,14 @@ class Upsert extends InsertBase { /** - * The unique or primary key of the table. + * The name of the fields that form a unique or primary key of the table. * - * @var string + * @todo Only a single field is supported at the moment. Revisit this decision + * in https://www.drupal.org/node/2547493 + * + * @var array */ - protected $key; + protected $keys; /** * Constructs an Upsert object. @@ -35,24 +38,33 @@ class Upsert extends InsertBase { * @param string $table * Name of the table to associate with this query. * @param array $options - * Array of database options. + * (optional) An array of database options. */ - public function __construct(Connection $connection, $table, array $options = array()) { + public function __construct(Connection $connection, $table, array $options = []) { $options['return'] = Database::RETURN_AFFECTED; parent::__construct($connection, $options); $this->table = $table; } /** - * Sets the unique / primary key field to be used as condition for this query. + * Sets the unique fields to be used as conditions for this query. * - * @param string $field - * The name of the field to set. + * @param array $fields + * The name of the fields that form an unique key. * * @return $this */ - public function key($field) { - $this->key = $field; + public function key($fields) { + $fields = (array) $fields; + + if (count($fields) > 1) { + throw new \InvalidArgumentException('Only '); + } + + // @todo We only support unique indexes that are composed of a single field. + // Decide if we can expand support to multiple fields in + // https://www.drupal.org/node/2547493 + $this->keys = reset($fields); return $this; } @@ -69,7 +81,7 @@ public function key($field) { */ public function preExecute() { // Confirm that the user set the unique/primary key of the table. - if (!$this->key) { + if (!$this->keys) { throw new NoUniqueFieldException('There is no unique field specified.'); } @@ -117,14 +129,12 @@ public function execute() { /** * Implements PHP magic __toString method to convert the query to a string. * - * In the degenerate case, there is no string-able query as this operation - * is potentially two queries. - * * @return string * The prepared query statement. */ public function __toString() { - throw new \BadMethodCallException('There is no generic cross-database way to implement UPSERT queries, each driver must implement their own logic.'); + // There is no generic cross-database way to implement UPSERT queries, each + // driver must implement their own logic. } }