generated from vormkracht10/laravel-package-template
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #37 from vormkracht10/feature/table-size-check
add DatabaseTableSizeCheck
- Loading branch information
Showing
1 changed file
with
78 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
<?php | ||
|
||
namespace Vormkracht10\LaravelOK\Checks; | ||
|
||
use Illuminate\Database\ConnectionInterface; | ||
use Illuminate\Database\ConnectionResolverInterface; | ||
use Illuminate\Database\MySqlConnection; | ||
use Illuminate\Database\PostgresConnection; | ||
use Vormkracht10\LaravelOK\Checks\Base\Check; | ||
use Vormkracht10\LaravelOK\Checks\Base\Result; | ||
|
||
class DatabaseTableSizeCheck extends Check | ||
{ | ||
protected string $connectionName; | ||
|
||
protected array $tableSizeThresholds = []; | ||
|
||
public function onConnection(string $name): static | ||
{ | ||
$this->connectionName = $name; | ||
|
||
return $this; | ||
} | ||
|
||
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; | ||
|
||
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'), | ||
}; | ||
} | ||
} |