Skip to content

Commit 9bde766

Browse files
committed
Add additional checks on the database connection
1 parent 8a2d0b0 commit 9bde766

1 file changed

Lines changed: 14 additions & 5 deletions

File tree

src/HealthCheck/DoctrineConnectionHealthCheck.php

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
namespace OpenConext\MonitorBundle\HealthCheck;
2020

2121
use Doctrine\DBAL\Connection;
22+
use Doctrine\DBAL\Exception\ConnectionException;
2223
use Exception;
2324
use OpenConext\MonitorBundle\Value\HealthReport;
2425
use Symfony\Component\DependencyInjection\Attribute\Autowire;
@@ -42,15 +43,23 @@ public function check(HealthReportInterface $report): HealthReportInterface
4243
{
4344
if (!is_null($this->connection)) {
4445
try {
46+
$this->connection->connect(); // This will create the SQLite database if it does not exist
4547
// Get the schema manager and grab the first table to later query on
4648
$sm = $this->connection->createSchemaManager();
49+
4750
$tables = $sm->listTables();
48-
if (!empty($tables)) {
49-
$table = reset($tables);
50-
// Perform a light-weight query on the chosen table
51-
$query = "SELECT * FROM %s LIMIT 1";
52-
$this->connection->executeQuery(sprintf($query, $table->getName()));
51+
52+
if ($tables === []) {
53+
return HealthReport::buildStatusDown('No tables found in the database.');
5354
}
55+
56+
$table = reset($tables);
57+
// Perform a light-weight query on the chosen table
58+
$query = "SELECT * FROM %s LIMIT 1";
59+
$this->connection->executeQuery(sprintf($query, $table->getName()));
60+
61+
} catch (ConnectionException) {
62+
return HealthReport::buildStatusDown('Unable to connect to the database.');
5463
} catch (Exception) {
5564
return HealthReport::buildStatusDown('Unable to execute a query on the database.');
5665
}

0 commit comments

Comments
 (0)