Skip to content

Commit

Permalink
faster sql query, fixes OpenConext#15
Browse files Browse the repository at this point in the history
  • Loading branch information
tacman committed Apr 3, 2024
1 parent ba622ab commit 16df034
Showing 1 changed file with 5 additions and 10 deletions.
15 changes: 5 additions & 10 deletions src/HealthCheck/DoctrineConnectionHealthCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

namespace OpenConext\MonitorBundle\HealthCheck;

use Doctrine\DBAL\Connection;
use Doctrine\ORM\EntityManager;
use Exception;
use OpenConext\MonitorBundle\Value\HealthReport;
Expand Down Expand Up @@ -47,18 +48,12 @@ public function check(HealthReportInterface $report): HealthReportInterface
{
// Was the entityManager injected? When it is not the project does not use Doctrine ORM
if (!is_null($this->entityManager)) {
// @todo: get a custom query from the bundle configuration
$query = 'SELECT 1';
try {
// Get the schema manager and grab the first table to later query on
$sm = $this->entityManager->getConnection()->getSchemaManager();
$tables = $sm->listTables();
if (!empty($tables)) {
$table = reset($tables);
// Perform a light-weight query on the chosen table
$query = 'SELECT * FROM `%s` LIMIT 1';
$this->entityManager->getConnection()->executeQuery(sprintf($query, $table->getName()));
}
$this->entityManager->getConnection()->executeQuery($query);
} catch (Exception $e) {
return HealthReport::buildStatusDown('Unable to execute a query on the database.');
return HealthReport::buildStatusDown("Unable to execute query [$query] on the database.");
}
}
return $report;
Expand Down

0 comments on commit 16df034

Please sign in to comment.