Skip to content

Commit e2d0a26

Browse files
committed
Catch duplicate ID edge case
1 parent 71efc8c commit e2d0a26

1 file changed

Lines changed: 8 additions & 1 deletion

File tree

src/Database/Database.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4920,7 +4920,7 @@ public function createOrUpdateDocumentsWithIncrease(
49204920
$time = DateTime::now();
49214921
$created = 0;
49224922
$updated = 0;
4923-
4923+
$seenIds = [];
49244924
foreach ($documents as $key => $document) {
49254925
if ($this->getSharedTables() && $this->getTenantPerDocument()) {
49264926
$old = Authorization::skip(fn () => $this->withTenant($document->getTenant(), fn () => $this->silent(fn () => $this->getDocument(
@@ -5028,12 +5028,19 @@ public function createOrUpdateDocumentsWithIncrease(
50285028
$document = $this->silent(fn () => $this->createDocumentRelationships($collection, $document));
50295029
}
50305030

5031+
$seenIds[] = $document->getId();
5032+
50315033
$documents[$key] = new Change(
50325034
old: $old,
50335035
new: $document
50345036
);
50355037
}
50365038

5039+
// Required because *some* DBs will allow duplicate IDs for upsert
5040+
if (\count($seenIds) !== \count(\array_unique($seenIds))) {
5041+
throw new DuplicateException('Duplicate document IDs found in the input array.');
5042+
}
5043+
50375044
foreach (\array_chunk($documents, $batchSize) as $chunk) {
50385045
/**
50395046
* @var array<Change> $chunk

0 commit comments

Comments
 (0)