From c3b8f81bee0c5514763cbd6130cb5abaded41e11 Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Tue, 26 Nov 2024 18:39:27 +1300 Subject: [PATCH 1/5] Catch not found exception when checking database exists --- src/Database/Adapter/MariaDB.php | 5 ++++- src/Database/Adapter/SQL.php | 16 +++++++++++++--- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/Database/Adapter/MariaDB.php b/src/Database/Adapter/MariaDB.php index e0e514fcc..3f73497bf 100644 --- a/src/Database/Adapter/MariaDB.php +++ b/src/Database/Adapter/MariaDB.php @@ -2420,7 +2420,10 @@ protected function processException(PDOException $e): \Exception return new TruncateException('Resize would result in data truncation', $e->getCode(), $e); } - + // Unknown database + if ($e->getCode() === '42000' && isset($e->errorInfo[1]) && $e->errorInfo[1] === 1049) { + return new NotFoundException($e->getMessage(), $e->getCode(), $e); + } return $e; } diff --git a/src/Database/Adapter/SQL.php b/src/Database/Adapter/SQL.php index 7e649919d..70031b6b6 100644 --- a/src/Database/Adapter/SQL.php +++ b/src/Database/Adapter/SQL.php @@ -9,6 +9,7 @@ use Utopia\Database\Database; use Utopia\Database\Document; use Utopia\Database\Exception as DatabaseException; +use Utopia\Database\Exception\NotFound as NotFoundException; use Utopia\Database\Exception\Transaction as TransactionException; use Utopia\Database\Query; @@ -163,10 +164,19 @@ public function exists(string $database, ?string $collection = null): bool $stmt->bindValue(':schema', $database, PDO::PARAM_STR); } - $stmt->execute(); + try { + $stmt->execute(); + $document = $stmt->fetchAll(); + $stmt->closeCursor(); + } catch (PDOException $e) { + $e = $this->processException($e); - $document = $stmt->fetchAll(); - $stmt->closeCursor(); + if ($e instanceof NotFoundException) { + return false; + } + + throw $e; + } if (empty($document)) { return false; From d53d6ab3ad79b243c2979cf6cee6841de392fbda Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Tue, 26 Nov 2024 19:55:36 +1300 Subject: [PATCH 2/5] Fix stan --- src/Database/Adapter.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Database/Adapter.php b/src/Database/Adapter.php index f5748e460..76adf996e 100644 --- a/src/Database/Adapter.php +++ b/src/Database/Adapter.php @@ -1022,4 +1022,6 @@ abstract public function getConnectionId(): string; * @return array */ abstract public function getInternalIndexesKeys(): array; + + abstract protected function processException(\PDOException $e): \Exception; } From 6fdc4cc8a4db2f2a224daa3c999660e9409858d7 Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Wed, 27 Nov 2024 16:59:04 +1300 Subject: [PATCH 3/5] Debug dump --- src/Database/Database.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Database/Database.php b/src/Database/Database.php index 976806271..9d5d43742 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -3305,6 +3305,8 @@ public function createDocument(string $collection, Document $document): Document $collection = $this->silent(fn () => $this->getCollection($collection)); + \var_dump($collection); + if ($collection->getId() !== self::METADATA) { $authorization = new Authorization(self::PERMISSION_CREATE); if (!$authorization->isValid($collection->getCreate())) { From c3f03747a1bbc99fd5d5f414803ae314c037e739 Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Wed, 27 Nov 2024 20:24:35 +1300 Subject: [PATCH 4/5] Remove logging --- src/Database/Database.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/Database/Database.php b/src/Database/Database.php index 9d5d43742..976806271 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -3305,8 +3305,6 @@ public function createDocument(string $collection, Document $document): Document $collection = $this->silent(fn () => $this->getCollection($collection)); - \var_dump($collection); - if ($collection->getId() !== self::METADATA) { $authorization = new Authorization(self::PERMISSION_CREATE); if (!$authorization->isValid($collection->getCreate())) { From e7bfd895c9ebcf44cacf89c6d3d70278b12a4f92 Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Mon, 2 Dec 2024 17:58:21 +1300 Subject: [PATCH 5/5] Remove PDO specific exception from interface --- src/Database/Adapter.php | 2 -- src/Database/Adapter/MariaDB.php | 28 ++++++++++++++-------------- src/Database/Adapter/SQL.php | 5 +++++ 3 files changed, 19 insertions(+), 16 deletions(-) diff --git a/src/Database/Adapter.php b/src/Database/Adapter.php index 76adf996e..f5748e460 100644 --- a/src/Database/Adapter.php +++ b/src/Database/Adapter.php @@ -1022,6 +1022,4 @@ abstract public function getConnectionId(): string; * @return array */ abstract public function getInternalIndexesKeys(): array; - - abstract protected function processException(\PDOException $e): \Exception; } diff --git a/src/Database/Adapter/MariaDB.php b/src/Database/Adapter/MariaDB.php index 5139015aa..b616247ab 100644 --- a/src/Database/Adapter/MariaDB.php +++ b/src/Database/Adapter/MariaDB.php @@ -2379,6 +2379,20 @@ public function setTimeout(int $milliseconds, string $event = Database::EVENT_AL }); } + /** + * @return string + */ + public function getConnectionId(): string + { + $stmt = $this->getPDO()->query("SELECT CONNECTION_ID();"); + return $stmt->fetchColumn(); + } + + public function getInternalIndexesKeys(): array + { + return ['primary', '_created_at', '_updated_at', '_tenant_id']; + } + protected function processException(PDOException $e): \Exception { // Timeout @@ -2419,18 +2433,4 @@ protected function processException(PDOException $e): \Exception return $e; } - - /** - * @return string - */ - public function getConnectionId(): string - { - $stmt = $this->getPDO()->query("SELECT CONNECTION_ID();"); - return $stmt->fetchColumn(); - } - - public function getInternalIndexesKeys(): array - { - return ['primary', '_created_at', '_updated_at', '_tenant_id']; - } } diff --git a/src/Database/Adapter/SQL.php b/src/Database/Adapter/SQL.php index 70031b6b6..a0b6a3c6a 100644 --- a/src/Database/Adapter/SQL.php +++ b/src/Database/Adapter/SQL.php @@ -1146,4 +1146,9 @@ public function getInternalIndexesKeys(): array { return []; } + + protected function processException(PDOException $e): \Exception + { + return $e; + } }