From 82c132b64100e3f31f341420137f2efee4dd599a Mon Sep 17 00:00:00 2001 From: Mathieu Date: Sun, 7 Jun 2026 12:22:19 +0200 Subject: [PATCH] Display whether a query ran on a primary or replica in the profiler --- config/middlewares.php | 1 + src/Middleware/DebugMiddleware.php | 4 +++- templates/Collector/db.html.twig | 8 +++++++- 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/config/middlewares.php b/config/middlewares.php index a857faac0..b42570edc 100644 --- a/config/middlewares.php +++ b/config/middlewares.php @@ -33,6 +33,7 @@ ->args([ service('doctrine.debug_data_holder'), service('debug.stopwatch')->nullOnInvalid(), + service('doctrine'), ]) ->set('doctrine.dbal.idle_connection_middleware', IdleConnectionMiddleware::class) diff --git a/src/Middleware/DebugMiddleware.php b/src/Middleware/DebugMiddleware.php index 8c0571888..71a43bdf1 100644 --- a/src/Middleware/DebugMiddleware.php +++ b/src/Middleware/DebugMiddleware.php @@ -6,6 +6,7 @@ use Doctrine\DBAL\Driver as DriverInterface; use Doctrine\DBAL\Driver\Middleware; +use Doctrine\Persistence\ConnectionRegistry; use Symfony\Bridge\Doctrine\Middleware\Debug\DebugDataHolder; use Symfony\Bridge\Doctrine\Middleware\Debug\Driver; use Symfony\Component\Stopwatch\Stopwatch; @@ -17,6 +18,7 @@ class DebugMiddleware implements Middleware, ConnectionNameAwareInterface public function __construct( private readonly DebugDataHolder $debugDataHolder, private readonly Stopwatch|null $stopwatch, + private readonly ConnectionRegistry|null $connectionRegistry = null, ) { } @@ -27,6 +29,6 @@ public function setConnectionName(string $name): void public function wrap(DriverInterface $driver): DriverInterface { - return new Driver($driver, $this->debugDataHolder, $this->stopwatch, $this->connectionName); + return new Driver($driver, $this->debugDataHolder, $this->stopwatch, $this->connectionName, $this->connectionRegistry); } } diff --git a/templates/Collector/db.html.twig b/templates/Collector/db.html.twig index 9d44f4fd2..740290ff3 100644 --- a/templates/Collector/db.html.twig +++ b/templates/Collector/db.html.twig @@ -227,7 +227,13 @@ {{ query.count }} {% else %} {{ loop.index }} - {{ '%0.2f'|format(query.executionMS * 1000) }} ms + + {{ '%0.2f'|format(query.executionMS * 1000) }} ms + {% if query.ranOnPrimary is defined and query.ranOnPrimary is not null %} +
+ {{ query.ranOnPrimary ? 'Primary' : 'Replica' }} + {% endif %} + {% endif %} {{ query.sql|doctrine_prettify_sql }}