Skip to content

Commit 0a83108

Browse files
committed
Add change class for upsert diffs
1 parent 97cda21 commit 0a83108

5 files changed

Lines changed: 59 additions & 18 deletions

File tree

src/Database/Adapter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -693,7 +693,7 @@ abstract public function updateDocuments(string $collection, Document $updates,
693693
*
694694
* @param string $collection
695695
* @param string $attribute
696-
* @param array<string|int, array{old: Document, new: Document}> $documents
696+
* @param array<Change> $documents
697697
* @return array<Document>
698698
*/
699699
abstract public function createOrUpdateDocuments(

src/Database/Adapter/MariaDB.php

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Exception;
66
use PDO;
77
use PDOException;
8+
use Utopia\Database\Change;
89
use Utopia\Database\Database;
910
use Utopia\Database\Document;
1011
use Utopia\Database\Exception as DatabaseException;
@@ -1333,7 +1334,7 @@ public function updateDocument(string $collection, string $id, Document $documen
13331334
/**
13341335
* @param string $collection
13351336
* @param string $attribute
1336-
* @param array<string|int, array{old: Document, new: Document}> $documents
1337+
* @param array<Change> $documents
13371338
* @return array<Document>
13381339
* @throws DatabaseException
13391340
*/
@@ -1357,8 +1358,8 @@ public function createOrUpdateDocuments(
13571358
$documentIds = [];
13581359
$documentTenants = [];
13591360

1360-
foreach ($documents as $document) {
1361-
$document = $document['new'];
1361+
foreach ($documents as $change) {
1362+
$document = $change->getNew();
13621363
$attributes = $document->getAttributes();
13631364
$attributes['_uid'] = $document->getId();
13641365
$attributes['_createdAt'] = $document->getCreatedAt();
@@ -1455,9 +1456,9 @@ public function createOrUpdateDocuments(
14551456
$addQueries = [];
14561457
$addBindValues = [];
14571458

1458-
foreach ($documents as $index => $document) {
1459-
$old = $document['old'];
1460-
$document = $document['new'];
1459+
foreach ($documents as $index => $change) {
1460+
$old = $change->getOld();
1461+
$document = $change->getNew();
14611462

14621463
$current = [];
14631464
foreach (Database::PERMISSIONS as $type) {
@@ -1530,16 +1531,17 @@ public function createOrUpdateDocuments(
15301531
}
15311532

15321533
$internalIds = $this->getInternalIds($collection, $documentIds, $documentTenants);
1534+
15331535
foreach ($documents as $document) {
1534-
if (isset($internalIds[$document['new']->getId()])) {
1535-
$document['new']['$internalId'] = $internalIds[$document['new']->getId()];
1536+
if (isset($internalIds[$document->getNew()->getId()])) {
1537+
$document->getNew()->setAttribute('$internalId', $internalIds[$document->getNew()->getId()]);
15361538
}
15371539
}
15381540
} catch (PDOException $e) {
15391541
throw $this->processException($e);
15401542
}
15411543

1542-
return \array_map(fn ($document) => $document['new'], $documents);
1544+
return \array_map(fn ($change) => $change->getNew(), $documents);
15431545
}
15441546

15451547
/**

src/Database/Adapter/Postgres.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Exception;
66
use PDO;
77
use PDOException;
8+
use Utopia\Database\Change;
89
use Utopia\Database\Database;
910
use Utopia\Database\Document;
1011
use Utopia\Database\Exception as DatabaseException;
@@ -1381,12 +1382,12 @@ public function updateDocument(string $collection, string $id, Document $documen
13811382
/**
13821383
* @param string $collection
13831384
* @param string $attribute
1384-
* @param array<array{old: Document, new: Document}> $documents
1385+
* @param array<Change> $documents
13851386
* @return array<Document>
13861387
*/
13871388
public function createOrUpdateDocuments(string $collection, string $attribute, array $documents): array
13881389
{
1389-
return $documents;
1390+
return \array_map(fn ($change) => $change->getNew(), $documents);
13901391
}
13911392

13921393
/**

src/Database/Change.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace Utopia\Database;
4+
5+
class Change
6+
{
7+
public function __construct(
8+
protected Document $old,
9+
protected Document $new,
10+
) {
11+
}
12+
13+
public function getOld(): Document
14+
{
15+
return $this->old;
16+
}
17+
18+
public function setOld(Document $old): void
19+
{
20+
$this->old = $old;
21+
}
22+
23+
public function getNew(): Document
24+
{
25+
return $this->new;
26+
}
27+
28+
public function setNew(Document $new): void
29+
{
30+
$this->new = $new;
31+
}
32+
}

src/Database/Database.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4742,21 +4742,21 @@ public function createOrUpdateDocumentsWithIncrease(
47424742
$document = $this->silent(fn () => $this->createDocumentRelationships($collection, $document));
47434743
}
47444744

4745-
$documents[$key] = [
4746-
'old' => $old,
4747-
'new' => $document
4748-
];
4745+
$documents[$key] = new Change(
4746+
old: $old,
4747+
new: $document
4748+
);
47494749
}
47504750

47514751
/**
4752-
* @var array<int|string, array{old: Document, new: Document}> $documents
4752+
* @var array<Change> $documents
47534753
*/
47544754
$documents = $this->withTransaction(function () use ($collection, $attribute, $documents, $batchSize) {
47554755
$stack = [];
47564756

47574757
foreach (\array_chunk($documents, $batchSize) as $chunk) {
47584758
/**
4759-
* @var array<int|string, array{old: Document, new: Document}> $chunk
4759+
* @var array<Change> $chunk
47604760
*/
47614761
\array_push(
47624762
$stack,
@@ -4771,7 +4771,13 @@ public function createOrUpdateDocumentsWithIncrease(
47714771
return $stack;
47724772
});
47734773

4774+
/**
4775+
* @var array<Document> $documents
4776+
*/
47744777
foreach ($documents as $key => $document) {
4778+
/**
4779+
* @var Document $document
4780+
*/
47754781
if ($this->resolveRelationships) {
47764782
$document = $this->silent(fn () => $this->populateDocumentRelationships($collection, $document));
47774783
}

0 commit comments

Comments
 (0)