Skip to content

Commit 1fbdacc

Browse files
Merge remote-tracking branch 'upstream/main' into dat-610-fix-createDocuments-attributes
2 parents c864ead + 4fc1fb3 commit 1fbdacc

File tree

4 files changed

+165
-2
lines changed

4 files changed

+165
-2
lines changed

src/Database/Database.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5218,7 +5218,7 @@ public function increaseDocumentAttribute(
52185218
int|float|null $max = null
52195219
): Document {
52205220
if ($value <= 0) { // Can be a float
5221-
throw new DatabaseException('Value must be numeric and greater than 0');
5221+
throw new \InvalidArgumentException('Value must be numeric and greater than 0');
52225222
}
52235223

52245224
$collection = $this->silent(fn () => $this->getCollection($collection));
@@ -5315,7 +5315,7 @@ public function decreaseDocumentAttribute(
53155315
int|float|null $min = null
53165316
): Document {
53175317
if ($value <= 0) { // Can be a float
5318-
throw new DatabaseException('Value must be numeric and greater than 0');
5318+
throw new \InvalidArgumentException('Value must be numeric and greater than 0');
53195319
}
53205320

53215321
$collection = $this->silent(fn () => $this->getCollection($collection));

src/Database/Mirror.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -781,6 +781,75 @@ function ($doc) use ($onNext, &$modified) {
781781
return $modified;
782782
}
783783

784+
public function createOrUpdateDocuments(string $collection, array $documents, int $batchSize = Database::INSERT_BATCH_SIZE, callable|null $onNext = null): int
785+
{
786+
$modified = 0;
787+
$this->source->createOrUpdateDocuments(
788+
$collection,
789+
$documents,
790+
$batchSize,
791+
function ($doc) use ($onNext, &$modified) {
792+
$onNext && $onNext($doc);
793+
$modified++;
794+
}
795+
);
796+
797+
if (
798+
\in_array($collection, self::SOURCE_ONLY_COLLECTIONS)
799+
|| $this->destination === null
800+
) {
801+
return $modified;
802+
}
803+
804+
$upgrade = $this->silent(fn () => $this->getUpgradeStatus($collection));
805+
if ($upgrade === null || $upgrade->getAttribute('status', '') !== 'upgraded') {
806+
return $modified;
807+
}
808+
809+
try {
810+
$clones = [];
811+
812+
foreach ($documents as $document) {
813+
$clone = clone $document;
814+
815+
foreach ($this->writeFilters as $filter) {
816+
$clone = $filter->beforeCreateOrUpdateDocument(
817+
source: $this->source,
818+
destination: $this->destination,
819+
collectionId: $collection,
820+
document: $clone,
821+
);
822+
}
823+
824+
$clones[] = $clone;
825+
}
826+
827+
$modified = $this->destination->withPreserveDates(
828+
fn () =>
829+
$this->destination->createOrUpdateDocuments(
830+
$collection,
831+
$clones,
832+
$batchSize,
833+
null,
834+
)
835+
);
836+
837+
foreach ($clones as $clone) {
838+
foreach ($this->writeFilters as $filter) {
839+
$filter->afterCreateOrUpdateDocument(
840+
source: $this->source,
841+
destination: $this->destination,
842+
collectionId: $collection,
843+
document: $clone,
844+
);
845+
}
846+
}
847+
} catch (\Throwable $err) {
848+
$this->logError('createDocuments', $err);
849+
}
850+
return $modified;
851+
}
852+
784853
public function deleteDocument(string $collection, string $id): bool
785854
{
786855
$result = $this->source->deleteDocument($collection, $id);

src/Database/Mirroring/Filter.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,4 +359,40 @@ public function afterDeleteDocuments(
359359
array $queries
360360
): void {
361361
}
362+
363+
/**
364+
* Called before document is upserted in the destination database
365+
*
366+
* @param Database $source
367+
* @param Database $destination
368+
* @param string $collectionId
369+
* @param Document $document
370+
* @return Document
371+
*/
372+
public function beforeCreateOrUpdateDocument(
373+
Database $source,
374+
Database $destination,
375+
string $collectionId,
376+
Document $document,
377+
): Document {
378+
return $document;
379+
}
380+
381+
/**
382+
* Called after document is upserted in the destination database
383+
*
384+
* @param Database $source
385+
* @param Database $destination
386+
* @param string $collectionId
387+
* @param Document $document
388+
* @return Document
389+
*/
390+
public function afterCreateOrUpdateDocument(
391+
Database $source,
392+
Database $destination,
393+
string $collectionId,
394+
Document $document,
395+
): Document {
396+
return $document;
397+
}
362398
}

tests/e2e/Adapter/Scopes/DocumentTests.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5820,4 +5820,62 @@ public function testUpsertDateOperations(): void
58205820
$database->setPreserveDates(false);
58215821
$database->deleteCollection($collection);
58225822
}
5823+
5824+
public function testUpdateDocumentsCount(): void
5825+
{
5826+
/** @var Database $database */
5827+
$database = static::getDatabase();
5828+
5829+
if (!$database->getAdapter()->getSupportForUpserts()) {
5830+
return;
5831+
}
5832+
5833+
$collectionName = "update_count";
5834+
$database->createCollection($collectionName);
5835+
5836+
$database->createAttribute($collectionName, 'key', Database::VAR_STRING, 60, false);
5837+
$database->createAttribute($collectionName, 'value', Database::VAR_STRING, 60, false);
5838+
5839+
$permissions = [Permission::read(Role::any()), Permission::write(Role::any()),Permission::update(Role::any())];
5840+
5841+
$docs = [
5842+
new Document([
5843+
'$id' => 'bulk_upsert1',
5844+
'$permissions' => $permissions,
5845+
'key' => 'bulk_upsert1_initial',
5846+
]),
5847+
new Document([
5848+
'$id' => 'bulk_upsert2',
5849+
'$permissions' => $permissions,
5850+
'key' => 'bulk_upsert2_initial',
5851+
]),
5852+
new Document([
5853+
'$id' => 'bulk_upsert3',
5854+
'$permissions' => $permissions,
5855+
'key' => 'bulk_upsert3_initial',
5856+
]),
5857+
new Document([
5858+
'$id' => 'bulk_upsert4',
5859+
'$permissions' => $permissions,
5860+
'key' => 'bulk_upsert4_initial'
5861+
])
5862+
];
5863+
$upsertUpdateResults = [];
5864+
$count = $database->createOrUpdateDocuments($collectionName, $docs, onNext: function ($doc) use (&$upsertUpdateResults) {
5865+
$upsertUpdateResults[] = $doc;
5866+
});
5867+
$this->assertCount(4, $upsertUpdateResults);
5868+
$this->assertEquals(4, $count);
5869+
5870+
$updates = new Document(['value' => 'test']);
5871+
$newDocs = [];
5872+
$count = $database->updateDocuments($collectionName, $updates, onNext:function ($doc) use (&$newDocs) {
5873+
$newDocs[] = $doc;
5874+
});
5875+
5876+
$this->assertCount(4, $newDocs);
5877+
$this->assertEquals(4, $count);
5878+
5879+
$database->deleteCollection($collectionName);
5880+
}
58235881
}

0 commit comments

Comments
 (0)