diff --git a/core/lib/Drupal/Core/Cache/DatabaseBackend.php b/core/lib/Drupal/Core/Cache/DatabaseBackend.php index 69c50f3..f0a9648 100644 --- a/core/lib/Drupal/Core/Cache/DatabaseBackend.php +++ b/core/lib/Drupal/Core/Cache/DatabaseBackend.php @@ -238,6 +238,7 @@ public function setMultiple(array $items) { // 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 diff --git a/core/lib/Drupal/Core/Database/Driver/mysql/Upsert.php b/core/lib/Drupal/Core/Database/Driver/mysql/Upsert.php index a1a2937..f02c2de 100644 --- a/core/lib/Drupal/Core/Database/Driver/mysql/Upsert.php +++ b/core/lib/Drupal/Core/Database/Driver/mysql/Upsert.php @@ -72,6 +72,9 @@ public function __toString() { $query .= implode(', ', $values); + // Updating the unique / primary key is not necessary. + unset($insert_fields[$this->key]); + $update = []; foreach ($insert_fields as $field) { $update[] = "$field = VALUES($field)"; diff --git a/core/lib/Drupal/Core/Database/Query/NoUniqueFieldException.php b/core/lib/Drupal/Core/Database/Query/NoUniqueFieldException.php new file mode 100644 index 0000000..9dade14 --- /dev/null +++ b/core/lib/Drupal/Core/Database/Query/NoUniqueFieldException.php @@ -0,0 +1,15 @@ +key = $field; + + return $this; + } + + /** * Adds a set of field->value pairs to be inserted. * * @param $fields @@ -129,13 +151,19 @@ public function values(array $values) { /** * Preprocesses and validates the query. * - * @return - * TRUE if the validation was successful, FALSE if not. + * @return bool + * TRUE if the validation was successful, FALSE otherwise. * + * @throws \Drupal\Core\Database\Query\NoUniqueFieldException * @throws \Drupal\Core\Database\Query\FieldsOverlapException * @throws \Drupal\Core\Database\Query\NoFieldsException */ public function preExecute() { + // Confirm that the user set the unique/primary key of the table. + if (!$this->key) { + throw new NoUniqueFieldException('There is no unique field specified.'); + } + // Confirm that the user did not try to specify an identical // field and default field. if (array_intersect($this->insertFields, $this->defaultFields)) { diff --git a/core/modules/system/src/Tests/Database/UpsertTest.php b/core/modules/system/src/Tests/Database/UpsertTest.php index 298aa80..47c6a69 100644 --- a/core/modules/system/src/Tests/Database/UpsertTest.php +++ b/core/modules/system/src/Tests/Database/UpsertTest.php @@ -24,7 +24,8 @@ public function testMergeInsert() { $connection = Database::getConnection(); $num_records_before = $connection->query('SELECT COUNT(*) FROM {test_people}')->fetchField(); - $result = $connection->upsert('test_people') + $connection->upsert('test_people') + ->key('job') ->fields(array( 'job' => 'Presenter', 'age' => 31, @@ -32,8 +33,6 @@ public function testMergeInsert() { )) ->execute(); - $this->assertEqual($result, Upsert::STATUS_INSERT, 'Insert status returned.'); - $num_records_after = $connection->query('SELECT COUNT(*) FROM {test_people}')->fetchField(); $this->assertEqual($num_records_before + 1, $num_records_after, 'Merge inserted properly.'); @@ -50,7 +49,8 @@ public function testMergeUpdate() { $connection = Database::getConnection(); $num_records_before = $connection->query('SELECT COUNT(*) FROM {test_people}')->fetchField(); - $result = $connection->upsert('test_people') + $connection->upsert('test_people') + ->key('job') ->fields(array( 'job' => 'Speaker', 'age' => 31, @@ -58,8 +58,6 @@ public function testMergeUpdate() { )) ->execute(); - $this->assertEqual($result, Upsert::STATUS_UPDATE, 'Update status returned.'); - $num_records_after = $connection->query('SELECT COUNT(*) FROM {test_people}')->fetchField(); $this->assertEqual($num_records_before, $num_records_after, 'Merge updated properly.');