-
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.
- Loading branch information
Showing
6 changed files
with
105 additions
and
5 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
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,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; | ||
} | ||
} |
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,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'); | ||
} | ||
} |
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