Skip to content

Commit d63dcbd

Browse files
allow modification of created at and updated at in update document and update documents
1 parent b81f27a commit d63dcbd

File tree

4 files changed

+305
-46
lines changed

4 files changed

+305
-46
lines changed

src/Database/Database.php

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4119,10 +4119,11 @@ public function updateDocument(string $collection, string $id, Document $documen
41194119

41204120
$skipPermissionsUpdate = ($originalPermissions === $currentPermissions);
41214121
}
4122+
$createdAt = $document->getCreatedAt();
41224123

41234124
$document = \array_merge($old->getArrayCopy(), $document->getArrayCopy());
41244125
$document['$collection'] = $old->getAttribute('$collection'); // Make sure user doesn't switch collection ID
4125-
$document['$createdAt'] = $old->getCreatedAt(); // Make sure user doesn't switch createdAt
4126+
$document['$createdAt'] = ($createdAt === null || !$this->preserveDates) ? $old->getCreatedAt() : $createdAt; // Make sure user doesn't switch createdAt
41264127

41274128
if ($this->adapter->getSharedTables()) {
41284129
$document['$tenant'] = $old->getTenant(); // Make sure user doesn't switch tenant
@@ -4251,7 +4252,7 @@ public function updateDocument(string $collection, string $id, Document $documen
42514252

42524253
if ($shouldUpdate) {
42534254
$updatedAt = $document->getUpdatedAt();
4254-
$document->setAttribute('$updatedAt', empty($updatedAt) || !$this->preserveDates ? $time : $updatedAt);
4255+
$document->setAttribute('$updatedAt', ($updatedAt === null || !$this->preserveDates) ? $time : $updatedAt);
42554256
}
42564257

42574258
// Check if document was updated after the request timestamp
@@ -4365,29 +4366,31 @@ public function updateDocuments(
43654366
if (!empty($cursor) && $cursor->getCollection() !== $collection->getId()) {
43664367
throw new DatabaseException("Cursor document must be from the same Collection.");
43674368
}
4368-
4369+
$attributesToCheckForRequiredValidation = ['$updatedAt'];
43694370
unset($updates['$id']);
4370-
unset($updates['$createdAt']);
43714371
unset($updates['$tenant']);
4372-
4372+
if (($updates->getCreatedAt() === null || !$this->preserveDates)) {
4373+
unset($updates['$createdAt']);
4374+
} else {
4375+
$updates['$createdAt'] = $updates->getCreatedAt();
4376+
$attributesToCheckForRequiredValidation[] = '$createdAt';
4377+
}
43734378
if ($this->adapter->getSharedTables()) {
43744379
$updates['$tenant'] = $this->adapter->getTenant();
43754380
}
43764381

4377-
if (!$this->preserveDates) {
4378-
$updates['$updatedAt'] = DateTime::now();
4379-
}
4382+
$updatedAt = $updates->getUpdatedAt();
4383+
$updates['$updatedAt'] = ($updatedAt === null || !$this->preserveDates) ? DateTime::now() : $updatedAt;
43804384

43814385
$updates = $this->encode($collection, $updates);
4382-
43834386
// Check new document structure
43844387
$validator = new PartialStructure(
43854388
$collection,
43864389
$this->adapter->getMinDateTime(),
43874390
$this->adapter->getMaxDateTime(),
43884391
);
43894392

4390-
if (!$validator->isValid($updates)) {
4393+
if (!$validator->isValid($updates, $attributesToCheckForRequiredValidation)) {
43914394
throw new StructureException($validator->getDescription());
43924395
}
43934396

src/Database/Validator/PartialStructure.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,11 @@ class PartialStructure extends Structure
1313
* Returns true if valid or false if not.
1414
*
1515
* @param mixed $document
16+
* @param array<string, string> $requiredAttributes optional list of required attributes to check
1617
*
1718
* @return bool
1819
*/
19-
public function isValid($document): bool
20+
public function isValid($document, array $requiredAttributes = []): bool
2021
{
2122
if (!$document instanceof Document) {
2223
$this->message = 'Value must be an instance of Document';
@@ -36,7 +37,16 @@ public function isValid($document): bool
3637
$name = $attribute['$id'] ?? '';
3738
$keys[$name] = $attribute;
3839
}
40+
$requiredAttributesMap = [];
41+
foreach ($this->attributes as $attribute) {
42+
if ($attribute['required'] === true && in_array($attribute['$id'], $requiredAttributes)) {
43+
$requiredAttributesMap[] = $attribute;
44+
}
45+
}
3946

47+
if (!$this->checkForAllRequiredValues($structure, $requiredAttributesMap, $keys)) {
48+
return false;
49+
}
4050
if (!$this->checkForUnknownAttributes($structure, $keys)) {
4151
return false;
4252
}

0 commit comments

Comments
 (0)