Skip to content

Commit 5d90f89

Browse files
committed
move while loop
1 parent 6ae8540 commit 5d90f89

File tree

2 files changed

+57
-52
lines changed

2 files changed

+57
-52
lines changed

src/Database/Database.php

Lines changed: 51 additions & 51 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+
$documents = $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(),

tests/e2e/Adapter/Base.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16518,7 +16518,12 @@ public function testDeleteBulkDocuments(): void
1651816518
$this->assertCount(10, $docs);
1651916519

1652016520
// TEST: Bulk Delete All Documents
16521-
$this->assertCount(10, static::getDatabase()->deleteDocuments('bulk_delete'));
16521+
$this->assertCount(10, static::getDatabase()->deleteDocuments(
16522+
'bulk_delete',
16523+
[
16524+
Query::select(['$internalId', '$uid', '$permissions', '$updatedAt'])
16525+
]
16526+
));
1652216527

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

0 commit comments

Comments
 (0)