From 26bf353614ea3fe0a71ca5e690dcf7a0a9f628db Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Thu, 8 May 2025 20:04:55 +1200 Subject: [PATCH 1/4] Check not updating a single attribute on skip check --- src/Database/Database.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/Database/Database.php b/src/Database/Database.php index 43f4cd923..35f41d0dd 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -4732,7 +4732,13 @@ public function createOrUpdateDocumentsWithIncrease( $updatesPermissions = \in_array('$permissions', \array_keys($document->getArrayCopy())) && $document->getPermissions() != $old->getPermissions(); - if ($old->getAttributes() == $document->getAttributes() && !$updatesPermissions) { + if ( + empty($attribute) + && !$updatesPermissions + && $old->getAttributes() == $document->getAttributes() + ) { + // If not updating a single attribute and the + // document is the same as the old one, skip it unset($documents[$key]); continue; } From 1692e90a1be0e7102bcc4f03d1f394396782f942 Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Thu, 8 May 2025 20:24:50 +1200 Subject: [PATCH 2/4] Separate created/updated count for upsert --- src/Database/Database.php | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/src/Database/Database.php b/src/Database/Database.php index 35f41d0dd..58dc1f370 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -4710,11 +4710,8 @@ public function createOrUpdateDocumentsWithIncrease( $documentSecurity = $collection->getAttribute('documentSecurity', false); $time = DateTime::now(); - $selects = ['$internalId', '$permissions']; - - if ($this->getSharedTables()) { - $selects[] = '$tenant'; - } + $created = 0; + $updated = 0; foreach ($documents as $key => $document) { if ($this->getSharedTables() && $this->getTenantPerDocument()) { @@ -4754,14 +4751,20 @@ public function createOrUpdateDocumentsWithIncrease( ); if ($old->isEmpty()) { + $created++; + if (!$validator->isValid($collection->getCreate())) { throw new AuthorizationException($validator->getDescription()); } - } elseif (!$validator->isValid([ - ...$collection->getUpdate(), - ...($documentSecurity ? $old->getUpdate() : []) - ])) { - throw new AuthorizationException($validator->getDescription()); + } else { + $updated++; + + if (!$validator->isValid([ + ...$collection->getUpdate(), + ...($documentSecurity ? $old->getUpdate() : []) + ])) { + throw new AuthorizationException($validator->getDescription()); + } } $createdAt = $document->getCreatedAt(); @@ -4838,16 +4841,16 @@ public function createOrUpdateDocumentsWithIncrease( } $onNext && $onNext($doc); - $modified++; } } $this->trigger(self::EVENT_DOCUMENTS_UPSERT, new Document([ '$collection' => $collection->getId(), - 'modified' => $modified, + 'created' => $created, + 'updated' => $updated, ])); - return $modified; + return $created + $updated; } /** From 9b25b004346d8ffd3aa30d89acfda258f9146369 Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Thu, 8 May 2025 21:04:33 +1200 Subject: [PATCH 3/4] Only count on success --- src/Database/Database.php | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/src/Database/Database.php b/src/Database/Database.php index 58dc1f370..713a4e508 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -4751,20 +4751,14 @@ public function createOrUpdateDocumentsWithIncrease( ); if ($old->isEmpty()) { - $created++; - if (!$validator->isValid($collection->getCreate())) { throw new AuthorizationException($validator->getDescription()); } - } else { - $updated++; - - if (!$validator->isValid([ - ...$collection->getUpdate(), - ...($documentSecurity ? $old->getUpdate() : []) - ])) { - throw new AuthorizationException($validator->getDescription()); - } + } elseif (!$validator->isValid([ + ...$collection->getUpdate(), + ...($documentSecurity ? $old->getUpdate() : []) + ])) { + throw new AuthorizationException($validator->getDescription()); } $createdAt = $document->getCreatedAt(); @@ -4815,8 +4809,6 @@ public function createOrUpdateDocumentsWithIncrease( ); } - $modified = 0; - foreach (\array_chunk($documents, $batchSize) as $chunk) { /** * @var array $chunk @@ -4827,6 +4819,14 @@ public function createOrUpdateDocumentsWithIncrease( $chunk ))); + foreach ($chunk as $change) { + if ($change->getOld()->isEmpty()) { + $created++; + } else { + $updated++; + } + } + foreach ($batch as $doc) { if ($this->resolveRelationships) { $doc = $this->silent(fn () => $this->populateDocumentRelationships($collection, $doc)); From d23b1fe446f2f8d8f8e1ebdc1fc89ada16caf6f0 Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Thu, 8 May 2025 21:04:44 +1200 Subject: [PATCH 4/4] Decode before on next --- src/Database/Database.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Database/Database.php b/src/Database/Database.php index 713a4e508..c50354a1f 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -4832,6 +4832,8 @@ public function createOrUpdateDocumentsWithIncrease( $doc = $this->silent(fn () => $this->populateDocumentRelationships($collection, $doc)); } + $doc = $this->decode($collection, $doc); + if ($this->getSharedTables() && $this->getTenantPerDocument()) { $this->withTenant($doc->getTenant(), function () use ($collection, $doc) { $this->purgeCachedDocument($collection->getId(), $doc->getId());