Skip to content

Commit d95bbbd

Browse files
authored
Merge pull request #488 from utopia-php/fix-not-found-db
Catch not found exception when checking database exists
2 parents 12c42df + e7bfd89 commit d95bbbd

File tree

2 files changed

+36
-18
lines changed

2 files changed

+36
-18
lines changed

src/Database/Adapter/MariaDB.php

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2379,6 +2379,20 @@ public function setTimeout(int $milliseconds, string $event = Database::EVENT_AL
23792379
});
23802380
}
23812381

2382+
/**
2383+
* @return string
2384+
*/
2385+
public function getConnectionId(): string
2386+
{
2387+
$stmt = $this->getPDO()->query("SELECT CONNECTION_ID();");
2388+
return $stmt->fetchColumn();
2389+
}
2390+
2391+
public function getInternalIndexesKeys(): array
2392+
{
2393+
return ['primary', '_created_at', '_updated_at', '_tenant_id'];
2394+
}
2395+
23822396
protected function processException(PDOException $e): \Exception
23832397
{
23842398
// Timeout
@@ -2412,22 +2426,11 @@ protected function processException(PDOException $e): \Exception
24122426
return new TruncateException('Resize would result in data truncation', $e->getCode(), $e);
24132427
}
24142428

2415-
2429+
// Unknown database
2430+
if ($e->getCode() === '42000' && isset($e->errorInfo[1]) && $e->errorInfo[1] === 1049) {
2431+
return new NotFoundException($e->getMessage(), $e->getCode(), $e);
2432+
}
24162433

24172434
return $e;
24182435
}
2419-
2420-
/**
2421-
* @return string
2422-
*/
2423-
public function getConnectionId(): string
2424-
{
2425-
$stmt = $this->getPDO()->query("SELECT CONNECTION_ID();");
2426-
return $stmt->fetchColumn();
2427-
}
2428-
2429-
public function getInternalIndexesKeys(): array
2430-
{
2431-
return ['primary', '_created_at', '_updated_at', '_tenant_id'];
2432-
}
24332436
}

src/Database/Adapter/SQL.php

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Utopia\Database\Database;
1010
use Utopia\Database\Document;
1111
use Utopia\Database\Exception as DatabaseException;
12+
use Utopia\Database\Exception\NotFound as NotFoundException;
1213
use Utopia\Database\Exception\Transaction as TransactionException;
1314
use Utopia\Database\Query;
1415

@@ -163,10 +164,19 @@ public function exists(string $database, ?string $collection = null): bool
163164
$stmt->bindValue(':schema', $database, PDO::PARAM_STR);
164165
}
165166

166-
$stmt->execute();
167+
try {
168+
$stmt->execute();
169+
$document = $stmt->fetchAll();
170+
$stmt->closeCursor();
171+
} catch (PDOException $e) {
172+
$e = $this->processException($e);
167173

168-
$document = $stmt->fetchAll();
169-
$stmt->closeCursor();
174+
if ($e instanceof NotFoundException) {
175+
return false;
176+
}
177+
178+
throw $e;
179+
}
170180

171181
if (empty($document)) {
172182
return false;
@@ -1136,4 +1146,9 @@ public function getInternalIndexesKeys(): array
11361146
{
11371147
return [];
11381148
}
1149+
1150+
protected function processException(PDOException $e): \Exception
1151+
{
1152+
return $e;
1153+
}
11391154
}

0 commit comments

Comments
 (0)