Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Database/Adapter/MariaDB.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Database/Adapter/Postgres.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Database/Adapter/SQLite.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
53 changes: 53 additions & 0 deletions tests/e2e/Adapter/Scopes/DocumentTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Comment on lines +1052 to +1055
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Missing adapter method — test will throw a fatal error

getSupportForUpsertOnUniqueIndex() is called here but is not declared in src/Database/Adapter.php (which defines all getSupportFor* abstract methods) and is not implemented in any adapter class. PHP will throw a fatal Call to undefined method error when this test runs, so it can never reach the assertion block or even skip gracefully. The method needs to be added as an abstract declaration in Adapter.php and implemented in each concrete adapter (returning true for MariaDB, which uses ON DUPLICATE KEY UPDATE for any unique constraint, and false for Postgres/SQLite, which use ON CONFLICT ("_uid") and cannot catch conflicts on other unique indexes).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no its present

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, my apologies — getSupportForUpsertOnUniqueIndex() is present in Adapter.php and all the concrete adapters. My comment was incorrect.

Tip: You can give me more context with .greptile/rules.md so I get this right next time.


$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 */
Expand Down
Loading