Skip to content

Commit 0c6e693

Browse files
committed
Send old for upsert + delete
1 parent fc01e45 commit 0c6e693

File tree

2 files changed

+20
-12
lines changed

2 files changed

+20
-12
lines changed

src/Database/Database.php

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4592,7 +4592,7 @@ public function updateDocuments(
45924592
array_merge($new, $queries),
45934593
forPermission: Database::PERMISSION_UPDATE
45944594
));
4595-
4595+
45964596
if (empty($batch)) {
45974597
break;
45984598
}
@@ -5101,7 +5101,7 @@ function (Document $doc) use (&$result) {
51015101
* @param string $collection
51025102
* @param array<Document> $documents
51035103
* @param int $batchSize
5104-
* @param (callable(Document): void)|null $onNext
5104+
* @param (callable(Document, ?Document): void)|null $onNext
51055105
* @param (callable(Throwable): void)|null $onError
51065106
* @return int
51075107
* @throws StructureException
@@ -5130,7 +5130,8 @@ public function upsertDocuments(
51305130
* @param string $collection
51315131
* @param string $attribute
51325132
* @param array<Document> $documents
5133-
* @param callable|null $onNext
5133+
* @param (callable(Document, ?Document): void)|null $onNext
5134+
* @param (callable(Throwable): void)|null $onError
51345135
* @param int $batchSize
51355136
* @return int
51365137
* @throws StructureException
@@ -5320,7 +5321,7 @@ public function upsertDocumentsWithIncrease(
53205321
}
53215322
}
53225323

5323-
foreach ($batch as $doc) {
5324+
foreach ($batch as $index => $doc) {
53245325
if ($this->resolveRelationships) {
53255326
$doc = $this->silent(fn () => $this->populateDocumentRelationships($collection, $doc));
53265327
}
@@ -5335,7 +5336,13 @@ public function upsertDocumentsWithIncrease(
53355336
$this->purgeCachedDocument($collection->getId(), $doc->getId());
53365337
}
53375338

5338-
$onNext && $onNext($doc);
5339+
$old = $chunk[$index]->getOld();
5340+
5341+
try {
5342+
$onNext && $onNext($doc, $old->isEmpty() ? null : $old);
5343+
} catch (\Throwable $th) {
5344+
$onError ? $onError($th) : throw $th;
5345+
}
53395346
}
53405347
}
53415348

@@ -6002,7 +6009,7 @@ private function deleteCascade(Document $collection, Document $relatedCollection
60026009
* @param string $collection
60036010
* @param array<Query> $queries
60046011
* @param int $batchSize
6005-
* @param (callable(Document): void)|null $onNext
6012+
* @param (callable(Document, Document): void)|null $onNext
60066013
* @param (callable(Throwable): void)|null $onError
60076014
* @return int
60086015
* @throws AuthorizationException
@@ -6095,6 +6102,7 @@ public function deleteDocuments(
60956102
break;
60966103
}
60976104

6105+
$old = array_map(fn ($doc) => clone $doc, $batch);
60986106
$sequences = [];
60996107
$permissionIds = [];
61006108

@@ -6131,7 +6139,7 @@ public function deleteDocuments(
61316139
);
61326140
});
61336141

6134-
foreach ($batch as $document) {
6142+
foreach ($batch as $index => $document) {
61356143
if ($this->getSharedTables() && $this->getTenantPerDocument()) {
61366144
$this->withTenant($document->getTenant(), function () use ($collection, $document) {
61376145
$this->purgeCachedDocument($collection->getId(), $document->getId());
@@ -6140,7 +6148,7 @@ public function deleteDocuments(
61406148
$this->purgeCachedDocument($collection->getId(), $document->getId());
61416149
}
61426150
try {
6143-
$onNext && $onNext($document);
6151+
$onNext && $onNext($document, $old[$index]);
61446152
} catch (Throwable $th) {
61456153
$onError ? $onError($th) : throw $th;
61466154
}

src/Database/Mirror.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -793,8 +793,8 @@ public function upsertDocuments(
793793
$collection,
794794
$documents,
795795
$batchSize,
796-
function ($doc) use ($onNext, &$modified) {
797-
$onNext && $onNext($doc);
796+
function ($doc, $old) use ($onNext, &$modified) {
797+
$onNext && $onNext($doc, $old);
798798
$modified++;
799799
}
800800
);
@@ -911,8 +911,8 @@ public function deleteDocuments(
911911
$collection,
912912
$queries,
913913
$batchSize,
914-
function ($doc) use (&$modified, $onNext) {
915-
$onNext && $onNext($doc);
914+
function ($doc, $old) use (&$modified, $onNext) {
915+
$onNext && $onNext($doc, $old);
916916
$modified++;
917917
},
918918
$onError

0 commit comments

Comments
 (0)