Skip to content

Commit 9b911d8

Browse files
authored
Merge pull request #539 from utopia-php/deleteDocumentSelect
Accumulate delete Documents outside transaction
2 parents 6ae8540 + 7f6486d commit 9b911d8

File tree

2 files changed

+63
-54
lines changed

2 files changed

+63
-54
lines changed

src/Database/Database.php

Lines changed: 51 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -5418,71 +5418,71 @@ public function deleteDocuments(string $collection, array $queries = [], int $ba
54185418
throw new DatabaseException("Cursor document must be from the same Collection.");
54195419
}
54205420

5421-
$documents = $this->withTransaction(function () use ($collection, $queries, $batchSize, $limit, $cursor, $skipAuth, $authorization) {
5422-
$documents = [];
5423-
$originalLimit = $limit;
5424-
$lastDocument = $cursor;
5425-
5426-
while (true) {
5427-
if ($limit && $limit < $batchSize && $limit > 0) {
5428-
$batchSize = $limit;
5429-
} elseif (!empty($limit)) {
5430-
$limit -= $batchSize;
5431-
}
5432-
5433-
$new = [
5434-
Query::limit($batchSize)
5435-
];
5436-
5437-
if (! empty($lastDocument)) {
5438-
$new[] = Query::cursorAfter($lastDocument);
5439-
}
5421+
$documents = [];
5422+
$originalLimit = $limit;
5423+
$lastDocument = $cursor;
5424+
5425+
while (true) {
5426+
if ($limit && $limit < $batchSize && $limit > 0) {
5427+
$batchSize = $limit;
5428+
} elseif (!empty($limit)) {
5429+
$limit -= $batchSize;
5430+
}
54405431

5441-
$affectedDocuments = $this->silent(fn () => $this->find(
5442-
$collection->getId(),
5443-
array_merge($new, $queries),
5444-
forPermission: Database::PERMISSION_DELETE
5445-
));
5432+
$new = [
5433+
Query::limit($batchSize)
5434+
];
54465435

5447-
if (empty($affectedDocuments)) {
5448-
break;
5449-
}
5436+
if (! empty($lastDocument)) {
5437+
$new[] = Query::cursorAfter($lastDocument);
5438+
}
54505439

5451-
$documents = \array_merge($affectedDocuments, $documents);
5440+
$affectedDocuments = $this->silent(fn () => $this->find(
5441+
$collection->getId(),
5442+
array_merge($new, $queries),
5443+
forPermission: Database::PERMISSION_DELETE
5444+
));
54525445

5453-
foreach ($affectedDocuments as $document) {
5454-
if ($this->resolveRelationships) {
5455-
$document = $this->silent(fn () => $this->deleteDocumentRelationships(
5456-
$collection,
5457-
$document
5458-
));
5459-
}
5446+
if (empty($affectedDocuments)) {
5447+
break;
5448+
}
54605449

5461-
// Check if document was updated after the request timestamp
5462-
try {
5463-
$oldUpdatedAt = new \DateTime($document->getUpdatedAt());
5464-
} catch (Exception $e) {
5465-
throw new DatabaseException($e->getMessage(), $e->getCode(), $e);
5466-
}
5450+
$documents = \array_merge($affectedDocuments, $documents);
54675451

5468-
if (!\is_null($this->timestamp) && $oldUpdatedAt > $this->timestamp) {
5469-
throw new ConflictException('Document was updated after the request timestamp');
5470-
}
5452+
foreach ($affectedDocuments as $document) {
5453+
if ($this->resolveRelationships) {
5454+
$document = $this->silent(fn () => $this->deleteDocumentRelationships(
5455+
$collection,
5456+
$document
5457+
));
54715458
}
54725459

5473-
if (count($affectedDocuments) < $batchSize) {
5474-
break;
5475-
} elseif ($originalLimit && count($documents) == $originalLimit) {
5476-
break;
5460+
// Check if document was updated after the request timestamp
5461+
try {
5462+
$oldUpdatedAt = new \DateTime($document->getUpdatedAt());
5463+
} catch (Exception $e) {
5464+
throw new DatabaseException($e->getMessage(), $e->getCode(), $e);
54775465
}
54785466

5479-
$lastDocument = end($affectedDocuments);
5467+
if (!\is_null($this->timestamp) && $oldUpdatedAt > $this->timestamp) {
5468+
throw new ConflictException('Document was updated after the request timestamp');
5469+
}
54805470
}
54815471

5482-
if (empty($documents)) {
5483-
return [];
5472+
if (count($affectedDocuments) < $batchSize) {
5473+
break;
5474+
} elseif ($originalLimit && count($documents) == $originalLimit) {
5475+
break;
54845476
}
54855477

5478+
$lastDocument = end($affectedDocuments);
5479+
}
5480+
5481+
if (empty($documents)) {
5482+
return [];
5483+
}
5484+
5485+
$this->withTransaction(function () use ($documents, $collection, $batchSize, $skipAuth, $authorization) {
54865486
foreach (\array_chunk($documents, $batchSize) as $chunk) {
54875487
$getResults = fn () => $this->adapter->deleteDocuments(
54885488
$collection->getId(),
@@ -5491,8 +5491,6 @@ public function deleteDocuments(string $collection, array $queries = [], int $ba
54915491

54925492
$skipAuth ? $authorization->skip($getResults) : $getResults();
54935493
}
5494-
5495-
return $documents;
54965494
});
54975495

54985496
foreach ($documents as $document) {

tests/e2e/Adapter/Base.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16517,8 +16517,19 @@ public function testDeleteBulkDocuments(): void
1651716517
$docs = static::getDatabase()->find('bulk_delete');
1651816518
$this->assertCount(10, $docs);
1651916519

16520+
/**
16521+
* Test Short select query
16522+
*/
16523+
$this->assertCount(2, static::getDatabase()->deleteDocuments(
16524+
'bulk_delete',
16525+
[
16526+
Query::select(['$internalId', '$id', '$permissions', '$updatedAt']),
16527+
Query::limit(2)
16528+
]
16529+
));
16530+
1652016531
// TEST: Bulk Delete All Documents
16521-
$this->assertCount(10, static::getDatabase()->deleteDocuments('bulk_delete'));
16532+
$this->assertCount(8, static::getDatabase()->deleteDocuments('bulk_delete'));
1652216533

1652316534
$docs = static::getDatabase()->find('bulk_delete');
1652416535
$this->assertCount(0, $docs);

0 commit comments

Comments
 (0)