summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorBrice Maron <brice@bmaron.net>2012-06-28 19:37:29 +0000
committerBrice Maron <brice@bmaron.net>2012-06-28 19:38:03 +0000
commitb2cbf1199ddf9802316a4508ee312490c6b88ddb (patch)
tree1ef120329a9e5ec553166cd0ca4cb1ee03c7eebf /lib
parentbdd1baeb858b4968a6b69b48960abe3b9f63bc92 (diff)
downloadnextcloud-server-b2cbf1199ddf9802316a4508ee312490c6b88ddb.tar.gz
nextcloud-server-b2cbf1199ddf9802316a4508ee312490c6b88ddb.zip
Pg setup enhancement
do not create a db if already existing .. and reset the user password instead of creating if the user already exists
Diffstat (limited to 'lib')
-rw-r--r--lib/setup.php34
1 files changed, 32 insertions, 2 deletions
diff --git a/lib/setup.php b/lib/setup.php
index 59c3aefbf13..5387a0ef493 100644
--- a/lib/setup.php
+++ b/lib/setup.php
@@ -285,13 +285,23 @@ class OC_Setup {
//we cant use OC_BD functions here because we need to connect as the administrative user.
$e_name = pg_escape_string($name);
$e_user = pg_escape_string($user);
- $query = "CREATE DATABASE \"$e_name\" OWNER \"$e_user\"";
+ $query = "select datname from pg_database where datname = '$e_name'";
$result = pg_query($connection, $query);
if(!$result) {
$entry='DB Error: "'.pg_last_error($connection).'"<br />';
$entry.='Offending command was: '.$query.'<br />';
echo($entry);
}
+ if(! pg_fetch_row($result)) {
+ //The database does not exists... let's create it
+ $query = "CREATE DATABASE \"$e_name\" OWNER \"$e_user\"";
+ $result = pg_query($connection, $query);
+ if(!$result) {
+ $entry='DB Error: "'.pg_last_error($connection).'"<br />';
+ $entry.='Offending command was: '.$query.'<br />';
+ echo($entry);
+ }
+ }
$query = "REVOKE ALL PRIVILEGES ON DATABASE \"$e_name\" FROM PUBLIC";
$result = pg_query($connection, $query);
}
@@ -299,13 +309,33 @@ class OC_Setup {
private static function pg_createDBUser($name,$password,$connection) {
$e_name = pg_escape_string($name);
$e_password = pg_escape_string($password);
- $query = "CREATE USER \"$e_name\" CREATEDB PASSWORD '$e_password';";
+ $query = "select * from pg_roles where rolname='$e_name';";
$result = pg_query($connection, $query);
if(!$result) {
$entry='DB Error: "'.pg_last_error($connection).'"<br />';
$entry.='Offending command was: '.$query.'<br />';
echo($entry);
}
+
+ if(! pg_fetch_row($result)) {
+ //user does not exists let's create it :)
+ $query = "CREATE USER \"$e_name\" CREATEDB PASSWORD '$e_password';";
+ $result = pg_query($connection, $query);
+ if(!$result) {
+ $entry='DB Error: "'.pg_last_error($connection).'"<br />';
+ $entry.='Offending command was: '.$query.'<br />';
+ echo($entry);
+ }
+ }
+ else { // change password of the existing role
+ $query = "ALTER ROLE \"$e_name\" WITH PASSWORD '$e_password';";
+ $result = pg_query($connection, $query);
+ if(!$result) {
+ $entry='DB Error: "'.pg_last_error($connection).'"<br />';
+ $entry.='Offending command was: '.$query.'<br />';
+ echo($entry);
+ }
+ }
}
/**