Skip to content

Commit aa0116b

Browse files
authored
Merge pull request #610 from utopia-php/feat-upsert-conflict
Throw conflict on upsert
2 parents 2bd87ac + b38ea71 commit aa0116b

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

src/Database/Database.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5025,6 +5025,19 @@ public function createOrUpdateDocumentsWithIncrease(
50255025
throw new StructureException($validator->getDescription());
50265026
}
50275027

5028+
if (!$old->isEmpty()) {
5029+
// Check if document was updated after the request timestamp
5030+
try {
5031+
$oldUpdatedAt = new \DateTime($old->getUpdatedAt());
5032+
} catch (Exception $e) {
5033+
throw new DatabaseException($e->getMessage(), $e->getCode(), $e);
5034+
}
5035+
5036+
if (!\is_null($this->timestamp) && $oldUpdatedAt > $this->timestamp) {
5037+
throw new ConflictException('Document was updated after the request timestamp');
5038+
}
5039+
}
5040+
50285041
if ($this->resolveRelationships) {
50295042
$document = $this->silent(fn () => $this->createDocumentRelationships($collection, $document));
50305043
}

0 commit comments

Comments
 (0)