From 1fbfbbf482169a96446a0b4a3edf26bfb0aa3a74 Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Mon, 17 Feb 2025 23:53:43 +1300 Subject: [PATCH] Also update database worker delete by group --- src/Appwrite/Platform/Workers/Databases.php | 40 +++++++-------------- src/Appwrite/Platform/Workers/Deletes.php | 4 +-- 2 files changed, 15 insertions(+), 29 deletions(-) diff --git a/src/Appwrite/Platform/Workers/Databases.php b/src/Appwrite/Platform/Workers/Databases.php index 441b09b4cc..0f400e0107 100644 --- a/src/Appwrite/Platform/Workers/Databases.php +++ b/src/Appwrite/Platform/Workers/Databases.php @@ -577,39 +577,25 @@ class Databases extends Action */ protected function deleteByGroup(string $collection, array $queries, Database $database, callable $callback = null): void { - $count = 0; - $chunk = 0; - $limit = 50; - $sum = $limit; + $start = \microtime(true); - $executionStart = \microtime(true); + try { + $documents = $database->deleteDocuments($collection, $queries); + } catch (\Throwable $th) { + Console::error('Failed to delete documents for collection ' . $collection . ': ' . $th->getMessage()); + return; + } - while ($sum === $limit) { - $chunk++; - - $results = $database->find($collection, \array_merge([Query::limit($limit)], $queries)); - - $sum = count($results); - - Console::info('Deleting chunk #' . $chunk . '. Found ' . $sum . ' documents'); - - foreach ($results as $document) { - if ($database->deleteDocument($document->getCollection(), $document->getId())) { - Console::success('Deleted document "' . $document->getId() . '" successfully'); - - if (\is_callable($callback)) { - $callback($document); - } - } else { - Console::warning('Failed to delete document: ' . $document->getId()); - } - $count++; + if (\is_callable($callback)) { + foreach ($documents as $document) { + $callback($document); } } - $executionEnd = \microtime(true); + $end = \microtime(true); + $count = \count($documents); - Console::info("Deleted {$count} document by group in " . ($executionEnd - $executionStart) . " seconds"); + Console::info("Deleted {$count} documents by group in " . ($end - $start) . " seconds"); } protected function trigger( diff --git a/src/Appwrite/Platform/Workers/Deletes.php b/src/Appwrite/Platform/Workers/Deletes.php index 6c2286294c..e11181d199 100644 --- a/src/Appwrite/Platform/Workers/Deletes.php +++ b/src/Appwrite/Platform/Workers/Deletes.php @@ -947,8 +947,8 @@ class Deletes extends Action try { $documents = $database->deleteDocuments($collection, $queries); - } catch (Exception $e) { - Console::error('Failed to delete documents for collection ' . $collection . ': ' . $e->getMessage()); + } catch (\Throwable $th) { + Console::error('Failed to delete documents for collection ' . $collection . ': ' . $th->getMessage()); return; }