src/Controller/Api/HealthController.php line 19

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Api;
  3. use App\Entity\Elevator;
  4. use App\Entity\Pump;
  5. use App\Entity\Sensor;
  6. use App\Entity\Ticket;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
  9. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  10. use Symfony\Component\HttpFoundation\JsonResponse;
  11. class HealthController extends AbstractController
  12. {
  13.     /**
  14.      * @Route("/health", name="api_health", methods={"GET"})
  15.      */
  16.     public function healthAction(EntityManagerInterface $em): JsonResponse
  17.     {
  18.         // Leichte Read-Query je Kern-Entity: findOneBy([]) lädt eine volle Zeile mit ALLEN
  19.         // gemappten Spalten (LIMIT 1). Fehlt eine Spalte in der DB (Schema-Drift / fehlende
  20.         // Migration), wirft die Query hier — so wird genau die "Unknown column"-500er-Klasse
  21.         // (z.B. sensor.pending_command, pump.current) beim Deploy-Health-Check sichtbar,
  22.         // statt still im Betrieb aufzuschlagen.
  23.         $entities = [
  24.             'sensor' => Sensor::class,
  25.             'pump' => Pump::class,
  26.             'elevator' => Elevator::class,
  27.             'ticket' => Ticket::class,
  28.         ];
  29.         foreach ($entities as $key => $class) {
  30.             try {
  31.                 $em->getRepository($class)->findOneBy([]);
  32.             } catch (\Throwable $e) {
  33.                 return new JsonResponse([
  34.                     'status' => 'error',
  35.                     'check' => $key,
  36.                     'message' => $e->getMessage(),
  37.                 ], JsonResponse::HTTP_SERVICE_UNAVAILABLE);
  38.             }
  39.         }
  40.         return new JsonResponse(['status' => 'ok']);
  41.     }
  42. }