mainenance functionality to run database updates for new releases
parent
a32220442c
commit
199922299d
|
@ -367,5 +367,7 @@ return [
|
|||
// A list of individual files to include in analysis
|
||||
// with a path relative to the root directory of the
|
||||
// project.
|
||||
'file_list' => [],
|
||||
'file_list' => [
|
||||
'maintenance.php'
|
||||
],
|
||||
];
|
||||
|
|
|
@ -22,6 +22,8 @@ Needed SQL commands:
|
|||
create database federator;
|
||||
create user if not exists 'federator'@'localhost' identified by '*change*me*';
|
||||
grant select,insert,update,delete on federator.* to 'federator'@'localhost';
|
||||
create user if not exists 'federatoradmin'@'localhost' identified by '*change*me*as*well';
|
||||
grant all privileges on federator.* to 'federatoradmin'@'localhost';
|
||||
|
||||
This will be changed, but works for the current develop verison.
|
||||
|
||||
|
|
|
@ -11,3 +11,7 @@ compiledir = '../cache'
|
|||
[plugins]
|
||||
rediscache = '../plugins/rediscache.php'
|
||||
dummy = '../plugins/dummyconnector.php'
|
||||
|
||||
[maintenance]
|
||||
username = 'federatoradmin'
|
||||
password = '*change*me*as*well'
|
|
@ -0,0 +1,95 @@
|
|||
<?php
|
||||
/**
|
||||
* SPDX-FileCopyrightText: 2024 Sascha Nitsch (grumpydeveloper) https://contentnation.net/@grumpydevelop
|
||||
* SPDX-License-Identifier: GPL-3.0-or-later
|
||||
*
|
||||
* @author Sascha Nitsch (grumpydeveloper)
|
||||
**/
|
||||
|
||||
function dbupgrade(\Federator\Main $main)
|
||||
{
|
||||
$config = $main->getConfig();
|
||||
$maintenance = $config['maintenance'];
|
||||
$main->openDatabase($maintenance['username'], $maintenance['password']);
|
||||
$dbh = $main->getDatabase();
|
||||
if ($dbh === false) {
|
||||
die();
|
||||
}
|
||||
$sql = 'select `value` from settings where `key`="database_version"';
|
||||
try {
|
||||
$sth = $dbh->query($sql);
|
||||
} catch (mysqli_sql_exception) {
|
||||
$sth = false;
|
||||
}
|
||||
$version = '0';
|
||||
if ($sth instanceof mysqli_result) {
|
||||
$row = $sth->fetch_row();
|
||||
if (is_array($row)) {
|
||||
$version = $row[0];
|
||||
}
|
||||
}
|
||||
echo "current version: $version\n";
|
||||
$updateFolder = opendir("sql");
|
||||
if ($updateFolder === false) {
|
||||
die();
|
||||
}
|
||||
$updates = [];
|
||||
while (($file = readdir($updateFolder)) !== false) {
|
||||
$matches = [];
|
||||
if (preg_match('/^(\d{4}-\d{2}-\d{2}).sql$/', $file, $matches) == 1) {
|
||||
if (strcmp($matches[1], $version) > 0) { // file is newer than our version
|
||||
$updates[] = $file;
|
||||
}
|
||||
}
|
||||
}
|
||||
closedir($updateFolder);
|
||||
sort($updates);
|
||||
foreach ($updates as $update) {
|
||||
echo "applying update $update\n";
|
||||
$u = fopen('sql/' . $update, 'r');
|
||||
if ($u !== false) {
|
||||
try {
|
||||
$dbh->begin_transaction();
|
||||
// apply updates line by line
|
||||
while (($line = fgets($u)) !== false) {
|
||||
$line = trim($line);
|
||||
$dbh->query($line);
|
||||
}
|
||||
$dbh->commit();
|
||||
} catch (mysqli_sql_exception $e) {
|
||||
//print_r($e);
|
||||
echo "error while applying update $update: " . $e->getMessage() . "\n";
|
||||
}
|
||||
fclose($u);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function printUsage()
|
||||
{
|
||||
echo "usage php maintenance.php <command>\n";
|
||||
echo "command can be one of:\n";
|
||||
echo " dbupgrade - this upgrades the db to the most recent version. Run this after you updated the program files\n";
|
||||
exit();
|
||||
}
|
||||
|
||||
date_default_timezone_set("Europe/Berlin");
|
||||
spl_autoload_register(static function (string $className) {
|
||||
$root = array_key_exists('DOCUMENT_ROOT', $_SERVER) ? $_SERVER['DOCUMENT_ROOT'] : '';
|
||||
// strip Federator from class path
|
||||
$className = str_replace('Federator\\', '', $className);
|
||||
include $root . 'php/' . str_replace("\\", "/", strtolower($className)) . '.php';
|
||||
});
|
||||
|
||||
if ($argc < 2) {
|
||||
printUsage();
|
||||
}
|
||||
|
||||
$main = new \Federator\Main('');
|
||||
switch ($argv[1]) {
|
||||
case 'dbupgrade':
|
||||
dbupgrade($main);
|
||||
break;
|
||||
default:
|
||||
printUsage();
|
||||
}
|
30
php/main.php
30
php/main.php
|
@ -3,6 +3,7 @@
|
|||
/**
|
||||
* SPDX-FileCopyrightText: 2024 Sascha Nitsch (grumpydeveloper) https://contentnation.net/@grumpydevelop
|
||||
* SPDX-License-Identifier: GPL-3.0-or-later
|
||||
*
|
||||
* @author Author: Sascha Nitsch (grumpydeveloper)
|
||||
**/
|
||||
|
||||
|
@ -77,11 +78,13 @@ class Main
|
|||
|
||||
/**
|
||||
* constructor
|
||||
*
|
||||
* @param string $rootDir root directory
|
||||
*/
|
||||
public function __construct()
|
||||
public function __construct($rootDir = '../')
|
||||
{
|
||||
$this->responseCode = 200;
|
||||
$config = parse_ini_file('../config.ini', true);
|
||||
$config = parse_ini_file($rootDir . 'config.ini', true);
|
||||
if ($config !== false) {
|
||||
$this->config = $config;
|
||||
}
|
||||
|
@ -122,6 +125,16 @@ class Main
|
|||
return $this->config;
|
||||
}
|
||||
|
||||
/**
|
||||
* get database handle
|
||||
*
|
||||
* @return \mysqli|false database handle
|
||||
*/
|
||||
public function getDatabase()
|
||||
{
|
||||
return $this->dbh;
|
||||
}
|
||||
|
||||
/**
|
||||
* load plugins
|
||||
*/
|
||||
|
@ -141,11 +154,20 @@ class Main
|
|||
|
||||
/**
|
||||
* open database
|
||||
*
|
||||
* @param string|null $usernameOverride optional username override
|
||||
* @param string|null $passwordOverride optional password override
|
||||
* @return void
|
||||
*/
|
||||
public function openDatabase() : void
|
||||
public function openDatabase($usernameOverride = null, $passwordOverride = null)
|
||||
{
|
||||
$dbconf = $this->config["database"];
|
||||
$this->dbh = new \mysqli($dbconf['host'], $dbconf['username'], $dbconf['password'], $dbconf['database']);
|
||||
$this->dbh = new \mysqli(
|
||||
$dbconf['host'],
|
||||
$usernameOverride ?? (string)$dbconf['username'],
|
||||
$passwordOverride ?? (string)$dbconf['password'],
|
||||
$dbconf['database']
|
||||
);
|
||||
if ($this->dbh->connect_error !== null) {
|
||||
http_response_code(500);
|
||||
die('Database Connect Error');
|
||||
|
|
|
@ -0,0 +1,2 @@
|
|||
create table settings(`key` varchar(255) unique primary key, `value` text);
|
||||
insert into settings (`key`, `value`) value ("database_version", "2024-08-19");
|
Loading…
Reference in New Issue