2012-01-20 18:10:48 +01:00
|
|
|
<?php
|
|
|
|
|
2017-01-04 23:16:12 +01:00
|
|
|
namespace SilverStripe\EnvironmentCheck\Checks;
|
|
|
|
|
|
|
|
use SilverStripe\EnvironmentCheck\EnvironmentCheck;
|
|
|
|
use SilverStripe\ORM\DB;
|
|
|
|
|
2012-01-20 18:10:48 +01:00
|
|
|
/**
|
2013-04-04 11:28:33 +02:00
|
|
|
* Check that the connection to the database is working, by ensuring that the table exists and that
|
2015-09-10 23:13:48 +02:00
|
|
|
* the table contains some records.
|
2017-01-04 23:16:12 +01:00
|
|
|
*
|
|
|
|
* @package environmentcheck
|
2012-01-20 18:10:48 +01:00
|
|
|
*/
|
2015-11-21 07:18:35 +01:00
|
|
|
class DatabaseCheck implements EnvironmentCheck
|
|
|
|
{
|
2017-03-21 11:57:07 +01:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2015-11-21 07:18:35 +01:00
|
|
|
protected $checkTable;
|
2015-09-10 23:13:48 +02:00
|
|
|
|
2015-11-21 07:18:35 +01:00
|
|
|
/**
|
|
|
|
* By default, Member will be checked.
|
|
|
|
*
|
|
|
|
* @param string $checkTable
|
|
|
|
*/
|
2017-01-04 23:16:12 +01:00
|
|
|
public function __construct($checkTable = 'Member')
|
2015-11-21 07:18:35 +01:00
|
|
|
{
|
|
|
|
$this->checkTable = $checkTable;
|
|
|
|
}
|
2013-04-04 11:28:33 +02:00
|
|
|
|
2015-11-21 07:18:35 +01:00
|
|
|
/**
|
2017-01-04 23:16:12 +01:00
|
|
|
* {@inheritDoc}
|
2015-11-21 07:18:35 +01:00
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function check()
|
|
|
|
{
|
2017-01-04 23:16:12 +01:00
|
|
|
if (!DB::get_schema()->hasTable($this->checkTable)) {
|
2017-03-21 11:57:07 +01:00
|
|
|
return [EnvironmentCheck::ERROR, "$this->checkTable not present in the database"];
|
2015-11-21 07:18:35 +01:00
|
|
|
}
|
2013-04-04 11:28:33 +02:00
|
|
|
|
2015-11-21 07:18:35 +01:00
|
|
|
$count = DB::query("SELECT COUNT(*) FROM \"$this->checkTable\"")->value();
|
2017-01-04 23:16:12 +01:00
|
|
|
|
2015-11-21 07:18:35 +01:00
|
|
|
if ($count > 0) {
|
2017-03-21 11:57:07 +01:00
|
|
|
return [EnvironmentCheck::OK, ''];
|
2015-11-21 07:18:35 +01:00
|
|
|
}
|
2017-01-04 23:16:12 +01:00
|
|
|
|
2017-03-21 11:57:07 +01:00
|
|
|
return [EnvironmentCheck::WARNING, "$this->checkTable queried ok but has no records"];
|
2015-11-21 07:18:35 +01:00
|
|
|
}
|
2015-09-10 23:13:48 +02:00
|
|
|
}
|