Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions src/Database/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -4318,6 +4318,7 @@ public function updateDocuments(
array $queries = [],
int $batchSize = self::INSERT_BATCH_SIZE,
?callable $onNext = null,
?callable $onError = null,
): int {
if ($updates->isEmpty()) {
return 0;
Expand Down Expand Up @@ -4421,10 +4422,6 @@ public function updateDocuments(
foreach ($batch as &$document) {
$new = new Document(\array_merge($document->getArrayCopy(), $updates->getArrayCopy()));

if ($this->resolveRelationships) {
$this->silent(fn () => $this->updateDocumentRelationships($collection, $document, $new));
}

$document = $new;

// Check if document was updated after the request timestamp
Expand All @@ -4441,7 +4438,14 @@ public function updateDocuments(
$document = $this->encode($collection, $document);
}

$this->withTransaction(function () use ($collection, $updates, $batch) {
$this->withTransaction(function () use ($collection, $updates, &$batch) {
foreach ($batch as $i => &$document) {
if ($this->resolveRelationships) {
$document = $this->silent(fn () => $this->updateDocumentRelationships($collection, $document, $document));
}
}
// deleting the reference of the last document
unset($document);
$this->adapter->updateDocuments(
$collection->getId(),
$updates,
Expand All @@ -4452,7 +4456,11 @@ public function updateDocuments(
foreach ($batch as $doc) {
$this->purgeCachedDocument($collection->getId(), $doc->getId());
$doc = $this->decode($collection, $doc);
$onNext && $onNext($doc);
try {
$onNext && $onNext($doc);
} catch (Exception $e) {
Comment thread
ArnabChatterjee20k marked this conversation as resolved.
Outdated
$onError && $onError($e);
Comment thread
ArnabChatterjee20k marked this conversation as resolved.
Outdated
}
$modified++;
}

Expand Down
4 changes: 3 additions & 1 deletion src/Database/Mirror.php
Original file line number Diff line number Diff line change
Expand Up @@ -713,6 +713,7 @@ public function updateDocuments(
array $queries = [],
int $batchSize = self::INSERT_BATCH_SIZE,
?callable $onNext = null,
?callable $onError = null,
): int {
$modified = 0;

Expand All @@ -724,7 +725,8 @@ public function updateDocuments(
function ($doc) use ($onNext, &$modified) {
$onNext && $onNext($doc);
$modified++;
}
},
$onError
);

if (
Expand Down
19 changes: 18 additions & 1 deletion tests/e2e/Adapter/Scopes/DocumentTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -3556,6 +3556,14 @@ public function testUpdateDocuments(): void
Query::greaterThanEqual('integer', 5),
], onNext: function ($doc) use (&$results) {
$results[] = $doc;
throw new Exception("Error thrown to test update doesn't stopped and error is caught");
}, onError:function ($e) {
if ($e instanceof Exception) {
$this->assertInstanceOf(Exception::class, $e);
$this->assertEquals("Error thrown to test update doesn't stopped and error is caught", $e->getMessage());
} else {
$this->fail("Caught value is not an Exception.");
}
});

$this->assertEquals(5, $count);
Expand Down Expand Up @@ -3588,7 +3596,16 @@ public function testUpdateDocuments(): void
// Test Update all documents
$this->assertEquals(10, $database->updateDocuments($collection, new Document([
'string' => 'text📝 updated all',
])));
]), onNext: function () {
throw new DatabaseException("Error thrown to test update doesn't stopped and error is caught");
}, onError:function ($e) {
if ($e instanceof DatabaseException) {
$this->assertInstanceOf(DatabaseException::class, $e);
$this->assertEquals("Error thrown to test update doesn't stopped and error is caught", $e->getMessage());
} else {
$this->fail("Caught value is not an Exception.");
}
}));
Comment thread
ArnabChatterjee20k marked this conversation as resolved.
Outdated

$updatedDocuments = $database->find($collection);

Expand Down
33 changes: 30 additions & 3 deletions tests/e2e/Adapter/Scopes/RelationshipTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -1791,7 +1791,16 @@ public function testUpdateDocumentsRelationships(): void

$this->getDatabase()->updateDocuments('testUpdateDocumentsRelationships1', new Document([
'string' => 'text📝 updated',
]));
]), onNext: function () {
throw new Exception("Error thrown to test update doesn't stopped and error is caught");
}, onError:function ($e) {
if ($e instanceof Exception) {
$this->assertInstanceOf(Exception::class, $e);
$this->assertEquals("Error thrown to test update doesn't stopped and error is caught", $e->getMessage());
} else {
$this->fail("Caught value is not an Exception.");
}
});

$document = $this->getDatabase()->findOne('testUpdateDocumentsRelationships1');

Expand Down Expand Up @@ -1829,11 +1838,29 @@ public function testUpdateDocumentsRelationships(): void

$this->getDatabase()->updateDocuments('testUpdateDocumentsRelationships2', new Document([
'testUpdateDocumentsRelationships1' => null
]));
]), onNext: function () {
throw new Exception("Error thrown to test update doesn't stopped and error is caught");
}, onError:function ($e) {
if ($e instanceof Exception) {
$this->assertInstanceOf(Exception::class, $e);
$this->assertEquals("Error thrown to test update doesn't stopped and error is caught", $e->getMessage());
} else {
$this->fail("Caught value is not an Exception.");
}
});

$this->getDatabase()->updateDocuments('testUpdateDocumentsRelationships2', new Document([
'testUpdateDocumentsRelationships1' => 'doc1'
]));
]), onNext: function () {
throw new Exception("Error thrown to test update doesn't stopped and error is caught");
}, onError:function ($e) {
if ($e instanceof Exception) {
$this->assertInstanceOf(Exception::class, $e);
$this->assertEquals("Error thrown to test update doesn't stopped and error is caught", $e->getMessage());
} else {
$this->fail("Caught value is not an Exception.");
}
});

$documents = $this->getDatabase()->find('testUpdateDocumentsRelationships2');

Expand Down