diff --git a/src/Database/Adapter/MariaDB.php b/src/Database/Adapter/MariaDB.php index 599da878e..ab030334e 100644 --- a/src/Database/Adapter/MariaDB.php +++ b/src/Database/Adapter/MariaDB.php @@ -1281,7 +1281,7 @@ public function getUpsertStatement( $updateColumns[] = $operatorSQL; } } else { - if (!in_array($attr, ['_uid', '_id', '_createdAt', '_tenant'])) { + if (!in_array($attr, ['_id', '_createdAt', '_tenant'])) { $updateColumns[] = $getUpdateClause($filteredAttr); } } diff --git a/src/Database/Adapter/Postgres.php b/src/Database/Adapter/Postgres.php index d81cdec0b..189d90afc 100644 --- a/src/Database/Adapter/Postgres.php +++ b/src/Database/Adapter/Postgres.php @@ -1423,7 +1423,7 @@ protected function getUpsertStatement( $updateColumns[] = $operatorSQL; } } else { - if (!in_array($attr, ['_uid', '_id', '_createdAt', '_tenant'])) { + if (!in_array($attr, ['_id', '_createdAt', '_tenant'])) { $updateColumns[] = $getUpdateClause($filteredAttr); } } diff --git a/src/Database/Adapter/SQLite.php b/src/Database/Adapter/SQLite.php index 5b07ce3b7..3aceb1af2 100644 --- a/src/Database/Adapter/SQLite.php +++ b/src/Database/Adapter/SQLite.php @@ -2517,7 +2517,7 @@ public function getUpsertStatement( $updateColumns[] = $operatorSQL; } } else { - if (!in_array($attr, ['_uid', '_id', '_createdAt', '_tenant'])) { + if (!in_array($attr, ['_id', '_createdAt', '_tenant'])) { $updateColumns[] = $getUpdateClause($filteredAttr); } } diff --git a/tests/e2e/Adapter/Scopes/DocumentTests.php b/tests/e2e/Adapter/Scopes/DocumentTests.php index 23cc3b623..f1309bbe6 100644 --- a/tests/e2e/Adapter/Scopes/DocumentTests.php +++ b/tests/e2e/Adapter/Scopes/DocumentTests.php @@ -1043,6 +1043,59 @@ public function testUpsertDocumentsInc(): void } } + public function testUpsertOnUniqueIndexCanUpdateUid(): void + { + /** @var Database $database */ + $database = $this->getDatabase(); + $collection = __FUNCTION__; + + if (!$database->getAdapter()->getSupportForUpsertOnUniqueIndex()) { + $this->expectNotToPerformAssertions(); + return; + } + + $database->createCollection($collection); + $database->createAttribute($collection, 'email', Database::VAR_STRING, 255, true); + $database->createAttribute($collection, 'name', Database::VAR_STRING, 255, true); + $database->createIndex($collection, 'email_unique', Database::INDEX_UNIQUE, ['email']); + + $original = $database->createDocument($collection, new Document([ + '$id' => 'original-id', + 'email' => 'uid-upsert@example.test', + 'name' => 'before', + ])); + + $originalSequence = $original->getSequence(); + + $count = $database->upsertDocuments($collection, [ + new Document([ + '$id' => 'updated-id', + 'email' => 'uid-upsert@example.test', + 'name' => 'after', + ]) + ]); + + $this->assertEquals(1, $count); + + $oldDoc = $database->getAuthorization()->skip( + fn () => $database->getDocument($collection, 'original-id') + ); + $this->assertTrue($oldDoc->isEmpty()); + + $newDoc = $database->getAuthorization()->skip( + fn () => $database->getDocument($collection, 'updated-id') + ); + $this->assertFalse($newDoc->isEmpty()); + $this->assertEquals('after', $newDoc->getAttribute('name')); + $this->assertEquals('uid-upsert@example.test', $newDoc->getAttribute('email')); + $this->assertEquals($originalSequence, $newDoc->getSequence()); + + $allDocs = $database->getAuthorization()->skip(fn () => $database->find($collection)); + $this->assertCount(1, $allDocs); + + $database->deleteCollection($collection); + } + public function testUpsertDocumentsPermissions(): void { /** @var Database $database */