From 0ecb36d6e59d3eec871a445aa569152bfc8684c5 Mon Sep 17 00:00:00 2001 From: David <75451291+dulkoss@users.noreply.github.com> Date: Wed, 27 Sep 2023 15:16:17 +0200 Subject: [PATCH 1/4] add DatabaseTableSizeCheck --- src/Checks/DatabaseTableSizeCheck.php | 72 +++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 src/Checks/DatabaseTableSizeCheck.php diff --git a/src/Checks/DatabaseTableSizeCheck.php b/src/Checks/DatabaseTableSizeCheck.php new file mode 100644 index 0000000..93d1eec --- /dev/null +++ b/src/Checks/DatabaseTableSizeCheck.php @@ -0,0 +1,72 @@ +connectionName = $name; + + return $this; + } + + public function setTableSizeThresholds(array $config): static + { + $this->tableSizeThresholds = $config; + + return $this; + } + + public function run(): Result + { + $result = Result::new(); + + $connectionName = $this->connectionName ?? config('database.default'); + + $connection = app(ConnectionResolverInterface::class)->connection($connectionName); + + $config = array_map( + fn ($MB) => $MB * 1024 * 1024, + $this->tableSizeThresholds, + ); + + foreach ($config as $table => $max) { + $size = $this->getTableSize($connection, $table); + + if ($size > $max) { + $mb = fn ($bytes) => round($bytes / 1024 / 1024, 2); + + return $result->failed("Table [{$table}] size is {$mb($size)} megabytes, max is configured at {$mb($max)} megabytes"); + } + } + + return $result->ok(); + } + + protected function getTableSize(ConnectionInterface $connection, string $table): int + { + return match (true) { + $connection instanceof MySqlConnection => $connection->selectOne('SELECT (data_length + index_length) AS size FROM information_schema.TABLES WHERE table_schema = ? AND table_name = ?', [ + $connection->getDatabaseName(), + $table, + ])->size, + $connection instanceof PostgresConnection => $connection->selectOne('SELECT pg_total_relation_size(?) AS size;', [ + $table, + ])->size, + default => throw new \Exception("This database type is not supported"), + }; + } +} From 82860b24a7460c3746c56717dc502e725e0b1e06 Mon Sep 17 00:00:00 2001 From: dulkoss Date: Wed, 27 Sep 2023 13:16:46 +0000 Subject: [PATCH 2/4] Fix styling --- src/Checks/DatabaseTableSizeCheck.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Checks/DatabaseTableSizeCheck.php b/src/Checks/DatabaseTableSizeCheck.php index 93d1eec..0897660 100644 --- a/src/Checks/DatabaseTableSizeCheck.php +++ b/src/Checks/DatabaseTableSizeCheck.php @@ -2,7 +2,6 @@ namespace Vormkracht10\LaravelOK\Checks; -use Illuminate\Database\Connection; use Illuminate\Database\ConnectionInterface; use Illuminate\Database\ConnectionResolverInterface; use Illuminate\Database\MySqlConnection; @@ -66,7 +65,7 @@ protected function getTableSize(ConnectionInterface $connection, string $table): $connection instanceof PostgresConnection => $connection->selectOne('SELECT pg_total_relation_size(?) AS size;', [ $table, ])->size, - default => throw new \Exception("This database type is not supported"), + default => throw new \Exception('This database type is not supported'), }; } } From fc0ce97bb5e87ca36acdd20b18a8c2a194798a5b Mon Sep 17 00:00:00 2001 From: David <75451291+dulkoss@users.noreply.github.com> Date: Mon, 2 Oct 2023 09:35:22 +0200 Subject: [PATCH 3/4] rename method --- src/Checks/DatabaseTableSizeCheck.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Checks/DatabaseTableSizeCheck.php b/src/Checks/DatabaseTableSizeCheck.php index 0897660..024cc4f 100644 --- a/src/Checks/DatabaseTableSizeCheck.php +++ b/src/Checks/DatabaseTableSizeCheck.php @@ -22,7 +22,7 @@ public function onConnection(string $name): static return $this; } - public function setTableSizeThresholds(array $config): static + public function setTableSizeThresholdsInMegabytes(array $config): static { $this->tableSizeThresholds = $config; From e3cea66da99fe78a596c555d90153cf17439aedc Mon Sep 17 00:00:00 2001 From: David <75451291+dulkoss@users.noreply.github.com> Date: Thu, 5 Oct 2023 11:39:41 +0200 Subject: [PATCH 4/4] add `DatabaseTableSizeCheck::setMaxTableSizeInGigabytes` --- src/Checks/DatabaseTableSizeCheck.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/Checks/DatabaseTableSizeCheck.php b/src/Checks/DatabaseTableSizeCheck.php index 024cc4f..6fe65cc 100644 --- a/src/Checks/DatabaseTableSizeCheck.php +++ b/src/Checks/DatabaseTableSizeCheck.php @@ -22,7 +22,14 @@ public function onConnection(string $name): static return $this; } - public function setTableSizeThresholdsInMegabytes(array $config): static + public function setMaxTableSizeInGigabytes(array $config): static + { + return $this->setMaxTableSizeInMegabytes( + array_map(fn ($size) => $size * 1000, $config), + ); + } + + public function setMaxTableSizeInMegabytes(array $config): static { $this->tableSizeThresholds = $config;