Skip to content
Open
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
33 changes: 31 additions & 2 deletions src/Database/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,8 @@ class Database

protected bool $preserveDates = false;

protected bool $preserveSequence = false;

protected int $maxQueryValues = 5000;

protected bool $migrating = false;
Expand Down Expand Up @@ -1400,6 +1402,30 @@ public function withPreserveDates(callable $callback): mixed
}
}

public function getPreserveSequence(): bool
{
return $this->preserveSequence;
}

public function setPreserveSequence(bool $preserve): static
{
$this->preserveSequence = $preserve;

return $this;
}

public function withPreserveSequence(callable $callback): mixed
{
$previous = $this->preserveSequence;
$this->preserveSequence = true;

try {
return $callback();
} finally {
$this->preserveSequence = $previous;
}
}

public function setMaxQueryValues(int $max): self
{
$this->maxQueryValues = $max;
Expand Down Expand Up @@ -6594,8 +6620,11 @@ public function upsertDocumentsWithIncrease(
$document
->setAttribute('$id', empty($document->getId()) ? ID::unique() : $document->getId())
->setAttribute('$collection', $collection->getId())
->setAttribute('$updatedAt', ($updatedAt === null || !$this->preserveDates) ? $time : $updatedAt)
->removeAttribute('$sequence');
->setAttribute('$updatedAt', ($updatedAt === null || !$this->preserveDates) ? $time : $updatedAt);

if (!$this->preserveSequence) {
$document->removeAttribute('$sequence');
}

$createdAt = $document->getCreatedAt();
if ($createdAt === null || !$this->preserveDates) {
Expand Down
9 changes: 9 additions & 0 deletions src/Database/Mirror.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,15 @@ public function setPreserveDates(bool $preserve): static
return $this;
}

public function setPreserveSequence(bool $preserve): static
{
$this->delegate(__FUNCTION__, \func_get_args());

$this->preserveSequence = $preserve;

return $this;
}

public function enableValidation(): static
{
$this->delegate(__FUNCTION__);
Expand Down
138 changes: 138 additions & 0 deletions tests/e2e/Adapter/Scopes/DocumentTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -1178,6 +1178,144 @@ public function testUpsertMixedPermissionDelta(): void
], $db->getDocument(__FUNCTION__, 'b')->getPermissions());
}

public function testPreserveSequenceUpsert(): void
{
/** @var Database $database */
$database = $this->getDatabase();

if (!$database->getAdapter()->getSupportForUpserts()) {
$this->expectNotToPerformAssertions();
return;
}

$collectionName = 'preserve_sequence_upsert';

$database->createCollection($collectionName);

if ($database->getAdapter()->getSupportForAttributes()) {
$database->createAttribute($collectionName, 'name', Database::VAR_STRING, 128, true);
}

// Create initial documents
$doc1 = $database->createDocument($collectionName, new Document([
'$id' => 'doc1',
'$permissions' => [
Permission::read(Role::any()),
Permission::update(Role::any()),
],
'name' => 'Alice',
]));

$doc2 = $database->createDocument($collectionName, new Document([
'$id' => 'doc2',
'$permissions' => [
Permission::read(Role::any()),
Permission::update(Role::any()),
],
'name' => 'Bob',
]));

$originalSeq1 = $doc1->getSequence();
$originalSeq2 = $doc2->getSequence();

$this->assertNotEmpty($originalSeq1);
$this->assertNotEmpty($originalSeq2);

// Test: Without preserveSequence (default), $sequence should be ignored
$database->setPreserveSequence(false);

$database->upsertDocuments($collectionName, [
new Document([
'$id' => 'doc1',
'$sequence' => 999, // Try to set a different sequence
'$permissions' => [
Permission::read(Role::any()),
Permission::update(Role::any()),
],
'name' => 'Alice Updated',
]),
]);
Comment on lines +1218 to +1237
Copy link
Contributor

@coderabbitai coderabbitai bot Jan 19, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Use a valid (UUID7-safe) sequence value in the “ignored” branch.

'$sequence' => 999 can be invalid for UUID7 adapters; if validation happens before $sequence is stripped, this test will fail or become adapter‑order dependent. Prefer a valid sequence (e.g., the other document’s sequence) to keep the test robust and still verify “ignore” behavior.

🔧 Suggested adjustment
         $originalSeq1 = $doc1->getSequence();
         $originalSeq2 = $doc2->getSequence();

         $this->assertNotEmpty($originalSeq1);
         $this->assertNotEmpty($originalSeq2);
+        $this->assertNotEquals($originalSeq1, $originalSeq2);

         // Test: Without preserveSequence (default), $sequence should be ignored
         $database->setPreserveSequence(false);

         $database->upsertDocuments($collectionName, [
             new Document([
                 '$id' => 'doc1',
-                '$sequence' => 999, // Try to set a different sequence
+                '$sequence' => $originalSeq2, // Valid but different sequence
                 '$permissions' => [
                     Permission::read(Role::any()),
                     Permission::update(Role::any()),
                 ],
                 'name' => 'Alice Updated',
             ]),
         ]);
🤖 Prompt for AI Agents
In `@tests/e2e/Adapter/Scopes/DocumentTests.php` around lines 1218 - 1237, The
test uses an invalid literal sequence 999 which can fail UUID7 adapters; change
the upsert payload in the ignored-branch to supply a valid sequence value (e.g.,
reuse $originalSeq1 or $originalSeq2) instead of 999 so validation passes even
though setPreserveSequence(false) should ignore it; update the Document creation
passed to Database::upsertDocuments (the new Document([... '$sequence' => ...]))
to use the captured $originalSeq1/$originalSeq2 while keeping the rest of the
assertions intact.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the setPreserveSequence is set false so getting ignored

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ArnabChatterjee20k Totally—preserveSequence=false means we intend to ignore $sequence. The concern is test robustness: some adapters may validate the payload before the stripping happens, and 999 is not a valid UUID7 sequence. Using a known-valid, different sequence keeps the test adapter-agnostic while still proving the “ignore” behavior.

Proposed small tweak:

  • Ensure the two initial sequences differ.
  • Provide a valid-but-different sequence in the “ignored” branch.
         $originalSeq1 = $doc1->getSequence();
         $originalSeq2 = $doc2->getSequence();

         $this->assertNotEmpty($originalSeq1);
         $this->assertNotEmpty($originalSeq2);
+        $this->assertNotEquals($originalSeq1, $originalSeq2);

         // Test: Without preserveSequence (default), $sequence should be ignored
         $database->setPreserveSequence(false);

         $database->upsertDocuments($collectionName, [
             new Document([
                 '$id' => 'doc1',
-                '$sequence' => 999, // Try to set a different sequence
+                '$sequence' => $originalSeq2, // Valid but different sequence; should still be ignored
                 '$permissions' => [
                     Permission::read(Role::any()),
                     Permission::update(Role::any()),
                 ],
                 'name' => 'Alice Updated',
             ]),
         ]);

This keeps the intent intact (sequence must remain $originalSeq1) and avoids adapter-order/format issues.


🧠 Learnings used
Learnt from: abnegate
Repo: utopia-php/database PR: 721
File: tests/e2e/Adapter/Scopes/DocumentTests.php:6418-6439
Timestamp: 2025-10-03T02:04:17.803Z
Learning: In tests/e2e/Adapter/Scopes/DocumentTests::testSchemalessDocumentInvalidInteralAttributeValidation (PHP), when the adapter reports getSupportForAttributes() === false (schemaless), the test should not expect exceptions from createDocuments for “invalid” internal attributes; remove try/catch and ensure the test passes without exceptions, keeping at least one assertion.

Learnt from: fogelito
Repo: utopia-php/database PR: 733
File: src/Database/Adapter/MariaDB.php:1801-1806
Timestamp: 2025-10-16T09:37:33.531Z
Learning: In the MariaDB adapter (src/Database/Adapter/MariaDB.php), only duplicate `_uid` violations should throw `DuplicateException`. All other unique constraint violations, including `PRIMARY` key collisions on the internal `_id` field, should throw `UniqueException`. This is the intended design to distinguish between user-facing document duplicates and internal/user-defined unique constraint violations.


$doc1Updated = $database->getDocument($collectionName, 'doc1');
$this->assertEquals('Alice Updated', $doc1Updated->getAttribute('name'));
$this->assertEquals($originalSeq1, $doc1Updated->getSequence()); // Sequence unchanged

// Test: With preserveSequence=true, $sequence from document should be used
$database->setPreserveSequence(true);

$database->upsertDocuments($collectionName, [
new Document([
'$id' => 'doc2',
'$sequence' => $originalSeq2, // Keep original sequence
'$permissions' => [
Permission::read(Role::any()),
Permission::update(Role::any()),
],
'name' => 'Bob Updated',
]),
]);

$doc2Updated = $database->getDocument($collectionName, 'doc2');
$this->assertEquals('Bob Updated', $doc2Updated->getAttribute('name'));
$this->assertEquals($originalSeq2, $doc2Updated->getSequence()); // Sequence preserved

// Test: withPreserveSequence helper
$database->setPreserveSequence(false);

$doc1 = $database->getDocument($collectionName, 'doc1');
$currentSeq1 = $doc1->getSequence();

$database->withPreserveSequence(function () use ($database, $collectionName, $currentSeq1) {
$database->upsertDocuments($collectionName, [
new Document([
'$id' => 'doc1',
'$sequence' => $currentSeq1,
'$permissions' => [
Permission::read(Role::any()),
Permission::update(Role::any()),
],
'name' => 'Alice Final',
]),
]);
});

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@premtsd-code can we add one more case here , where externally setting a very invalid sequence like 'abc' is throwing error

$doc1Final = $database->getDocument($collectionName, 'doc1');
$this->assertEquals('Alice Final', $doc1Final->getAttribute('name'));
$this->assertEquals($currentSeq1, $doc1Final->getSequence());

// Verify flag was reset after withPreserveSequence
$this->assertFalse($database->getPreserveSequence());

// Test: With preserveSequence=true, invalid $sequence should throw error (SQL adapters only)
$database->setPreserveSequence(true);

try {
$database->upsertDocuments($collectionName, [
new Document([
'$id' => 'doc1',
'$sequence' => 'abc', // Invalid sequence value
'$permissions' => [
Permission::read(Role::any()),
Permission::update(Role::any()),
],
'name' => 'Alice Invalid',
]),
]);
// Schemaless adapters may not validate sequence type, so only fail for schemaful
if ($database->getAdapter()->getSupportForAttributes()) {
$this->fail('Expected StructureException for invalid sequence');
}
} catch (Throwable $e) {
Copy link
Contributor

@ArnabChatterjee20k ArnabChatterjee20k Jan 19, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what if not support for attributes? what is the issue coming then? check processException

if ($database->getAdapter()->getSupportForAttributes()) {
$this->assertInstanceOf(StructureException::class, $e);
$this->assertStringContainsString('sequence', $e->getMessage());
}
}

$database->setPreserveSequence(false);
$database->deleteCollection($collectionName);
}

public function testRespectNulls(): Document
{
/** @var Database $database */
Expand Down