generated from spatie/package-skeleton-laravel
-
-
Notifications
You must be signed in to change notification settings - Fork 154
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 #151 from ralphjsmit/rjs/add-database-size-check
Add DatabaseSizeCheck
- Loading branch information
Showing
16 changed files
with
154 additions
and
15 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,39 @@ | ||
--- | ||
title: DB size | ||
weight: 7 | ||
--- | ||
|
||
This check makes sure that your database is not too big. This check supports MySQL and Postgres. | ||
|
||
If the database is larger than the specified maximum, this check will fail. | ||
|
||
## Requirements | ||
|
||
You'll need to install the `doctrine/dbal` package in your project. | ||
|
||
```bash | ||
composer require doctrine/dbal | ||
``` | ||
|
||
|
||
## Usage | ||
|
||
Here's how you can register the check. | ||
|
||
```php | ||
use Spatie\Health\Facades\Health; | ||
use Spatie\Health\Checks\Checks\DatabaseSizeCheck; | ||
|
||
Health::checks([ | ||
DatabaseSizeCheck::new() | ||
->failWhenSizeAboveGb(errorThresholdGb: 5.0) | ||
]); | ||
``` | ||
|
||
### Specifying the database connection | ||
|
||
To check another database connection, call `connectionName()` | ||
|
||
```php | ||
DatabaseSizeCheck::new()->connectionName('another-connection-name')->failWhenSizeAboveGb(5.0), | ||
``` |
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
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
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
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
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
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
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
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
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
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
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
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
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,54 @@ | ||
<?php | ||
|
||
namespace Spatie\Health\Checks\Checks; | ||
|
||
use Illuminate\Database\ConnectionResolverInterface; | ||
use Spatie\Health\Checks\Check; | ||
use Spatie\Health\Checks\Result; | ||
use Spatie\Health\Support\DbConnectionInfo; | ||
|
||
class DatabaseSizeCheck extends Check | ||
{ | ||
protected ?string $connectionName = null; | ||
|
||
protected float $failWhenSizeAboveGb = 1; | ||
|
||
public function connectionName(string $connectionName): self | ||
{ | ||
$this->connectionName = $connectionName; | ||
|
||
return $this; | ||
} | ||
|
||
public function failWhenSizeAboveGb(float $errorThresholdGb): self | ||
{ | ||
$this->failWhenSizeAboveGb = $errorThresholdGb; | ||
|
||
return $this; | ||
} | ||
|
||
public function run(): Result | ||
{ | ||
$result = Result::make(); | ||
|
||
$databaseSizeInGb = $this->getDatabaseSizeInGb(); | ||
|
||
return $databaseSizeInGb >= $this->failWhenSizeAboveGb | ||
? $result->failed("Database size is {$databaseSizeInGb} GB, which is above the threshold of {$this->failWhenSizeAboveGb} GB") | ||
: $result->ok("{$databaseSizeInGb} GB"); | ||
} | ||
|
||
protected function getDefaultConnectionName(): string | ||
{ | ||
return config('database.default'); | ||
} | ||
|
||
protected function getDatabaseSizeInGb(): float | ||
{ | ||
$connectionName = $this->connectionName ?? $this->getDefaultConnectionName(); | ||
|
||
$connection = app(ConnectionResolverInterface::class)->connection($connectionName); | ||
|
||
return round((new DbConnectionInfo())->databaseSizeInMb($connection) / 1000, 2); | ||
} | ||
} |
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
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,23 @@ | ||
<?php | ||
|
||
use Illuminate\Support\Facades\DB; | ||
use Spatie\Health\Checks\Checks\DatabaseSizeCheck; | ||
use Spatie\Health\Enums\Status; | ||
|
||
it('will determine that database size is ok if it does not cross the maximum', function () { | ||
$result = DatabaseSizeCheck::new() | ||
->connectionName('mysql') | ||
->failWhenSizeAboveGb(50) | ||
->run(); | ||
|
||
expect($result->status)->toBe(Status::ok()); | ||
}); | ||
|
||
it('will determine that database size is not ok if it does cross the maximum', function () { | ||
$result = DatabaseSizeCheck::new() | ||
->connectionName('mysql') | ||
->failWhenSizeAboveGb(0) | ||
->run(); | ||
|
||
expect($result->status)->toBe(Status::failed()); | ||
}); |