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
39 changes: 21 additions & 18 deletions src/Database/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -3488,7 +3488,7 @@ public function createDocuments(
$time = DateTime::now();
$modified = 0;

foreach ($documents as &$document) {
foreach ($documents as $document) {
$createdAt = $document->getCreatedAt();
$updatedAt = $document->getUpdatedAt();

Expand Down Expand Up @@ -3529,11 +3529,13 @@ public function createDocuments(
return $this->adapter->createDocuments($collection->getId(), $chunk);
});

foreach ($batch as $doc) {
foreach ($batch as $document) {
if ($this->resolveRelationships) {
$doc = $this->silent(fn () => $this->populateDocumentRelationships($collection, $doc));
$document = $this->silent(fn () => $this->populateDocumentRelationships($collection, $document));
}
$onNext && $onNext($doc);

$document = $this->decode($collection, $document);
$onNext && $onNext($document);
$modified++;
}
}
Expand Down Expand Up @@ -4142,6 +4144,10 @@ public function updateDocuments(
unset($updates['$createdAt']);
unset($updates['$tenant']);

if ($this->adapter->getSharedTables()) {
$updates['$tenant'] = $this->adapter->getTenant();
}

if (!$this->preserveDates) {
$updates['$updatedAt'] = DateTime::now();
}
Expand All @@ -4163,7 +4169,6 @@ public function updateDocuments(
$last = $cursor;
$modified = 0;

// Resolve and update relationships
while (true) {
if ($limit && $limit < $batchSize) {
$batchSize = $limit;
Expand All @@ -4190,12 +4195,14 @@ public function updateDocuments(
}

foreach ($batch as &$document) {
$new = new Document(\array_merge($document->getArrayCopy(), $updates->getArrayCopy()));

if ($this->resolveRelationships) {
$newDocument = new Document(array_merge($document->getArrayCopy(), $updates->getArrayCopy()));
$this->silent(fn () => $this->updateDocumentRelationships($collection, $document, $newDocument));
$document = $newDocument;
$this->silent(fn () => $this->updateDocumentRelationships($collection, $document, $new));
}

$document = $new;

// Check if document was updated after the request timestamp
try {
$oldUpdatedAt = new \DateTime($document->getUpdatedAt());
Expand All @@ -4206,6 +4213,8 @@ public function updateDocuments(
if (!is_null($this->timestamp) && $oldUpdatedAt > $this->timestamp) {
throw new ConflictException('Document was updated after the request timestamp');
}

$document = $this->encode($collection, $document);
}

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

foreach ($batch as $doc) {
if ($this->getSharedTables() && $this->getTenantPerDocument()) {
$this->withTenant($doc->getTenant(), function () use ($collection, $doc) {
$this->purgeCachedDocument($collection->getId(), $doc->getId());
});
} else {
$this->purgeCachedDocument($collection->getId(), $doc->getId());
}

$this->purgeCachedDocument($collection->getId(), $doc->getId());
$doc = $this->decode($collection, $doc);
$onNext && $onNext($doc);
$modified++;
}
Expand Down Expand Up @@ -6105,7 +6108,7 @@ public function decode(Document $collection, Document $document, array $selectio
}
}

$attributes = array_merge($attributes, $this->getInternalAttributes());
$attributes = \array_merge($attributes, $this->getInternalAttributes());

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

foreach ($value as &$node) {
foreach (array_reverse($filters) as $filter) {
foreach (\array_reverse($filters) as $filter) {
$node = $this->decodeAttribute($filter, $node, $document);
}
}
Expand Down
6 changes: 5 additions & 1 deletion tests/e2e/Adapter/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -2289,13 +2289,15 @@ public function testCreateDocuments(): array
$this->assertEquals($count, \count($documents));

foreach ($documents as $document) {
$fresh = static::getDatabase()->getDocument($collection, $document->getId());
$this->assertEquals($document, $fresh);
$this->assertNotEmpty(true, $document->getId());
$this->assertIsString($document->getAttribute('string'));
$this->assertEquals('text📝', $document->getAttribute('string')); // Also makes sure an emoji is working
$this->assertIsInt($document->getAttribute('integer'));
$this->assertEquals(5, $document->getAttribute('integer'));
$this->assertIsInt($document->getAttribute('bigint'));
$this->assertEquals(9223372036854775807, $document->getAttribute('bigint'));
$this->assertEquals(Database::BIG_INT_MAX, $document->getAttribute('bigint'));
}

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

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

Expand Down