From b38ea71673663f2cb7aba26cb6ac0be5b2d6d1ec Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Tue, 17 Jun 2025 18:47:10 -0400 Subject: [PATCH] Throw conflict on upsert --- src/Database/Database.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/Database/Database.php b/src/Database/Database.php index 6ed45aa99..6d55e5f17 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -5025,6 +5025,19 @@ public function createOrUpdateDocumentsWithIncrease( throw new StructureException($validator->getDescription()); } + if (!$old->isEmpty()) { + // Check if document was updated after the request timestamp + try { + $oldUpdatedAt = new \DateTime($old->getUpdatedAt()); + } catch (Exception $e) { + throw new DatabaseException($e->getMessage(), $e->getCode(), $e); + } + + if (!\is_null($this->timestamp) && $oldUpdatedAt > $this->timestamp) { + throw new ConflictException('Document was updated after the request timestamp'); + } + } + if ($this->resolveRelationships) { $document = $this->silent(fn () => $this->createDocumentRelationships($collection, $document)); }