Skip to content

Commit 40f72f9

Browse files
authored
Merge pull request #562 from utopia-php/fix-bulk-encoding
Fix bulk encoding
2 parents 944d197 + 95cb26e commit 40f72f9

File tree

2 files changed

+26
-19
lines changed

2 files changed

+26
-19
lines changed

src/Database/Database.php

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3488,7 +3488,7 @@ public function createDocuments(
34883488
$time = DateTime::now();
34893489
$modified = 0;
34903490

3491-
foreach ($documents as &$document) {
3491+
foreach ($documents as $document) {
34923492
$createdAt = $document->getCreatedAt();
34933493
$updatedAt = $document->getUpdatedAt();
34943494

@@ -3529,11 +3529,13 @@ public function createDocuments(
35293529
return $this->adapter->createDocuments($collection->getId(), $chunk);
35303530
});
35313531

3532-
foreach ($batch as $doc) {
3532+
foreach ($batch as $document) {
35333533
if ($this->resolveRelationships) {
3534-
$doc = $this->silent(fn () => $this->populateDocumentRelationships($collection, $doc));
3534+
$document = $this->silent(fn () => $this->populateDocumentRelationships($collection, $document));
35353535
}
3536-
$onNext && $onNext($doc);
3536+
3537+
$document = $this->decode($collection, $document);
3538+
$onNext && $onNext($document);
35373539
$modified++;
35383540
}
35393541
}
@@ -4142,6 +4144,10 @@ public function updateDocuments(
41424144
unset($updates['$createdAt']);
41434145
unset($updates['$tenant']);
41444146

4147+
if ($this->adapter->getSharedTables()) {
4148+
$updates['$tenant'] = $this->adapter->getTenant();
4149+
}
4150+
41454151
if (!$this->preserveDates) {
41464152
$updates['$updatedAt'] = DateTime::now();
41474153
}
@@ -4163,7 +4169,6 @@ public function updateDocuments(
41634169
$last = $cursor;
41644170
$modified = 0;
41654171

4166-
// Resolve and update relationships
41674172
while (true) {
41684173
if ($limit && $limit < $batchSize) {
41694174
$batchSize = $limit;
@@ -4190,12 +4195,14 @@ public function updateDocuments(
41904195
}
41914196

41924197
foreach ($batch as &$document) {
4198+
$new = new Document(\array_merge($document->getArrayCopy(), $updates->getArrayCopy()));
4199+
41934200
if ($this->resolveRelationships) {
4194-
$newDocument = new Document(array_merge($document->getArrayCopy(), $updates->getArrayCopy()));
4195-
$this->silent(fn () => $this->updateDocumentRelationships($collection, $document, $newDocument));
4196-
$document = $newDocument;
4201+
$this->silent(fn () => $this->updateDocumentRelationships($collection, $document, $new));
41974202
}
41984203

4204+
$document = $new;
4205+
41994206
// Check if document was updated after the request timestamp
42004207
try {
42014208
$oldUpdatedAt = new \DateTime($document->getUpdatedAt());
@@ -4206,6 +4213,8 @@ public function updateDocuments(
42064213
if (!is_null($this->timestamp) && $oldUpdatedAt > $this->timestamp) {
42074214
throw new ConflictException('Document was updated after the request timestamp');
42084215
}
4216+
4217+
$document = $this->encode($collection, $document);
42094218
}
42104219

42114220
$this->withTransaction(function () use ($collection, $updates, $batch) {
@@ -4217,14 +4226,8 @@ public function updateDocuments(
42174226
});
42184227

42194228
foreach ($batch as $doc) {
4220-
if ($this->getSharedTables() && $this->getTenantPerDocument()) {
4221-
$this->withTenant($doc->getTenant(), function () use ($collection, $doc) {
4222-
$this->purgeCachedDocument($collection->getId(), $doc->getId());
4223-
});
4224-
} else {
4225-
$this->purgeCachedDocument($collection->getId(), $doc->getId());
4226-
}
4227-
4229+
$this->purgeCachedDocument($collection->getId(), $doc->getId());
4230+
$doc = $this->decode($collection, $doc);
42284231
$onNext && $onNext($doc);
42294232
$modified++;
42304233
}
@@ -6105,7 +6108,7 @@ public function decode(Document $collection, Document $document, array $selectio
61056108
}
61066109
}
61076110

6108-
$attributes = array_merge($attributes, $this->getInternalAttributes());
6111+
$attributes = \array_merge($attributes, $this->getInternalAttributes());
61096112

61106113
foreach ($attributes as $attribute) {
61116114
$key = $attribute['$id'] ?? '';
@@ -6125,7 +6128,7 @@ public function decode(Document $collection, Document $document, array $selectio
61256128
$value = (is_null($value)) ? [] : $value;
61266129

61276130
foreach ($value as &$node) {
6128-
foreach (array_reverse($filters) as $filter) {
6131+
foreach (\array_reverse($filters) as $filter) {
61296132
$node = $this->decodeAttribute($filter, $node, $document);
61306133
}
61316134
}

tests/e2e/Adapter/Base.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2289,13 +2289,15 @@ public function testCreateDocuments(): array
22892289
$this->assertEquals($count, \count($documents));
22902290

22912291
foreach ($documents as $document) {
2292+
$fresh = static::getDatabase()->getDocument($collection, $document->getId());
2293+
$this->assertEquals($document, $fresh);
22922294
$this->assertNotEmpty(true, $document->getId());
22932295
$this->assertIsString($document->getAttribute('string'));
22942296
$this->assertEquals('text📝', $document->getAttribute('string')); // Also makes sure an emoji is working
22952297
$this->assertIsInt($document->getAttribute('integer'));
22962298
$this->assertEquals(5, $document->getAttribute('integer'));
22972299
$this->assertIsInt($document->getAttribute('bigint'));
2298-
$this->assertEquals(9223372036854775807, $document->getAttribute('bigint'));
2300+
$this->assertEquals(Database::BIG_INT_MAX, $document->getAttribute('bigint'));
22992301
}
23002302

23012303
return $documents;
@@ -17600,6 +17602,8 @@ public function testUpdateDocuments(): void
1760017602
$this->assertEquals(5, $count);
1760117603

1760217604
foreach ($results as $document) {
17605+
$fresh = static::getDatabase()->getDocument($collection, $document->getId());
17606+
$this->assertEquals($fresh, $document);
1760317607
$this->assertEquals('text📝 updated', $document->getAttribute('string'));
1760417608
}
1760517609

0 commit comments

Comments
 (0)