Updated db setup to catch PDOExceptions

This commit is contained in:
Duncan Sommerville 2014-12-11 15:54:55 -05:00
parent 3966a7aa36
commit 270e84e9d8
1 changed files with 20 additions and 15 deletions

View File

@ -41,15 +41,10 @@ class DatabaseSetup extends Setup {
private function setNewDatabaseConnection($dbName) { private function setNewDatabaseConnection($dbName) {
self::$dbh = new PDO("pgsql:host=" . $this->host . ";dbname=" . $dbName . ";port=5432" self::$dbh = new PDO("pgsql:host=" . $this->host . ";dbname=" . $dbName . ";port=5432"
. ";user=" . $this->user . ";password=" . $this->pass); . ";user=" . $this->user . ";password=" . $this->pass);
$err = self::$dbh->errorInfo(); $err = self::$dbh->errorInfo();
if ($err[1] != null) { if ($err[1] != null) {
throw new AirtimeDatabaseException("Couldn't establish a connection to the database!", throw new PDOException();
array(
self::DB_NAME,
self::DB_USER,
self::DB_PASS,
));
} }
} }
@ -61,13 +56,23 @@ class DatabaseSetup extends Setup {
* @throws AirtimeDatabaseException * @throws AirtimeDatabaseException
*/ */
public function runSetup() { public function runSetup() {
$this->setNewDatabaseConnection("postgres"); try {
if ($this->checkDatabaseExists()) { $this->setNewDatabaseConnection("postgres");
$this->installDatabaseTables(); if ($this->checkDatabaseExists()) {
} else { $this->installDatabaseTables();
$this->checkUserCanCreateDb(); } else {
$this->createDatabase(); $this->checkUserCanCreateDb();
$this->installDatabaseTables(); $this->createDatabase();
$this->installDatabaseTables();
}
} catch (PDOException $e) {
throw new AirtimeDatabaseException("Couldn't establish a connection to the database! "
. "Please check your credentials and try again.",
array(
self::DB_NAME,
self::DB_USER,
self::DB_PASS,
));
} }
$this->writeToTemp(); $this->writeToTemp();
@ -178,7 +183,7 @@ class DatabaseSetup extends Setup {
*/ */
private function checkDatabaseEncoding() { private function checkDatabaseEncoding() {
$statement = self::$dbh->prepare("SELECT pg_encoding_to_char(encoding) " $statement = self::$dbh->prepare("SELECT pg_encoding_to_char(encoding) "
. "FROM pg_database WHERE datname = :dbname"); . "FROM pg_database WHERE datname = :dbname");
$statement->execute(array(":dbname" => $this->name)); $statement->execute(array(":dbname" => $this->name));
$encoding = $statement->fetch(); $encoding = $statement->fetch();
if (!($encoding && $encoding[0] == "UTF8")) { if (!($encoding && $encoding[0] == "UTF8")) {