From 54bcda700f4747527df2de02a30e83d4c81830f3 Mon Sep 17 00:00:00 2001 From: David <75451291+dulkoss@users.noreply.github.com> Date: Wed, 27 Sep 2023 14:36:57 +0200 Subject: [PATCH 1/3] add DatabaseSizeCheck --- src/Checks/DatabaseSizeCheck.php | 76 ++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 src/Checks/DatabaseSizeCheck.php diff --git a/src/Checks/DatabaseSizeCheck.php b/src/Checks/DatabaseSizeCheck.php new file mode 100644 index 0000000..8454720 --- /dev/null +++ b/src/Checks/DatabaseSizeCheck.php @@ -0,0 +1,76 @@ +connection = $name; + + return $this; + } + + public function setMaxDatabaseSizeInGigabytes(int $amount): static + { + return $this->setMaxDatabaseSizeInMegabytes($amount * 1024); + } + + public function setMaxDatabaseSizeInMegabytes(int $amount): static + { + return $this->setMaxDatabaseSize($amount * 1024 * 1024); + } + + public function setMaxDatabaseSize(int $bytes): static + { + $this->max = $bytes; + + return $this; + } + + public function run(): Result + { + $result = Result::new(); + + $max = $this->max * 1024 * 1024 * 1024; + + $size = $this->getDatabaseSize(); + + + if ($size > $max) { + $mb = fn ($bytes) => round($bytes / 1024 / 1024); + + return $result->failed("Database has a size of {$mb($size)}MB, max is configured at {$mb($max)}MB"); + } + + return $result->ok('Database size is under the configured threshold'); + } + + protected function getDatabaseSize(): int + { + $connectionName = $this->connection ?? config('database.default'); + + $connection = app(ConnectionResolverInterface::class)->connection($connectionName); + + return match (true) { + $connection instanceof MySqlConnection => $connection->selectOne('SELECT size FROM (SELECT table_schema "name", ROUND(SUM(data_length + index_length)) AS size FROM information_schema.tables GROUP BY table_schema) alias_one WHERE name = ?', [ + $connection->getDatabaseName(), + ])->size, + $connection instanceof PostgresConnection => $connection->selectOne('SELECT pg_database_size(?) AS size;', [ + $connection->getDatabaseName(), + ])->size, + default => throw new \Exception("Database [{$connection->getDatabaseName()}] not supported"), + }; + } +} From f2762618a5ffbd7a20b5933bf1d1051da69758bc Mon Sep 17 00:00:00 2001 From: dulkoss Date: Wed, 27 Sep 2023 12:37:35 +0000 Subject: [PATCH 2/3] Fix styling --- src/Checks/DatabaseSizeCheck.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/Checks/DatabaseSizeCheck.php b/src/Checks/DatabaseSizeCheck.php index 8454720..53c38c3 100644 --- a/src/Checks/DatabaseSizeCheck.php +++ b/src/Checks/DatabaseSizeCheck.php @@ -5,7 +5,6 @@ use Illuminate\Database\ConnectionResolverInterface; use Illuminate\Database\MySqlConnection; use Illuminate\Database\PostgresConnection; -use Illuminate\Support\Arr; use Vormkracht10\LaravelOK\Checks\Base\Check; use Vormkracht10\LaravelOK\Checks\Base\Result; @@ -47,7 +46,6 @@ public function run(): Result $size = $this->getDatabaseSize(); - if ($size > $max) { $mb = fn ($bytes) => round($bytes / 1024 / 1024); From 1b7585e831e4de89b1ae6c4f6a4ef6c58baadcea Mon Sep 17 00:00:00 2001 From: David <75451291+dulkoss@users.noreply.github.com> Date: Wed, 27 Sep 2023 14:38:34 +0200 Subject: [PATCH 3/3] unused import --- src/Checks/DatabaseSizeCheck.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Checks/DatabaseSizeCheck.php b/src/Checks/DatabaseSizeCheck.php index 8454720..9092391 100644 --- a/src/Checks/DatabaseSizeCheck.php +++ b/src/Checks/DatabaseSizeCheck.php @@ -5,7 +5,6 @@ use Illuminate\Database\ConnectionResolverInterface; use Illuminate\Database\MySqlConnection; use Illuminate\Database\PostgresConnection; -use Illuminate\Support\Arr; use Vormkracht10\LaravelOK\Checks\Base\Check; use Vormkracht10\LaravelOK\Checks\Base\Result;