From 270e84e9d85ea22e296c622bab7ddd3e6a7735b7 Mon Sep 17 00:00:00 2001 From: Duncan Sommerville Date: Thu, 11 Dec 2014 15:54:55 -0500 Subject: [PATCH] Updated db setup to catch PDOExceptions --- airtime_mvc/public/setup/database-setup.php | 35 ++++++++++++--------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/airtime_mvc/public/setup/database-setup.php b/airtime_mvc/public/setup/database-setup.php index 769431778..6971501ee 100644 --- a/airtime_mvc/public/setup/database-setup.php +++ b/airtime_mvc/public/setup/database-setup.php @@ -41,15 +41,10 @@ class DatabaseSetup extends Setup { private function setNewDatabaseConnection($dbName) { 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(); if ($err[1] != null) { - throw new AirtimeDatabaseException("Couldn't establish a connection to the database!", - array( - self::DB_NAME, - self::DB_USER, - self::DB_PASS, - )); + throw new PDOException(); } } @@ -61,13 +56,23 @@ class DatabaseSetup extends Setup { * @throws AirtimeDatabaseException */ public function runSetup() { - $this->setNewDatabaseConnection("postgres"); - if ($this->checkDatabaseExists()) { - $this->installDatabaseTables(); - } else { - $this->checkUserCanCreateDb(); - $this->createDatabase(); - $this->installDatabaseTables(); + try { + $this->setNewDatabaseConnection("postgres"); + if ($this->checkDatabaseExists()) { + $this->installDatabaseTables(); + } else { + $this->checkUserCanCreateDb(); + $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(); @@ -178,7 +183,7 @@ class DatabaseSetup extends Setup { */ private function checkDatabaseEncoding() { $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)); $encoding = $statement->fetch(); if (!($encoding && $encoding[0] == "UTF8")) {