From 8f28af882aec3e4ba9651a02390c94f7accbaf11 Mon Sep 17 00:00:00 2001 From: ArnabChatterjee20k Date: Fri, 8 May 2026 14:15:11 +0530 Subject: [PATCH] feat(upsert): remove _uid from update checks and add test for upsert on unique index --- src/Database/Adapter/MariaDB.php | 2 +- src/Database/Adapter/Postgres.php | 2 +- src/Database/Adapter/SQLite.php | 2 +- tests/e2e/Adapter/Scopes/DocumentTests.php | 53 ++++++++++++++++++++++ 4 files changed, 56 insertions(+), 3 deletions(-) diff --git a/src/Database/Adapter/MariaDB.php b/src/Database/Adapter/MariaDB.php index 223f91e71..341677d6a 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 2c27e08e7..9982f5016 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 33f370775..a5742850f 100644 --- a/src/Database/Adapter/SQLite.php +++ b/src/Database/Adapter/SQLite.php @@ -1866,7 +1866,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 45824f3bd..f9e4bcb0e 100644 --- a/tests/e2e/Adapter/Scopes/DocumentTests.php +++ b/tests/e2e/Adapter/Scopes/DocumentTests.php @@ -883,6 +883,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 */