From f29c159caf6582b35147317ba17c492064160b2b Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Mon, 25 Nov 2024 21:06:49 +1300 Subject: [PATCH 1/5] Adapter create after limit checks --- src/Database/Database.php | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/Database/Database.php b/src/Database/Database.php index 976806271..23d31eaf2 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -1191,8 +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); } @@ -1208,17 +1206,19 @@ 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.'); } } + $this->adapter->createCollection($id, $attributes, $indexes); + $createdCollection = $this->silent(fn () => $this->createDocument(self::METADATA, $collection)); $this->trigger(self::EVENT_COLLECTION_CREATE, $createdCollection); @@ -1410,6 +1410,8 @@ public function deleteCollection(string $id): bool $this->trigger(self::EVENT_COLLECTION_DELETE, $collection); } + $this->purgeCachedCollection($id); + return $deleted; } @@ -1521,7 +1523,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); From 0e5801755059e3953804b191bf448e1e2cd01594 Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Mon, 25 Nov 2024 21:08:45 +1300 Subject: [PATCH 2/5] Fix meta check --- src/Database/Database.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Database/Database.php b/src/Database/Database.php index 23d31eaf2..d8f4860c0 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -1191,10 +1191,6 @@ public function createCollection(string $id, array $attributes = [], array $inde } } - 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.'); @@ -1219,6 +1215,10 @@ public function createCollection(string $id, array $attributes = [], array $inde $this->adapter->createCollection($id, $attributes, $indexes); + if ($id === self::METADATA) { + return new Document(self::COLLECTION); + } + $createdCollection = $this->silent(fn () => $this->createDocument(self::METADATA, $collection)); $this->trigger(self::EVENT_COLLECTION_CREATE, $createdCollection); From 43180e3fbe997af4dbdad4e6f5341acafb3bb3bd Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Mon, 25 Nov 2024 21:38:49 +1300 Subject: [PATCH 3/5] Delete table if metadata create failed --- src/Database/Database.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/Database/Database.php b/src/Database/Database.php index d8f4860c0..7764554b4 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -1219,7 +1219,12 @@ public function createCollection(string $id, array $attributes = [], array $inde return new Document(self::COLLECTION); } - $createdCollection = $this->silent(fn () => $this->createDocument(self::METADATA, $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); From bd502439b3ea9dc29847b0edd917e031599f0af1 Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Tue, 26 Nov 2024 15:55:07 +1300 Subject: [PATCH 4/5] Dump on metadata create fail --- src/Database/Database.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Database/Database.php b/src/Database/Database.php index 7764554b4..50987c197 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -1222,6 +1222,7 @@ public function createCollection(string $id, array $attributes = [], array $inde try { $createdCollection = $this->silent(fn () => $this->createDocument(self::METADATA, $collection)); } catch (Exception $e) { + \var_dump('Error creating collection ' . $id . ' metadata document. Deleting collection.' . "\n" . $e->getMessage() . "\n" . $e->getTraceAsString()); $this->adapter->deleteCollection($id); throw $e; } From 38354fb891dc509006c0a81651ffc6d17796466f Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Tue, 26 Nov 2024 17:10:48 +1300 Subject: [PATCH 5/5] Remove dump --- src/Database/Database.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Database/Database.php b/src/Database/Database.php index 50987c197..7764554b4 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -1222,7 +1222,6 @@ public function createCollection(string $id, array $attributes = [], array $inde try { $createdCollection = $this->silent(fn () => $this->createDocument(self::METADATA, $collection)); } catch (Exception $e) { - \var_dump('Error creating collection ' . $id . ' metadata document. Deleting collection.' . "\n" . $e->getMessage() . "\n" . $e->getTraceAsString()); $this->adapter->deleteCollection($id); throw $e; }