From 7e8beb7152ac7e9bf537ef7d217ab268fb8b7983 Mon Sep 17 00:00:00 2001 From: "Sebastian BURGIN-FIX (ext)" Date: Fri, 23 Oct 2026 16:09:51 +0200 Subject: [PATCH 1/3] fix(health): drop OptimizedAppCheck, Cloud containers never see deploy-time caches The config, route and event caches built by the Cloud deploy command never reach the running containers, so OptimizedAppCheck failed on every Cloud environment. php artisan optimize stays in the deploy script. Co-Authored-By: Claude Opus 5.5 --- app/Providers/AppServiceProvider.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index da8f8b8..688622a 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -35,7 +35,6 @@ use Spatie\Health\Checks\Checks\CacheCheck; use Spatie\Health\Checks\Checks\DebugModeCheck; use Spatie\Health\Checks\Checks\EnvironmentCheck; -use Spatie\Health\Checks\Checks\OptimizedAppCheck; use Spatie\Health\Checks\Checks\ScheduleCheck; use Spatie\Health\Facades\Health; use Spatie\SecurityAdvisoriesHealthCheck\SecurityAdvisoriesCheck; @@ -82,7 +81,6 @@ public function boot(): void DatabaseCheck::new(), DatabaseConnectionCountCheck::new(), CacheCheck::new(), - OptimizedAppCheck::new(), ScheduleCheck::new()->heartbeatMaxAgeInMinutes(120), SecurityAdvisoriesCheck::new(), NightwatchIssuesCheck::new()->if(filled(config('default.nightwatch.api_token')) && filled(config('default.nightwatch.application_id'))), From ae85d3e270911e3f26d5e47cd741bff57f432bce Mon Sep 17 00:00:00 2001 From: "Sebastian BURGIN-FIX (ext)" Date: Fri, 23 Oct 2026 16:12:08 +0200 Subject: [PATCH 2/3] fix(health): report the database connection count as info only DatabaseConnectionCountCheck no longer warns or fails; it reports "N connections" with the count in meta. On PostgreSQL it counts only pg_stat_activity rows of the current database, because the Neon servers are shared between apps. Co-Authored-By: Claude Opus 5.5 --- app/Checks/DatabaseConnectionCountCheck.php | 42 ++++++--- .../DatabaseConnectionCountCheckTest.php | 85 ++++++++++++++++--- 2 files changed, 102 insertions(+), 25 deletions(-) diff --git a/app/Checks/DatabaseConnectionCountCheck.php b/app/Checks/DatabaseConnectionCountCheck.php index e669a02..32dfca6 100644 --- a/app/Checks/DatabaseConnectionCountCheck.php +++ b/app/Checks/DatabaseConnectionCountCheck.php @@ -4,32 +4,52 @@ namespace App\Checks; +use Illuminate\Database\ConnectionResolverInterface; +use Illuminate\Support\Str; use Spatie\Health\Checks\Checks\DatabaseConnectionCountCheck as SpatieDatabaseConnectionCountCheck; use Spatie\Health\Checks\Result; /** - * spatie's DatabaseConnectionCountCheck with the agreed thresholds (warn - * above 50, fail above 80), reporting ok for apps without a database and - * for SQLite, which has no connection count. + * Informational only: reports the number of open connections of the app's + * own database and never warns or fails. On PostgreSQL only connections to + * the current database count, because Neon servers are shared between apps. + * Apps without a database and SQLite report ok. */ class DatabaseConnectionCountCheck extends SpatieDatabaseConnectionCountCheck { - protected ?int $warningThreshold = 50; - - protected int $errorThreshold = 80; - public function run(): Result { if (! DatabaseAvailability::isAvailable()) { return Result::make()->shortSummary('no database')->ok(); } - $connection = $this->connectionName ?? $this->getDefaultConnectionName(); - - if (config("database.connections.{$connection}.driver") === 'sqlite') { + if (config("database.connections.{$this->connection()}.driver") === 'sqlite') { return Result::make()->shortSummary('sqlite')->ok(); } - return parent::run(); + $connectionCount = $this->getConnectionCount(); + + return Result::make() + ->ok() + ->meta(['connection_count' => $connectionCount]) + ->shortSummary($connectionCount.' '.Str::plural('connection', $connectionCount)); + } + + protected function getConnectionCount(): int + { + $connection = app(ConnectionResolverInterface::class)->connection($this->connection()); + + if ($connection->getDriverName() !== 'pgsql') { + return parent::getConnectionCount(); + } + + $count = $connection->scalar('select count(*) from pg_stat_activity where datname = current_database()'); + + return is_numeric($count) ? (int) $count : 0; + } + + private function connection(): string + { + return $this->connectionName ?? $this->getDefaultConnectionName(); } } diff --git a/tests/Unit/Checks/DatabaseConnectionCountCheckTest.php b/tests/Unit/Checks/DatabaseConnectionCountCheckTest.php index 83d3c38..c20513a 100644 --- a/tests/Unit/Checks/DatabaseConnectionCountCheckTest.php +++ b/tests/Unit/Checks/DatabaseConnectionCountCheckTest.php @@ -3,26 +3,35 @@ declare(strict_types=1); use App\Checks\DatabaseConnectionCountCheck; +use Illuminate\Database\Connection; +use Illuminate\Database\ConnectionResolverInterface; +use Illuminate\Database\MySqlConnection; use Spatie\Health\Enums\Status; function connectionCountCheckWith(int $connections): DatabaseConnectionCountCheck { config(['health.uses_database' => true, 'database.connections.health_pgsql' => ['driver' => 'pgsql']]); - $check = new class($connections) extends DatabaseConnectionCountCheck - { - public function __construct(private readonly int $connections) {} - - protected function getConnectionCount(): int - { - return $this->connections; - } - }; + $check = Mockery::mock(DatabaseConnectionCountCheck::class)->makePartial()->shouldAllowMockingProtectedMethods(); + $check->shouldReceive('getConnectionCount')->andReturn($connections); $check->connectionName('health_pgsql'); return $check; } +function bindConnection(string $class, string $driver, mixed $count): void +{ + $connection = Mockery::mock($class); + $connection->shouldReceive('getDriverName')->andReturn($driver); + $connection->shouldReceive('scalar')->andReturn($count); + $connection->shouldReceive('selectOne')->andReturn((object) ['COUNT(*)' => $count]); + + $resolver = Mockery::mock(ConnectionResolverInterface::class); + $resolver->shouldReceive('connection')->andReturn($connection); + + app()->instance(ConnectionResolverInterface::class, $resolver); +} + it('reports ok with "no database" when the app has none', function () { config(['health.uses_database' => false]); @@ -41,10 +50,58 @@ protected function getConnectionCount(): int ->and($result->shortSummary)->toBe('sqlite'); })->group('unit', 'checks'); -it('applies the agreed thresholds', function (int $connections, Status $status) { - expect(connectionCountCheckWith($connections)->run()->status->equals($status))->toBeTrue(); +it('only reports the count and never warns or fails', function (int $connections, string $summary) { + $result = connectionCountCheckWith($connections)->run(); + + expect($result->status->equals(Status::ok()))->toBeTrue() + ->and($result->shortSummary)->toBe($summary) + ->and($result->meta)->toBe(['connection_count' => $connections]); })->with([ - 'ok' => [10, Status::ok()], - 'warning above 50' => [60, Status::warning()], - 'failed above 80' => [90, Status::failed()], + 'one' => [1, '1 connection'], + 'many' => [500, '500 connections'], ])->group('unit', 'checks'); + +it('counts only the connections of the current database on postgres', function () { + config(['database.connections.health_pgsql' => ['driver' => 'pgsql']]); + bindConnection(Connection::class, 'pgsql', 7); + + $check = new class extends DatabaseConnectionCountCheck + { + public function count(): int + { + return $this->getConnectionCount(); + } + }; + + expect($check->connectionName('health_pgsql')->count())->toBe(7); +})->group('unit', 'checks'); + +it('reports zero when postgres returns no number', function () { + config(['database.connections.health_pgsql' => ['driver' => 'pgsql']]); + bindConnection(Connection::class, 'pgsql', null); + + $check = new class extends DatabaseConnectionCountCheck + { + public function count(): int + { + return $this->getConnectionCount(); + } + }; + + expect($check->connectionName('health_pgsql')->count())->toBe(0); +})->group('unit', 'checks'); + +it('falls back to spatie for other drivers', function () { + config(['database.connections.health_mysql' => ['driver' => 'mysql']]); + bindConnection(MySqlConnection::class, 'mysql', 4); + + $check = new class extends DatabaseConnectionCountCheck + { + public function count(): int + { + return $this->getConnectionCount(); + } + }; + + expect($check->connectionName('health_mysql')->count())->toBe(4); +})->group('unit', 'checks'); From 7581bd1d7965d53a84a93bb272deb9e901b93ccd Mon Sep 17 00:00:00 2001 From: "Sebastian BURGIN-FIX (ext)" Date: Fri, 23 Oct 2026 16:16:22 +0200 Subject: [PATCH 3/3] test(health): keep the connection count test larastan-clean Larastan analyses tests in this repo; the Mockery partial and the chained connectionName()->count() calls were reported as untyped. Use an anonymous subclass instead, as before. Co-Authored-By: Claude Opus 5.5 --- .../DatabaseConnectionCountCheckTest.php | 23 +++++++++++++++---- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/tests/Unit/Checks/DatabaseConnectionCountCheckTest.php b/tests/Unit/Checks/DatabaseConnectionCountCheckTest.php index c20513a..114d3d0 100644 --- a/tests/Unit/Checks/DatabaseConnectionCountCheckTest.php +++ b/tests/Unit/Checks/DatabaseConnectionCountCheckTest.php @@ -12,8 +12,15 @@ function connectionCountCheckWith(int $connections): DatabaseConnectionCountChec { config(['health.uses_database' => true, 'database.connections.health_pgsql' => ['driver' => 'pgsql']]); - $check = Mockery::mock(DatabaseConnectionCountCheck::class)->makePartial()->shouldAllowMockingProtectedMethods(); - $check->shouldReceive('getConnectionCount')->andReturn($connections); + $check = new class($connections) extends DatabaseConnectionCountCheck + { + public function __construct(private readonly int $connections) {} + + protected function getConnectionCount(): int + { + return $this->connections; + } + }; $check->connectionName('health_pgsql'); return $check; @@ -73,7 +80,9 @@ public function count(): int } }; - expect($check->connectionName('health_pgsql')->count())->toBe(7); + $check->connectionName('health_pgsql'); + + expect($check->count())->toBe(7); })->group('unit', 'checks'); it('reports zero when postgres returns no number', function () { @@ -88,7 +97,9 @@ public function count(): int } }; - expect($check->connectionName('health_pgsql')->count())->toBe(0); + $check->connectionName('health_pgsql'); + + expect($check->count())->toBe(0); })->group('unit', 'checks'); it('falls back to spatie for other drivers', function () { @@ -103,5 +114,7 @@ public function count(): int } }; - expect($check->connectionName('health_mysql')->count())->toBe(4); + $check->connectionName('health_mysql'); + + expect($check->count())->toBe(4); })->group('unit', 'checks');