Skip to content

Commit

Permalink
Database Tables Check
Browse files Browse the repository at this point in the history
  • Loading branch information
svilborg committed Jul 16, 2019
1 parent 69a15aa commit b76b562
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/Checks/BaseCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

use Health\Builder\HealthCheckResponseBuilder;

abstract class BaseCheck implements HealthCheckInterface
abstract class BaseCheck
{

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Checks/Servers/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ public function call()
try {
DB::connection()->getPdo();
if (! DB::connection()->getDatabaseName()) {
$builder->state(false)->withData("error", "Could not find the database.");
$builder->down()->withData("error", "Could not find the database.");
}
} catch (\Exception $e) {
$builder->state(false)->withData("error", "Could not open connection to database server.");
$builder->down()->withData("error", "Could not open connection to database server.");
}

return $builder->build();
Expand Down
43 changes: 43 additions & 0 deletions src/Checks/Servers/DatabaseTables.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php
namespace Health\Checks\Servers;

use Health\Checks\HealthCheckInterface;
use DB;
use Health\HealthCheck;

class DatabaseTables extends Database implements HealthCheckInterface
{

/**
*
* {@inheritdoc}
* @see \Health\Checks\HealthCheckInterface::call()
*/
public function call()
{
$health = parent::call();

if ($health->getState() == HealthCheck::STATE_UP) {

$builder = $this->getBuilder();

$tables = $this->params['tables'] ?? [];
$missing = [];
foreach ($tables as $table) {
if (! DB::getSchemaBuilder()->hasTable($table)) {
$missing[] = $table;

$builder->down();
}
}

if ($missing) {
$builder->withData('missing', $missing);
}

$health = $builder->build();
}

return $health;
}
}
2 changes: 1 addition & 1 deletion tests/Checks/CheckTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class CheckTestCase extends \Orchestra\Testbench\TestCase

protected function assertCheck($health, string $state = 'UP')
{
// dd($health);
// dump($health);
$this->assertInstanceOf(HealthCheck::class, $health);
$this->assertNotEmpty($health->getName());
$this->assertEquals($state, $health->getState());
Expand Down
57 changes: 57 additions & 0 deletions tests/Checks/DatabaseTablesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php
namespace Tests\Checks;

use Health\Checks\Servers\DatabaseTables;
use Schema;

class DatabaseTablesTest extends CheckTestCase
{

/**
* Define environment setup.
*
* @param \Illuminate\Foundation\Application $app
* @return void
*/
protected function getEnvironmentSetUp($app)
{
// Setup default database to use sqlite :memory:
$app['config']->set('database.default', 'testing');
$app['config']->set('database.connections.testing', [
'driver' => 'sqlite',
'database' => ':memory:',
'prefix' => ''
]);
}

public function testCheckUpForEmptyTablesList()
{
$this->assertCheck($this->runCheck(DatabaseTables::class), 'UP');
}


public function testCheckUpForExistingNonExistingTable()
{
Schema::create('users', function ($table) {
$table->increments('id');
});

$check = $this->runCheck(DatabaseTables::class, [
'tables' => [
'users'
]
]);

$this->assertCheck($check, 'UP');

Schema::drop('users');

$check = $this->runCheck(DatabaseTables::class, [
'tables' => [
'users'
]
]);

$this->assertCheck($check, 'DOWN');
}
}
2 changes: 1 addition & 1 deletion tests/Checks/DatabaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ protected function getEnvironmentSetUp($app)
]);
}

public function testCheckDown()
public function testCheckUp()
{
$this->assertCheck($this->runCheck(Database::class), 'UP');
}
Expand Down

0 comments on commit b76b562

Please sign in to comment.