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
46 changes: 23 additions & 23 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 22 additions & 10 deletions src/Database/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -2667,14 +2667,15 @@ private function populateDocumentRelationships(Document $collection, Document $d
*
* @param string $collection
* @param Document $document
* @param bool $preserveDates If true, createdAt and updatedAt will not be overwritten
*
* @return Document
*
* @throws AuthorizationException
* @throws DatabaseException
* @throws StructureException
*/
public function createDocument(string $collection, Document $document): Document
public function createDocument(string $collection, Document $document, bool $preserveDates = false): Document
{
$collection = $this->silent(fn () => $this->getCollection($collection));

Expand All @@ -2687,11 +2688,14 @@ public function createDocument(string $collection, Document $document): Document

$time = DateTime::now();

$createdAt = $document->getCreatedAt();
$updatedAt = $document->getUpdatedAt();

$document
->setAttribute('$id', empty($document->getId()) ? ID::unique() : $document->getId())
->setAttribute('$collection', $collection->getId())
->setAttribute('$createdAt', $time)
->setAttribute('$updatedAt', $time);
->setAttribute('$createdAt', empty($createdAt) || !$preserveDates ? $time : $createdAt)
->setAttribute('$updatedAt', empty($updatedAt) || !$preserveDates ? $time : $updatedAt);

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

Expand Down Expand Up @@ -2728,14 +2732,15 @@ public function createDocument(string $collection, Document $document): Document
* @param string $collection
* @param array<Document> $documents
* @param int $batchSize
* @param bool $preserveDates If true, createdAt and updatedAt will not be overwritten
*
* @return array<Document>
*
* @throws AuthorizationException
* @throws StructureException
* @throws Exception
*/
public function createDocuments(string $collection, array $documents, int $batchSize = self::INSERT_BATCH_SIZE): array
public function createDocuments(string $collection, array $documents, int $batchSize = self::INSERT_BATCH_SIZE, bool $preserveDates = false): array
{
if (empty($documents)) {
return [];
Expand All @@ -2746,11 +2751,14 @@ public function createDocuments(string $collection, array $documents, int $batch
$time = DateTime::now();

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

$document
->setAttribute('$id', empty($document->getId()) ? ID::unique() : $document->getId())
->setAttribute('$collection', $collection->getId())
->setAttribute('$createdAt', $time)
->setAttribute('$updatedAt', $time);
->setAttribute('$createdAt', empty($createdAt) || !$preserveDates ? $time : $createdAt)
->setAttribute('$updatedAt', empty($updatedAt) || !$preserveDates ? $time : $updatedAt);

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

Expand Down Expand Up @@ -3055,14 +3063,15 @@ private function relateDocumentsById(
* @param string $collection
* @param string $id
* @param Document $document
* @param bool $preserveDates If true, updatedAt will not be overwritten
* @return Document
*
* @throws AuthorizationException
* @throws ConflictException
* @throws DatabaseException
* @throws StructureException
*/
public function updateDocument(string $collection, string $id, Document $document): Document
public function updateDocument(string $collection, string $id, Document $document, bool $preserveDates = false): Document
{
if (!$document->getId() || !$id) {
throw new DatabaseException('Must define $id attribute');
Expand Down Expand Up @@ -3176,7 +3185,8 @@ public function updateDocument(string $collection, string $id, Document $documen
}

if ($shouldUpdate) {
$document->setAttribute('$updatedAt', $time);
$updatedAt = $document->getUpdatedAt();
$document->setAttribute('$updatedAt', empty($updatedAt) || !$preserveDates ? $time : $updatedAt);
}

// Check if document was updated after the request timestamp
Expand Down Expand Up @@ -3220,14 +3230,15 @@ public function updateDocument(string $collection, string $id, Document $documen
* @param string $collection
* @param array<Document> $documents
* @param int $batchSize
* @param bool $preserveDates If true, updatedAt will not be overwritten
*
* @return array<Document>
*
* @throws AuthorizationException
* @throws Exception
* @throws StructureException
*/
public function updateDocuments(string $collection, array $documents, int $batchSize = self::INSERT_BATCH_SIZE): array
public function updateDocuments(string $collection, array $documents, int $batchSize = self::INSERT_BATCH_SIZE, bool $preserveDates = false): array
{
if (empty($documents)) {
return [];
Expand All @@ -3241,7 +3252,8 @@ public function updateDocuments(string $collection, array $documents, int $batch
throw new Exception('Must define $id attribute for each document');
}

$document->setAttribute('$updatedAt', $time);
$updatedAt = $document->getUpdatedAt();
$document->setAttribute('$updatedAt', empty($updatedAt) || !$preserveDates ? $time : $updatedAt);
$document = $this->encode($collection, $document);

$old = Authorization::skip(fn () => $this->silent(
Expand Down
91 changes: 91 additions & 0 deletions tests/Database/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,97 @@ public function testCreateExistsDelete(): void
$this->assertEquals(true, static::getDatabase()->create());
}

public function testPreserveDatesUpdate(): void
{
Authorization::disable();

static::getDatabase()->createCollection('preserve_update_dates');

static::getDatabase()->createAttribute('preserve_update_dates', 'attr1', Database::VAR_STRING, 10, false);

$doc1 = static::getDatabase()->createDocument('preserve_update_dates', new Document([
'$id' => 'doc1',
'$permissions' => [],
'attr1' => 'value1',
]));

$doc2 = static::getDatabase()->createDocument('preserve_update_dates', new Document([
'$id' => 'doc2',
'$permissions' => [],
'attr1' => 'value2',
]));

$doc3 = static::getDatabase()->createDocument('preserve_update_dates', new Document([
'$id' => 'doc3',
'$permissions' => [],
'attr1' => 'value3',
]));

$newDate = '2000-01-01T10:00:00.000+00:00';

$doc1->setAttribute('$updatedAt', $newDate);
static::getDatabase()->updateDocument('preserve_update_dates', 'doc1', $doc1, true);
$doc1 = static::getDatabase()->getDocument('preserve_update_dates', 'doc1');
$this->assertEquals($newDate, $doc1->getAttribute('$updatedAt'));

$doc2->setAttribute('$updatedAt', $newDate);
$doc3->setAttribute('$updatedAt', $newDate);
static::getDatabase()->updateDocuments('preserve_update_dates', [$doc2, $doc3], 2, true);

$doc2 = static::getDatabase()->getDocument('preserve_update_dates', 'doc2');
$doc3 = static::getDatabase()->getDocument('preserve_update_dates', 'doc3');
$this->assertEquals($newDate, $doc2->getAttribute('$updatedAt'));
$this->assertEquals($newDate, $doc3->getAttribute('$updatedAt'));

static::getDatabase()->deleteCollection('preserve_update_dates');

Authorization::reset();
}

public function testPreserveDatesCreate(): void
{
Authorization::disable();

static::getDatabase()->createCollection('preserve_create_dates');

static::getDatabase()->createAttribute('preserve_create_dates', 'attr1', Database::VAR_STRING, 10, false);

$date = '2000-01-01T10:00:00.000+00:00';

static::getDatabase()->createDocument('preserve_create_dates', new Document([
'$id' => 'doc1',
'$permissions' => [],
'attr1' => 'value1',
'$createdAt' => $date
]), true);

static::getDatabase()->createDocuments('preserve_create_dates', [
new Document([
'$id' => 'doc2',
'$permissions' => [],
'attr1' => 'value2',
'$createdAt' => $date
]),
new Document([
'$id' => 'doc3',
'$permissions' => [],
'attr1' => 'value3',
'$createdAt' => $date
]),
], 2, true);

$doc1 = static::getDatabase()->getDocument('preserve_create_dates', 'doc1');
$doc2 = static::getDatabase()->getDocument('preserve_create_dates', 'doc2');
$doc3 = static::getDatabase()->getDocument('preserve_create_dates', 'doc3');
$this->assertEquals($date, $doc1->getAttribute('$createdAt'));
$this->assertEquals($date, $doc2->getAttribute('$createdAt'));
$this->assertEquals($date, $doc3->getAttribute('$createdAt'));

static::getDatabase()->deleteCollection('preserve_create_dates');

Authorization::reset();
}

/**
* @throws Exception|Throwable
*/
Expand Down