ENHANCEMENT Added PostgreSQLDatabaseConfigurationHelper::requireDatabaseVersion() for checking the database version is at least 8.3 during installation

This commit is contained in:
Sean Harvey 2010-05-15 04:03:25 +00:00
parent 113d75f825
commit 5f17db6599
1 changed files with 47 additions and 0 deletions

View File

@ -80,6 +80,53 @@ class PostgreSQLDatabaseConfigurationHelper implements DatabaseConfigurationHelp
);
}
/**
* Ensure that the PostgreSQL version is at least 8.3.
* @param array $databaseConfig Associative array of db configuration, e.g. "server", "username" etc
* @return array Result - e.g. array('success' => true, 'error' => 'details of error')
*/
public function requireDatabaseVersion($databaseConfig) {
$success = false;
$error = '';
$version = 0;
$username = $databaseConfig['username'] ? $databaseConfig['username'] : '';
$password = $databaseConfig['password'] ? $databaseConfig['password'] : '';
$server = $databaseConfig['server'];
$userPart = $username ? " user=$username" : '';
$passwordPart = $password ? " password=$password" : '';
$connstring = "host=$server port=5432 dbname=postgres {$userPart}{$passwordPart}";
$conn = @pg_connect($connstring);
$versionInfo = pg_version($conn);
$version = isset($versionInfo['server']) ? $versionInfo['server'] : null;
if(!$version) {
// fallback to using the version() function
$result = @pg_query($conn, "SELECT version()");
$row = @pg_fetch_array($result);
if($row && isset($row[0])) {
$parts = explode(' ', trim($row[0]));
// ASSUMPTION version number is the second part e.g. "PostgreSQL 8.4.3"
$version = trim($parts[1]);
}
}
if($version) {
$success = version_compare($version, '8.3', '>=');
if(!$success) {
$error = "Your PostgreSQL version is $version. It's recommended you use at least 8.3.";
}
} else {
$error = "Your PostgreSQL version could not be determined.";
}
return array(
'success' => $success,
'error' => $error
);
}
/**
* Ensure that the database connection is able to use an existing database,
* or be able to create one if it doesn't exist.