silverstripe-environmentcheck/code/checks/DatabaseCheck.php

41 lines
1014 B
PHP
Raw Normal View History

<?php
/**
* 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.
*/
2015-11-21 07:18:35 +01:00
class DatabaseCheck implements EnvironmentCheck
{
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
*/
public function __construct($checkTable = "Member")
{
$this->checkTable = $checkTable;
}
2015-11-21 07:18:35 +01:00
/**
* @inheritdoc
*
* @return array
*/
public function check()
{
if (!DB::getConn()->hasTable($this->checkTable)) {
return array(EnvironmentCheck::ERROR, "$this->checkTable not present in the database");
}
2015-11-21 07:18:35 +01:00
$count = DB::query("SELECT COUNT(*) FROM \"$this->checkTable\"")->value();
if ($count > 0) {
return array(EnvironmentCheck::OK, "");
} else {
return array(EnvironmentCheck::WARNING, "$this->checkTable queried ok but has no records");
}
}
2015-09-10 23:13:48 +02:00
}