diff --git a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Transactions/Update.php b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Transactions/Update.php index 927adb9bc7..26fd631f3a 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Transactions/Update.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Transactions/Update.php @@ -167,8 +167,10 @@ class Update extends Action } } - $totalOperations++; - $databaseOperations[$databaseInternalId] = ($databaseOperations[$databaseInternalId] ?? 0) + 1; + if (!\in_array($action, ['bulkCreate', 'bulkUpdate', 'bulkUpsert', 'bulkDelete'])) { + $totalOperations++; + $databaseOperations[$databaseInternalId] = ($databaseOperations[$databaseInternalId] ?? 0) + 1; + } if ($data instanceof Document) { $data = $data->getArrayCopy(); @@ -194,16 +196,24 @@ class Update extends Action $this->handleDecrementOperation($dbForProject, $collectionId, $documentId, $data, $createdAt, $state); break; case 'bulkCreate': - $this->handleBulkCreateOperation($dbForProject, $collectionId, $data, $createdAt, $state); + $count = $this->handleBulkCreateOperation($dbForProject, $collectionId, $data, $createdAt, $state); + $totalOperations += $count; + $databaseOperations[$databaseInternalId] = ($databaseOperations[$databaseInternalId] ?? 0) + $count; break; case 'bulkUpdate': - $this->handleBulkUpdateOperation($dbForProject, $transactionState, $collectionId, $data, $createdAt, $state); + $count = $this->handleBulkUpdateOperation($dbForProject, $transactionState, $collectionId, $data, $createdAt, $state); + $totalOperations += $count; + $databaseOperations[$databaseInternalId] = ($databaseOperations[$databaseInternalId] ?? 0) + $count; break; case 'bulkUpsert': - $this->handleBulkUpsertOperation($dbForProject, $transactionState, $collectionId, $data, $createdAt, $state); + $count = $this->handleBulkUpsertOperation($dbForProject, $transactionState, $collectionId, $data, $createdAt, $state); + $totalOperations += $count; + $databaseOperations[$databaseInternalId] = ($databaseOperations[$databaseInternalId] ?? 0) + $count; break; case 'bulkDelete': - $this->handleBulkDeleteOperation($dbForProject, $transactionState, $collectionId, $data, $createdAt, $state); + $count = $this->handleBulkDeleteOperation($dbForProject, $transactionState, $collectionId, $data, $createdAt, $state); + $totalOperations += $count; + $databaseOperations[$databaseInternalId] = ($databaseOperations[$databaseInternalId] ?? 0) + $count; break; } } @@ -645,7 +655,7 @@ class Update extends Action * @param array $data * @param \DateTime $createdAt * @param array &$state - * @return void + * @return int Number of documents created * @throws \Utopia\Database\Exception */ private function handleBulkCreateOperation( @@ -654,13 +664,14 @@ class Update extends Action array $data, \DateTime $createdAt, array &$state - ): void { - $dbForProject->withRequestTimestamp($createdAt, function () use ($dbForProject, $collectionId, $data, &$state) { + ): int { + $count = 0; + $dbForProject->withRequestTimestamp($createdAt, function () use ($dbForProject, $collectionId, $data, &$state, &$count) { $documents = \array_map(function ($doc) { return $doc instanceof Document ? $doc : new Document($doc); }, $data); - $dbForProject->createDocuments( + $count = $dbForProject->createDocuments( $collectionId, $documents, onNext: function (Document $document) use (&$state, $collectionId) { @@ -668,6 +679,7 @@ class Update extends Action } ); }); + return $count; } /** @@ -679,7 +691,7 @@ class Update extends Action * @param array $data * @param \DateTime $createdAt * @param array &$state - * @return void + * @return int Number of documents updated * @throws \Utopia\Database\Exception * @throws \Utopia\Database\Exception\Query * @throws ConflictException @@ -691,7 +703,7 @@ class Update extends Action array $data, \DateTime $createdAt, array &$state - ): void { + ): int { $queries = Query::parseQueries($data['queries'] ?? []); $updateData = new Document($data['data']); @@ -701,7 +713,7 @@ class Update extends Action // Clone the document before passing to updateDocuments to prevent mutation // The database layer mutates the input document, which would corrupt transaction state - $dbForProject->updateDocuments( + $count = $dbForProject->updateDocuments( $collectionId, clone $updateData, $queries, @@ -739,6 +751,8 @@ class Update extends Action ); } } + + return $count; } /** @@ -750,7 +764,7 @@ class Update extends Action * @param array $data * @param \DateTime $createdAt * @param array &$state - * @return void + * @return int Number of documents upserted * @throws ConflictException * @throws \Utopia\Database\Exception */ @@ -761,14 +775,14 @@ class Update extends Action array $data, \DateTime $createdAt, array &$state - ): void { + ): int { $documents = \array_map(function ($doc) { return $doc instanceof Document ? $doc : new Document($doc); }, $data); $mergedDocuments = $transactionState->applyBulkUpsertToState($collectionId, $documents, $state); - $dbForProject->upsertDocuments( + $count = $dbForProject->upsertDocuments( $collectionId, $mergedDocuments, onNext: function (Document $upserted, ?Document $old) use (&$state, $collectionId, $createdAt) { @@ -786,6 +800,8 @@ class Update extends Action $state[$collectionId][$upserted->getId()] = $upserted; } ); + + return $count; } /** @@ -797,7 +813,7 @@ class Update extends Action * @param array $data * @param \DateTime $createdAt * @param array &$state - * @return void + * @return int Number of documents deleted * @throws \Utopia\Database\Exception\Query * @throws ConflictException * @throws \Utopia\Database\Exception @@ -809,10 +825,10 @@ class Update extends Action array $data, \DateTime $createdAt, array &$state - ): void { + ): int { $queries = Query::parseQueries($data['queries'] ?? []); - $dbForProject->deleteDocuments( + $count = $dbForProject->deleteDocuments( $collectionId, $queries, onNext: function (Document $deleted, Document $old) use (&$state, $collectionId, $createdAt) { @@ -832,5 +848,7 @@ class Update extends Action ); $transactionState->applyBulkDeleteToState($collectionId, $queries, $state); + + return $count; } }