Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
23 changes: 15 additions & 8 deletions src/Database/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -3979,10 +3979,11 @@ public function updateDocuments(string $collection, Document $updates, array $qu
throw new StructureException($validator->getDescription());
}

$affected = $this->withTransaction(function () use ($collection, $queries, $batchSize, $updates, $limit, $cursor) {
$affectedDocumentIds = [];

$affected = $this->withTransaction(function () use ($collection, $queries, $batchSize, $updates, $limit, $cursor, $affectedDocumentIds) {
$lastDocument = null;
$totalModified = 0;
$affectedDocumentIds = [];

$documentSecurity = $collection->getAttribute('documentSecurity', false);

Expand All @@ -4004,15 +4005,15 @@ public function updateDocuments(string $collection, Document $updates, array $qu
$limit -= $batchSize;
}

$affectedDocuments = $this->find($collection->getId(), array_merge(
$affectedDocuments = $this->silent(fn () => $this->find($collection->getId(), array_merge(
$queries,
empty($lastDocument) ? [
Query::limit($batchSize),
] : [
Query::limit($batchSize),
Query::cursorAfter($lastDocument),
]
), forPermission: Database::PERMISSION_UPDATE);
), forPermission: Database::PERMISSION_UPDATE));

if (empty($affectedDocuments)) {
break;
Expand Down Expand Up @@ -4047,7 +4048,10 @@ public function updateDocuments(string $collection, Document $updates, array $qu
$lastDocument = end($affectedDocuments);
}

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

foreach ($affectedDocumentIds as $id) {
$this->purgeRelatedDocuments($collection, $id);
Expand Down Expand Up @@ -5156,15 +5160,15 @@ public function deleteDocuments(string $collection, array $queries = [], int $ba
$limit -= $batchSize;
}

$affectedDocuments = $this->find($collection->getId(), array_merge(
$affectedDocuments = $this->silent(fn () => $this->find($collection->getId(), array_merge(
$queries,
empty($lastDocument) ? [
Query::limit($batchSize),
] : [
Query::limit($batchSize),
Query::cursorAfter($lastDocument),
]
), forPermission: Database::PERMISSION_DELETE);
), forPermission: Database::PERMISSION_DELETE));

if (empty($affectedDocuments)) {
break;
Expand Down Expand Up @@ -5195,7 +5199,10 @@ public function deleteDocuments(string $collection, array $queries = [], int $ba
return 0;
}

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

// Mass delete using adapter with query
return $this->adapter->deleteDocuments($collection->getId(), $affectedDocumentIds);
Expand Down
19 changes: 18 additions & 1 deletion tests/e2e/Adapter/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -16922,11 +16922,14 @@ public function testEvents(): void
Database::EVENT_DOCUMENT_SUM,
Database::EVENT_DOCUMENT_INCREASE,
Database::EVENT_DOCUMENT_DECREASE,
Database::EVENT_DOCUMENTS_CREATE,
Database::EVENT_DOCUMENTS_UPDATE,
Database::EVENT_INDEX_DELETE,
Database::EVENT_DOCUMENT_DELETE,
Database::EVENT_DOCUMENTS_DELETE,
Database::EVENT_ATTRIBUTE_DELETE,
Database::EVENT_COLLECTION_DELETE,
Database::EVENT_DATABASE_DELETE,
Database::EVENT_DATABASE_DELETE
];

$database->on(Database::EVENT_ALL, 'test', function ($event, $data) use (&$events) {
Expand Down Expand Up @@ -16983,8 +16986,22 @@ public function testEvents(): void

$this->assertFalse($executed);

$database->createDocuments($collectionId, [
new Document([
'attr1' => 10,
]),
new Document([
'attr1' => 20,
]),
]);

$database->updateDocuments($collectionId, new Document([
'attr1' => 15,
]));

$database->deleteIndex($collectionId, $indexId1);
$database->deleteDocument($collectionId, 'doc1');
$database->deleteDocuments($collectionId);
$database->deleteAttribute($collectionId, 'attr1');
$database->deleteCollection($collectionId);
$database->delete('hellodb');
Expand Down