From 25b6d6d00979e97edea864ff6c4519d6138f019b Mon Sep 17 00:00:00 2001 From: fogelito Date: Mon, 10 Aug 2026 15:04:03 +0300 Subject: [PATCH] test --- src/Database/Database.php | 5 +- tests/e2e/Adapter/Scopes/DocumentTests.php | 62 ++++++++++++++++++++++ 2 files changed, 64 insertions(+), 3 deletions(-) diff --git a/src/Database/Database.php b/src/Database/Database.php index a674dcc8b6..1caba3c55a 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -8579,10 +8579,9 @@ class Database $leadingOrderType = $orderTypes[0] ?? Database::ORDER_ASC; if (\in_array($leadingAttribute, ['$createdAt', '$updatedAt'], true)) { - \array_splice($orderAttributes, 1, 0, ['$sequence']); - \array_splice($orderTypes, 1, 0, [$leadingOrderType]); + $orderAttributes[] = '$sequence'; + $orderTypes[] = $leadingOrderType; } else { - // Everything else keeps the ascending tie break it has always had. $orderAttributes[] = '$sequence'; $orderTypes[] = Database::ORDER_ASC; } diff --git a/tests/e2e/Adapter/Scopes/DocumentTests.php b/tests/e2e/Adapter/Scopes/DocumentTests.php index 960e98ef14..622f034c80 100644 --- a/tests/e2e/Adapter/Scopes/DocumentTests.php +++ b/tests/e2e/Adapter/Scopes/DocumentTests.php @@ -3627,6 +3627,68 @@ trait DocumentTests $this->assertEquals($documentsTest[1]['$id'], $documents[0]['$id']); } + /** + * A batch insert stamps every row with the same `$createdAt` — createDocuments() takes + * one timestamp for the whole batch — so ordering by that timestamp alone leaves the + * order of those rows to the tie break Database::find() appends. + * + * The tie break follows the direction of the leading timestamp, so a descending order + * returns the batch newest-inserted first. It used to be appended ascending regardless, + * which both reversed the rows inside a tied group and produced a mixed-direction + * ORDER BY that no ascending index can answer. + */ + public function testFindOrderByCreateDateTieBreak(): void + { + /** @var Database $database */ + $database = $this->getDatabase(); + + $database->createCollection('tieBreak', permissions: [ + Permission::create(Role::any()), + Permission::read(Role::any()), + ], documentSecurity: false); + + $this->assertEquals(true, $database->createAttribute('tieBreak', 'name', Database::VAR_STRING, 128, true)); + + $documents = []; + foreach (['alpha', 'bravo', 'charlie'] as $name) { + $documents[] = new Document([ + 'name' => $name, + '$permissions' => [Permission::read(Role::any())], + ]); + } + + $this->assertEquals(3, $database->createDocuments('tieBreak', $documents)); + + $created = $database->find('tieBreak', [Query::orderAsc('$sequence')]); + $this->assertCount(3, $created); + + // The batch shares one timestamp, otherwise the tie break is never consulted. + $this->assertEquals($created[0]->getCreatedAt(), $created[2]->getCreatedAt()); + + $descending = $database->find('tieBreak', [Query::orderDesc('$createdAt')]); + $this->assertEquals( + [$created[2]->getSequence(), $created[1]->getSequence(), $created[0]->getSequence()], + \array_map(fn (Document $document) => $document->getSequence(), $descending) + ); + + $ascending = $database->find('tieBreak', [Query::orderAsc('$createdAt')]); + $this->assertEquals( + [$created[0]->getSequence(), $created[1]->getSequence(), $created[2]->getSequence()], + \array_map(fn (Document $document) => $document->getSequence(), $ascending) + ); + + // The tie break sits behind the caller's own order, so `name` still decides the + // rows the timestamp ties — placing a unique key ahead of it would leave it unread. + $byName = $database->find('tieBreak', [ + Query::orderDesc('$createdAt'), + Query::orderAsc('name'), + ]); + $this->assertEquals( + ['alpha', 'bravo', 'charlie'], + \array_map(fn (Document $document) => $document->getAttribute('name'), $byName) + ); + } + public function testFindCreatedBefore(): void { /** @var Database $database */