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 #36 from vormkracht10/feature/database-size-check
add DatabaseSizeCheck
- Loading branch information
Showing
1 changed file
with
74 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,74 @@ | ||
<?php | ||
|
||
namespace Vormkracht10\LaravelOK\Checks; | ||
|
||
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 DatabaseSizeCheck extends Check | ||
{ | ||
protected string $connection; | ||
|
||
protected int $max; | ||
|
||
public function onConnection(string $name): static | ||
{ | ||
$this->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"), | ||
}; | ||
} | ||
} |