Skip to content

Commit

Permalink
drop function: db_connect_with_errors(); just throw from db_connect()…
Browse files Browse the repository at this point in the history
… if something goes wrong
  • Loading branch information
DavidGoodwin committed Jun 7, 2019
1 parent 87746e6 commit cc19870
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 30 deletions.
30 changes: 8 additions & 22 deletions functions.inc.php
Expand Up @@ -1464,29 +1464,14 @@ function smtp_get_response($fh) {
* @return \PDO
*/
function db_connect() {
list($link, $error_text) = db_connect_with_errors();

if (!$link instanceof PDO) {
throw new Exception("Database connection failed: $error_text");
}

return $link;
}

/**
* @param bool $ignore_errors
* @return array [PDO link | false, string $error_text];
*/
function db_connect_with_errors() {
global $CONF;
global $DEBUG_TEXT;

$error_text = '';

/* some attempt at not reopening an existing connection */
static $link;
if (isset($link) && $link) {
return array($link, $error_text);
return $link;
}

$link = false;

$options = array(
Expand Down Expand Up @@ -1520,17 +1505,17 @@ function db_connect_with_errors() {

if (!file_exists($db)) {
$error_text = 'SQLite database missing: '. $db;
return array($link, $error_text);
throw new Exception($error_text);
}

if (!is_writeable($db)) {
$error_text = 'SQLite database not writeable: '. $db;
return array($link, $error_text);
throw new Exception($error_text);
}

if (!is_writeable(dirname($db))) {
$error_text = 'The directory the SQLite database is in is not writeable: '. dirname($db);
return array($link, $error_text);
throw new Exception($error_text);
}

$dsn = "sqlite:{$db}";
Expand All @@ -1556,7 +1541,8 @@ function db_connect_with_errors() {
}
}

return array($link, $error_text);
return $link;

}

/**
Expand Down
10 changes: 9 additions & 1 deletion public/setup.php
Expand Up @@ -184,7 +184,15 @@
//
// Database connection
//
list($link, $error_text) = db_connect_with_errors();
$link = null;
$error_text = null;

try {
$link = db_connect();
}
catch(Exception $e) {
$error_text = $e->getMessage();
}

if (!empty($link) && $error_text == "") {
print "<li>Testing database connection (using {$CONF['database_type']}) - Success</li>";
Expand Down
14 changes: 7 additions & 7 deletions tests/bootstrap.php
Expand Up @@ -28,7 +28,7 @@
$user = getenv('PGUSER') ?: 'postgres';
$pass = getenv('PGPASSWORD') ?: '';
$host = getenv('PGHOST') ?: 'localhost';

$CONF['database_type'] = 'pgsql';
$CONF['database_user'] = $user;
$CONF['database_password'] = $pass;
Expand Down Expand Up @@ -56,7 +56,7 @@
$config = parse_ini_file($expand_tilde('~/.my.cnf'));

if (empty($config)) {
$config = ['user'=>'root', 'host' => '127.0.0.1', 'password' => ''];
$config = ['user' => 'root', 'host' => '127.0.0.1', 'password' => ''];
}

if (isset($config['socket'])) {
Expand All @@ -79,13 +79,13 @@
echo "Using: MySQL database for tests\n";
}


list($db, $error_text) = db_connect_with_errors();

if ($db === false) {
try {
$db = db_connect();
} catch (Exception $e) {
echo "failed to connect to database\n";
echo $error_text;
echo $e->getMessage();
exit(1);

}

require_once(dirname(__FILE__) . '/../public/upgrade.php');

0 comments on commit cc19870

Please sign in to comment.