Skip to content

Commit

Permalink
Merge pull request #488 from utopia-php/fix-not-found-db
Browse files Browse the repository at this point in the history
Catch not found exception when checking database exists
  • Loading branch information
abnegate authored Dec 2, 2024
2 parents 12c42df + e7bfd89 commit d95bbbd
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 18 deletions.
33 changes: 18 additions & 15 deletions src/Database/Adapter/MariaDB.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -2412,22 +2426,11 @@ 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;
}

/**
* @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'];
}
}
21 changes: 18 additions & 3 deletions src/Database/Adapter/SQL.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -1136,4 +1146,9 @@ public function getInternalIndexesKeys(): array
{
return [];
}

protected function processException(PDOException $e): \Exception
{
return $e;
}
}

0 comments on commit d95bbbd

Please sign in to comment.