diff --git a/src/Database/Database.php b/src/Database/Database.php index 43f4cd923..c50354a1f 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()) { @@ -4732,7 +4729,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; } @@ -4806,8 +4809,6 @@ public function createOrUpdateDocumentsWithIncrease( ); } - $modified = 0; - foreach (\array_chunk($documents, $batchSize) as $chunk) { /** * @var array $chunk @@ -4818,11 +4819,21 @@ 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)); } + $doc = $this->decode($collection, $doc); + if ($this->getSharedTables() && $this->getTenantPerDocument()) { $this->withTenant($doc->getTenant(), function () use ($collection, $doc) { $this->purgeCachedDocument($collection->getId(), $doc->getId()); @@ -4832,16 +4843,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; } /**