Skip to content

Commit

Permalink
Merge pull request #528 from utopia-php/fix-order-deletes
Browse files Browse the repository at this point in the history
Move cache clear + trigger outside delete transaction loop
  • Loading branch information
abnegate authored Feb 17, 2025
2 parents d4e17e7 + 8534d13 commit 1e42d2f
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions src/Database/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -5376,7 +5376,7 @@ public function deleteDocuments(string $collection, array $queries = [], int $ba
$cursor = $grouped['cursor'];

if (!empty($cursor) && $cursor->getCollection() !== $collection->getId()) {
throw new DatabaseException("cursor Document must be from the same Collection.");
throw new DatabaseException("Cursor document must be from the same Collection.");
}

$documents = $this->withTransaction(function () use ($collection, $queries, $batchSize, $limit, $cursor) {
Expand Down Expand Up @@ -5413,12 +5413,14 @@ public function deleteDocuments(string $collection, array $queries = [], int $ba
break;
}

$documents = array_merge($affectedDocuments, $documents);
$documents = \array_merge($affectedDocuments, $documents);

foreach ($affectedDocuments as $document) {
// Delete Relationships
if ($this->resolveRelationships) {
$document = $this->silent(fn () => $this->deleteDocumentRelationships($collection, $document));
$document = $this->silent(fn () => $this->deleteDocumentRelationships(
$collection,
$document
));
}

// Check if document was updated after the request timestamp
Expand All @@ -5431,8 +5433,6 @@ public function deleteDocuments(string $collection, array $queries = [], int $ba
if (!\is_null($this->timestamp) && $oldUpdatedAt > $this->timestamp) {
throw new ConflictException('Document was updated after the request timestamp');
}

$this->purgeCachedDocument($collection->getId(), $document->getId());
}

if (count($affectedDocuments) < $batchSize) {
Expand All @@ -5448,11 +5448,6 @@ public function deleteDocuments(string $collection, array $queries = [], int $ba
return [];
}

$this->trigger(self::EVENT_DOCUMENTS_DELETE, new Document([
'$collection' => $collection->getId(),
'modified' => count($documents)
]));

foreach (\array_chunk($documents, $batchSize) as $chunk) {
$this->adapter->deleteDocuments(
$collection->getId(),
Expand All @@ -5463,6 +5458,15 @@ public function deleteDocuments(string $collection, array $queries = [], int $ba
return $documents;
});

foreach ($documents as $document) {
$this->purgeCachedDocument($collection->getId(), $document->getId());
}

$this->trigger(self::EVENT_DOCUMENTS_DELETE, new Document([
'$collection' => $collection->getId(),
'modified' => count($documents)
]));

return $documents;
}

Expand Down

0 comments on commit 1e42d2f

Please sign in to comment.