Skip to content

Commit

Permalink
Merge pull request #487 from utopia-php/fix-create-order-of-operations
Browse files Browse the repository at this point in the history
Adapter create after limit checks
  • Loading branch information
abnegate authored Dec 2, 2024
2 parents 29f5e3f + 38354fb commit 12c42df
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions src/Database/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -1191,12 +1191,6 @@ public function createCollection(string $id, array $attributes = [], array $inde
}
}

$this->adapter->createCollection($id, $attributes, $indexes);

if ($id === self::METADATA) {
return new Document(self::COLLECTION);
}

// Check index limits, if given
if ($indexes && $this->adapter->getCountOfIndexes($collection) > $this->adapter->getLimitForIndexes()) {
throw new LimitException('Index limit of ' . $this->adapter->getLimitForIndexes() . ' exceeded. Cannot create collection.');
Expand All @@ -1208,18 +1202,29 @@ public function createCollection(string $id, array $attributes = [], array $inde
$this->adapter->getLimitForAttributes() > 0 &&
$this->adapter->getCountOfAttributes($collection) > $this->adapter->getLimitForAttributes()
) {
throw new LimitException('Column limit of ' . $this->adapter->getLimitForAttributes() . ' exceeded. Cannot create collection.');
throw new LimitException('Attribute limit of ' . $this->adapter->getLimitForAttributes() . ' exceeded. Cannot create collection.');
}

if (
$this->adapter->getDocumentSizeLimit() > 0 &&
$this->adapter->getAttributeWidth($collection) > $this->adapter->getDocumentSizeLimit()
) {
throw new LimitException('Row width limit of ' . $this->adapter->getDocumentSizeLimit() . ' exceeded. Cannot create collection.');
throw new LimitException('Document size limit of ' . $this->adapter->getDocumentSizeLimit() . ' exceeded. Cannot create collection.');
}
}

$createdCollection = $this->silent(fn () => $this->createDocument(self::METADATA, $collection));
$this->adapter->createCollection($id, $attributes, $indexes);

if ($id === self::METADATA) {
return new Document(self::COLLECTION);
}

try {
$createdCollection = $this->silent(fn () => $this->createDocument(self::METADATA, $collection));
} catch (Exception $e) {
$this->adapter->deleteCollection($id);
throw $e;
}

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

Expand Down Expand Up @@ -1410,6 +1415,8 @@ public function deleteCollection(string $id): bool
$this->trigger(self::EVENT_COLLECTION_DELETE, $collection);
}

$this->purgeCachedCollection($id);

return $deleted;
}

Expand Down Expand Up @@ -1521,7 +1528,7 @@ public function createAttribute(string $collection, string $id, string $type, in
// Only execute when $default is given
if (!\is_null($default)) {
if ($required === true) {
throw new DatabaseException('Cannot set a default value on a required attribute');
throw new DatabaseException('Cannot set a default value for a required attribute');
}

$this->validateDefaultTypes($type, $default);
Expand Down

0 comments on commit 12c42df

Please sign in to comment.