Skip to content

Commit

Permalink
Updates for Postgres. Should now be able to setup a new install with …
Browse files Browse the repository at this point in the history
…postgres and update an old one.
  • Loading branch information
craigk5n committed Jan 23, 2024
1 parent 1ff3afa commit 0e4c04e
Show file tree
Hide file tree
Showing 10 changed files with 230 additions and 81 deletions.
46 changes: 41 additions & 5 deletions install/index.php
Expand Up @@ -63,17 +63,44 @@

function tryDbConnect()
{
global $settings, $db_database;
global $settings, $db_database, $db_connection, $debugInstaller;
if (!isset($settings['db_type']) || !isset($_SESSION['db_host']) || !isset($_SESSION['db_login']) || !isset($_SESSION['db_database'])) {
return false;
}
try {
// Don't require database to exist in mysqli
// Don't require database to exist in mysqli and postgres
if ($_SESSION['db_type'] == 'mysqli') {
$mysqli = new mysqli($_SESSION['db_host'], $_SESSION['db_login'], $_SESSION['db_password']);
if ($mysqli->connect_error) {
return false;
}
$db_connection = $mysqli;
return true;
} else if ( $_SESSION['db_type'] == 'postgresql') {
try {
$connString = "host=" . $_SESSION['db_host'] . " dbname=" . $_SESSION['db_database'] . " user=" . $_SESSION['db_login']
. " password=" . $_SESSION['db_password'];
$c = @pg_connect($connString);
if ($c) {
$db_connection = $c;
if ($debugInstaller)
echo "Successful Postgres connection to " . $_SESSION['db_database'] . "<br>";
return true;
} else {
if ($debugInstaller)
echo "First Postgres connection to " . $_SESSION['db_database'] . " FAILED<br>";
}
} catch (Exception $e) {
// We may have failed because the db has not been created yet. So try again with the 'postgres' database.
if(!$debugInstaller) {
echo "First db connect attempt failed. Trying postgres db instead.<br>";
}
}
$connString = "host=" . $_SESSION['db_host'] . " dbname=postgres user=" . $_SESSION['db_login'] . " password=" . $_SESSION['db_password'];
$c = @pg_connect($connString);
if (!$c) {
return false;
}
return true;
} else {
$c = @dbi_connect(
Expand All @@ -87,6 +114,9 @@ function tryDbConnect()
} catch (Exception $e) {
return false;
}
if ($c) {
$db_connection = $c;
}
return !empty($c);
}

Expand Down Expand Up @@ -261,17 +291,23 @@ function_exists('gd_info'),
phpinfo ();
exit;
}
$emptyDatabase = $canConnectDb ? isEmptyDatabase() : true;
$databaseExists = false;
$emptyDatabase = false;
try {
// Try checking if there is a webcal_config table. If there is, then the db exists.
$emptyDatabase = isEmptyDatabase();
} catch (Exception $e) {
// If we get an exception, then the db does not exist.
}
$unsavedDbSettings = !empty($_SESSION['unsavedDbSettings']); // Keep track if Db settings were modified by not yet saved
$reportedDbVersion = 'Unknown';
$adminUserCount = 0;
$databaseExists = false;
$databaseCurrent = false;
$settingsSaved = true; // True if a valid settings.php found unless user changes settings
$detectedDbVersion = 'Unknown';
if ($canConnectDb && !empty($db_connection)) {
$reportedDbVersion = getDbVersion();
$detectedDbVersion = getDatabaseVersionFromSchema();
$detectedDbVersion = getDatabaseVersionFromSchema(!$debugInstaller);
if ($debugInstaller) {
//echo "Db Version: $dbV <br>";
}
Expand Down
11 changes: 8 additions & 3 deletions install/install_ajax.php
Expand Up @@ -31,10 +31,15 @@ function testDbConnection($host, $login, $password, $database)
$ret = true;
$c->close();
} elseif ($_POST['dbType'] == 'postgresql') {
$c = pg_connect("host=$host dbname=$database user=$login password=$password");
$c = @pg_connect("host=$host dbname=$database user=$login password=$password");
$ret = ($c !== false);
$error_msg = pg_last_error($c);
pg_close($c);
if (!$ret) {
$c = @pg_connect("host=$host dbname=postgres user=$login password=$password");
$ret = ($c !== false);
}
if ($c) {
pg_close($c);
}
} elseif ($_POST['dbType'] == 'ibase') {
$c = ibase_connect($database, $login, $password);
$ret = ($c !== false);
Expand Down
12 changes: 6 additions & 6 deletions install/install_createdb.php
Expand Up @@ -16,9 +16,9 @@
</p>

<?php
if ($databaseExists) {
printNextPageButton($action);
} else {
printSubmitButton($action, null, translate('Create Database'));
}
?>
if ($databaseExists) {
printNextPageButton($action);
} else {
printSubmitButton($action, null, translate('Create Database'));
}
?>
29 changes: 26 additions & 3 deletions install/install_createdb_handler.php
@@ -1,9 +1,12 @@
<?php

// The dbi4php.php functions don't uniformly handle create database, so we have the code for it here.
$existsMessage = translate('Database XXX already exists.');
$createdMessage = translate('Created database XXX');

function createMysqlDatabase(string $hostname, string $login, string $password, string $databaseName): bool
{
global $existsMessage;
// Create connection without selecting a database
$conn = new mysqli($hostname, $login, $password);

Expand All @@ -16,6 +19,8 @@ function createMysqlDatabase(string $hostname, string $login, string $password,
$sql = "CREATE DATABASE " . $databaseName;
if ($conn->query($sql) === TRUE) {
$conn->close();
$existsMessage = str_replace("XXX", $databaseName, $existsMessage);
$_SESSION['alert'] = $existsMessage;
return true;
} else {
throw new Exception("Error creating database: " . $conn->error);
Expand All @@ -31,24 +36,42 @@ function createSqliteDatabase(string $filename): bool
$db->exec("DROP TABLE dummy");
$db->close();
return true;
// TODO: Implement this...
} catch (Exception $e) {
throw new Exception("Error creating SQLite3 database: " . $e->getMessage());
}
}

function createPostgresqlDatabase($hostname, $login, $password, $databaseName): bool
{
$connString = "host={$hostname} user={$login} password={$password}";
global $existsMessage, $createdMessage;
// Use specific query for existing database check (dbname=postgres)
$connString = "host={$hostname} dbname=postgres user={$login} password={$password}";
$db = pg_connect($connString);
if (!$db) {
throw new Exception("Connection failed: " . pg_last_error());
throw new Exception("Connection failed");
}
$existsQuery = "SELECT 1 FROM information_schema.schemata WHERE schema_name = $1";
$result = pg_query_params($db, $existsQuery, [$databaseName]);
if ($result) {
$row = pg_fetch_row($result);
if ($row && $row[0] === '1') {
// Database exists
pg_close($db);
$existsMessage = str_replace("XXX", $databaseName, $existsMessage);
$_SESSION['alert'] = $existsMessage;
return false;
} else {
// Database doesn't exist
}
}

$result = pg_query($db, "CREATE DATABASE {$databaseName}");
if (!$result) {
throw new Exception("Error creating database: " . pg_last_error($db));
}
pg_close($db);
$createdMessage = str_replace("XXX", $databaseName, $createdMessage);
$_SESSION['alert'] = $createdMessage;
return true;
}

Expand Down
8 changes: 7 additions & 1 deletion install/install_functions.php
Expand Up @@ -17,6 +17,9 @@ function isEmptyDatabase()
{
global $db_connection, $debugInstaller;
if (empty($db_connection)) {
if ($debugInstaller) {
echo "No connection => empty db<br>";
}
return true;
}
try {
Expand Down Expand Up @@ -499,14 +502,17 @@ function getDatabaseVersionFromSchema($silent = true)
for ($i = 0; $i < count($database_upgrade_matrix); $i++) {
$sql = $database_upgrade_matrix[$i][0];
if (!$silent) {
echo "SQL: $sql<br>\n";
echo "SQL: $sql<br>Success: " . ($success ? "true":"false") . "\n<br>";
}

if (empty($sql)) {
if ($success) {
// We reached the end of database_upgrade_matrix[] with no errors, which
// means the database is structurally up-to-date.
$dbVersion = $PROGRAM_VERSION;
if (!$silent) {
echo "Complete success: version $dbVersion<br>\n";
}
}
} else {
try {
Expand Down
3 changes: 2 additions & 1 deletion install/sql/tables-mysql.sql
Expand Up @@ -280,7 +280,8 @@ CREATE TABLE webcal_site_extras (
/* how many minutes before event should a reminder be sent */
cal_remind INT DEFAULT 0,
/* used to store text data */
cal_data TEXT
cal_data TEXT,
PRIMARY KEY (cal_id, cal_name)
);

/**
Expand Down
9 changes: 5 additions & 4 deletions install/sql/tables-postgres.sql
Expand Up @@ -94,7 +94,8 @@ CREATE TABLE webcal_site_extras (
cal_type INT NOT NULL,
cal_date INT DEFAULT '0',
cal_remind INT DEFAULT '0',
cal_data TEXT
cal_data TEXT,
PRIMARY KEY (cal_id, cal_name)
);
CREATE TABLE webcal_reminders (
cal_id INT DEFAULT '0' NOT NULL,
Expand Down Expand Up @@ -158,7 +159,7 @@ CREATE TABLE webcal_categories (
cat_status CHAR DEFAULT 'A',
cat_icon_mime VARCHAR(32) DEFAULT NULL,
cat_icon_blob BYTEA DEFAULT NULL,
PRIMARY KEY ( cat_id, cat_order, cat_owner )
PRIMARY KEY ( cat_id, cat_owner )
);
CREATE TABLE webcal_asst (
cal_boss VARCHAR(25) NOT NULL,
Expand All @@ -184,8 +185,6 @@ CREATE TABLE webcal_import (
cal_md5 VARCHAR(32) NULL DEFAULT NULL,
PRIMARY KEY ( cal_import_id )
);
CREATE INDEX webcal_import_data_type ON webcal_import_data(cal_import_type);
CREATE INDEX webcal_import_data_ext_id ON webcal_import_data(cal_external_id);
CREATE TABLE webcal_import_data (
cal_import_id INT NOT NULL,
cal_id INT NOT NULL,
Expand All @@ -194,6 +193,8 @@ CREATE TABLE webcal_import_data (
cal_external_id VARCHAR(200) NULL,
PRIMARY KEY ( cal_id, cal_login )
);
CREATE INDEX webcal_import_data_type ON webcal_import_data(cal_import_type);
CREATE INDEX webcal_import_data_ext_id ON webcal_import_data(cal_external_id);
CREATE TABLE webcal_report (
cal_login VARCHAR(25) NOT NULL,
cal_report_id INT NOT NULL,
Expand Down

0 comments on commit 0e4c04e

Please sign in to comment.