diff --git a/src/Checks/DatabaseSizeCheck.php b/src/Checks/DatabaseSizeCheck.php new file mode 100644 index 0000000..53c38c3 --- /dev/null +++ b/src/Checks/DatabaseSizeCheck.php @@ -0,0 +1,74 @@ +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"), + }; + } +}