Skip to content

Commit ab5b315

Browse files
committed
Rollback on collection create fail
1 parent 767b4b2 commit ab5b315

1 file changed

Lines changed: 15 additions & 1 deletion

File tree

src/Database/Database.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1566,8 +1566,11 @@ public function createCollection(string $id, array $attributes = [], array $inde
15661566
}
15671567
}
15681568

1569+
$created = false;
1570+
15691571
try {
15701572
$this->adapter->createCollection($id, $attributes, $indexes);
1573+
$created = true;
15711574
} catch (DuplicateException $e) {
15721575
// HACK: Metadata should still be updated, can be removed when null tenant collections are supported.
15731576
if (!$this->adapter->getSharedTables() || !$this->isMigrating()) {
@@ -1579,7 +1582,18 @@ public function createCollection(string $id, array $attributes = [], array $inde
15791582
return new Document(self::COLLECTION);
15801583
}
15811584

1582-
$createdCollection = $this->silent(fn () => $this->createDocument(self::METADATA, $collection));
1585+
try {
1586+
$createdCollection = $this->silent(fn () => $this->createDocument(self::METADATA, $collection));
1587+
} catch (\Throwable $e) {
1588+
if ($created) {
1589+
try {
1590+
$this->adapter->deleteCollection($id);
1591+
} catch (\Throwable $rollbackError) {
1592+
// Log rollback failure but continue throwing original error
1593+
}
1594+
}
1595+
throw new DatabaseException("Failed to create collection metadata for '{$id}': " . $e->getMessage(), previous: $e);
1596+
}
15831597

15841598
$this->trigger(self::EVENT_COLLECTION_CREATE, $createdCollection);
15851599

0 commit comments

Comments
 (0)