Skip to content
Closed
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
3 changes: 1 addition & 2 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
>
stopOnFailure="true">
<testsuites>
<testsuite name="unit">
<directory>./tests/unit</directory>
Expand Down
25 changes: 12 additions & 13 deletions src/Database/Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -388,19 +388,6 @@ public function withTransaction(callable $callback): mixed
} catch (\Throwable $action) {
try {
$this->rollbackTransaction();

if (
$action instanceof DuplicateException ||
$action instanceof RestrictedException ||
$action instanceof AuthorizationException ||
$action instanceof RelationshipException ||
$action instanceof ConflictException ||
$action instanceof LimitException
) {
$this->inTransaction = 0;
throw $action;
}

} catch (\Throwable $rollback) {
if ($attempts < $retries) {
\usleep($sleep * ($attempts + 1));
Expand All @@ -411,6 +398,18 @@ public function withTransaction(callable $callback): mixed
throw $rollback;
}

if (
$action instanceof DuplicateException ||
$action instanceof RestrictedException ||
$action instanceof AuthorizationException ||
$action instanceof RelationshipException ||
$action instanceof ConflictException ||
$action instanceof LimitException
) {
$this->inTransaction = 0;
throw $action;
}

if ($attempts < $retries) {
\usleep($sleep * ($attempts + 1));
continue;
Expand Down
17 changes: 17 additions & 0 deletions tests/e2e/Adapter/Scopes/CollectionTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,23 @@ public function testCreateListExistsDeleteCollection(): void
/** @var Database $database */
$database = static::getDatabase();


$database->createCollection('shmuel', permissions: [
Permission::create(Role::any()),
Permission::read(Role::any()),
]);

$database->createDocument('shmuel', new Document([
'$id' => 'doc1',
]));

$database->createDocument('shmuel', new Document([
'$id' => 'doc1',
]));

$this->assertEquals(1,222);


Comment on lines +47 to +63
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Remove failing debug block; assert DuplicateException and clean up to avoid state leakage.

This prep code will (a) error on duplicate insert without asserting, (b) fail the suite with an intentional bad assert, and (c) pollute later collection count assertions by leaving 'shmuel' around. Replace with an explicit DuplicateException check and delete the temp collection.

Apply this diff:

@@
-        $database->createCollection('shmuel', permissions: [
-            Permission::create(Role::any()),
-            Permission::read(Role::any()),
-        ]);
-
-        $database->createDocument('shmuel', new Document([
-            '$id' => 'doc1',
-        ]));
-
-        $database->createDocument('shmuel', new Document([
-            '$id' => 'doc1',
-        ]));
-
-        $this->assertEquals(1,222);
+        // Sanity check for transaction behavior on duplicate document IDs.
+        $database->createCollection('shmuel', permissions: [
+            Permission::create(Role::any()),
+            Permission::read(Role::any()),
+        ]);
+        $database->createDocument('shmuel', new Document([
+            '$id' => 'doc1',
+        ]));
+        try {
+            $database->createDocument('shmuel', new Document([
+                '$id' => 'doc1',
+            ]));
+            $this->fail('Failed to throw exception');
+        } catch (Exception $e) {
+            $this->assertInstanceOf(DuplicateException::class, $e);
+        }
+        $this->assertTrue($database->deleteCollection('shmuel'));
🤖 Prompt for AI Agents
In tests/e2e/Adapter/Scopes/CollectionTests.php around lines 47 to 63, the block
attempts to insert a duplicate document then leaves an intentional failing
assert and the temporary 'shmuel' collection behind; replace that debug code
with a proper assertion that a DuplicateException is thrown on the second
createDocument (use $this->expectException(DuplicateException::class) or
equivalent try/catch asserting the exception), and after the check explicitly
delete the 'shmuel' collection to avoid state leakage for later tests.

$this->assertInstanceOf('Utopia\Database\Document', $database->createCollection('actors', permissions: [
Permission::create(Role::any()),
Permission::read(Role::any()),
Expand Down
Loading