1919namespace OpenConext \MonitorBundle \HealthCheck ;
2020
2121use Doctrine \DBAL \Connection ;
22+ use Doctrine \DBAL \Exception \ConnectionException ;
2223use Exception ;
2324use OpenConext \MonitorBundle \Value \HealthReport ;
2425use 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