2022-12-02 23:55:58 +01:00
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Upgrader using git for file upgrade and mustal to update the database definition
|
|
|
|
|
*
|
|
|
|
|
* Copyright (c) 2022 OpenXE project
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
2022-12-06 21:05:41 +01:00
|
|
|
|
$upgrade_echo_out_file_name = "";
|
|
|
|
|
|
|
|
|
|
function upgrade_set_out_file_name(string $filename) {
|
|
|
|
|
|
|
|
|
|
GLOBAL $upgrade_echo_out_file_name;
|
|
|
|
|
|
|
|
|
|
$upgrade_echo_out_file_name = $filename;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function echo_out(string $text) {
|
|
|
|
|
|
|
|
|
|
GLOBAL $upgrade_echo_out_file_name;
|
|
|
|
|
|
|
|
|
|
if ($upgrade_echo_out_file_name == "") {
|
|
|
|
|
echo($text);
|
|
|
|
|
} else {
|
|
|
|
|
file_put_contents($upgrade_echo_out_file_name,$text, FILE_APPEND);
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-12-06 17:55:31 +01:00
|
|
|
|
|
2022-12-05 18:11:32 +01:00
|
|
|
|
function echo_output(array $output) {
|
2022-12-06 21:05:41 +01:00
|
|
|
|
echo_out(implode("\n",$output)."\n");
|
2022-12-05 18:11:32 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function abort(string $message) {
|
2022-12-06 21:05:41 +01:00
|
|
|
|
echo_out($message."\n");
|
|
|
|
|
echo_out("--------------- Aborted! ---------------\n");
|
|
|
|
|
echo_out("--------------- ".date("Y-m-d H:i:s")." ---------------\n");
|
2022-12-05 18:11:32 +01:00
|
|
|
|
}
|
|
|
|
|
|
2022-12-06 21:05:41 +01:00
|
|
|
|
function git(string $command, &$output, bool $show_command, bool $show_output, string $error_text) : int {
|
2022-12-02 23:55:58 +01:00
|
|
|
|
$output = array();
|
2022-12-06 21:05:41 +01:00
|
|
|
|
if ($show_command) {
|
|
|
|
|
echo_out("git ".$command."\n");
|
2022-12-02 23:55:58 +01:00
|
|
|
|
}
|
|
|
|
|
exec("git ".$command,$output,$retval);
|
2022-12-03 17:27:00 +01:00
|
|
|
|
if (!empty($output)) {
|
2022-12-06 21:05:41 +01:00
|
|
|
|
if ($show_output || $retval != 0) {
|
2022-12-03 17:27:00 +01:00
|
|
|
|
echo_output($output);
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-12-13 19:23:37 +01:00
|
|
|
|
if ($retval != 0) {
|
|
|
|
|
echo_out($error_text."\n");
|
|
|
|
|
}
|
2022-12-02 23:55:58 +01:00
|
|
|
|
return($retval);
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-05 18:11:32 +01:00
|
|
|
|
// -------------------------------- START
|
2022-12-06 15:43:02 +01:00
|
|
|
|
|
2022-12-05 18:11:32 +01:00
|
|
|
|
// Check for correct call method
|
|
|
|
|
if (php_sapi_name() == "cli") {
|
2022-12-13 19:23:37 +01:00
|
|
|
|
|
|
|
|
|
$directory = getcwd();
|
|
|
|
|
if (basename($directory) != 'upgrade') {
|
|
|
|
|
abort("Must be executed from 'upgrade' directory.");
|
|
|
|
|
return(-1);
|
|
|
|
|
}
|
2022-12-05 18:11:32 +01:00
|
|
|
|
|
2022-12-06 17:55:31 +01:00
|
|
|
|
$check_git = false;
|
|
|
|
|
$do_git = false;
|
|
|
|
|
$check_db = false;
|
|
|
|
|
$do_db = false;
|
|
|
|
|
$do = false;
|
|
|
|
|
|
|
|
|
|
if ($argc > 1) {
|
2022-12-06 15:43:02 +01:00
|
|
|
|
|
|
|
|
|
if (in_array('-v', $argv)) {
|
|
|
|
|
$verbose = true;
|
|
|
|
|
} else {
|
|
|
|
|
$verbose = false;
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-08 15:01:32 +01:00
|
|
|
|
if (in_array('-e', $argv)) {
|
|
|
|
|
$export_db = true;
|
|
|
|
|
} else {
|
|
|
|
|
$export_db = false;
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-06 15:43:02 +01:00
|
|
|
|
if (in_array('-f', $argv)) {
|
|
|
|
|
$force = true;
|
|
|
|
|
} else {
|
|
|
|
|
$force = false;
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-14 16:18:06 +01:00
|
|
|
|
if (in_array('-o', $argv)) {
|
|
|
|
|
$origin = true;
|
|
|
|
|
} else {
|
|
|
|
|
$origin = false;
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-09 08:13:12 +01:00
|
|
|
|
if (in_array('-connection', $argv)) {
|
|
|
|
|
$connection = true;
|
|
|
|
|
} else {
|
|
|
|
|
$connection = false;
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-06 17:55:31 +01:00
|
|
|
|
if (in_array('-s', $argv)) {
|
|
|
|
|
$check_git = true;
|
2022-12-06 15:43:02 +01:00
|
|
|
|
} else {
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-06 17:55:31 +01:00
|
|
|
|
if (in_array('-db', $argv)) {
|
|
|
|
|
$check_db = true;
|
|
|
|
|
} else {
|
|
|
|
|
}
|
2022-12-09 08:13:12 +01:00
|
|
|
|
|
2022-12-06 17:55:31 +01:00
|
|
|
|
if (in_array('-do', $argv)) {
|
|
|
|
|
if (!$check_git && !$check_db) {
|
|
|
|
|
$do_git = true;
|
|
|
|
|
$do_db = true;
|
|
|
|
|
}
|
|
|
|
|
if ($check_git) {
|
|
|
|
|
$do_git = true;
|
|
|
|
|
}
|
|
|
|
|
if ($check_db) {
|
|
|
|
|
$do_db = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($check_git || $check_db || $do_git || $do_db) {
|
2022-12-14 16:18:06 +01:00
|
|
|
|
upgrade_main($directory,$verbose,$check_git,$do_git,$export_db,$check_db,$do_db,$force,$connection,$origin);
|
2022-12-06 15:43:02 +01:00
|
|
|
|
} else {
|
|
|
|
|
info();
|
|
|
|
|
}
|
2022-12-05 18:11:32 +01:00
|
|
|
|
|
|
|
|
|
} else {
|
2022-12-06 15:43:02 +01:00
|
|
|
|
info();
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-05 18:11:32 +01:00
|
|
|
|
}
|
|
|
|
|
// -------------------------------- END
|
2022-12-02 23:55:58 +01:00
|
|
|
|
|
2022-12-14 16:18:06 +01:00
|
|
|
|
function upgrade_main(string $directory,bool $verbose, bool $check_git, bool $do_git, bool $export_db, bool $check_db, bool $do_db, bool $force, bool $connection, bool $origin) {
|
2022-12-13 19:23:37 +01:00
|
|
|
|
|
|
|
|
|
$mainfolder = dirname($directory);
|
2022-12-05 18:11:32 +01:00
|
|
|
|
$datafolder = $directory."/data";
|
|
|
|
|
$lockfile_name = $datafolder."/.in_progress.flag";
|
|
|
|
|
$remote_file_name = $datafolder."/remote.json";
|
|
|
|
|
$schema_file_name = "db_schema.json";
|
2022-12-02 23:55:58 +01:00
|
|
|
|
|
2022-12-06 21:05:41 +01:00
|
|
|
|
echo_out("--------------- OpenXE upgrade ---------------\n");
|
|
|
|
|
echo_out("--------------- ".date("Y-m-d H:i:s")." ---------------\n");
|
2022-12-02 23:55:58 +01:00
|
|
|
|
|
2022-12-06 15:43:02 +01:00
|
|
|
|
//require_once($directory.'/../cronjobs/githash.php');
|
2022-12-03 17:27:00 +01:00
|
|
|
|
|
2022-12-14 16:18:06 +01:00
|
|
|
|
if ($origin) {
|
|
|
|
|
$remote_info = array('host' => 'origin','branch' => 'master');
|
|
|
|
|
} else {
|
|
|
|
|
$remote_info_contents = file_get_contents($remote_file_name);
|
|
|
|
|
if (!$remote_info_contents) {
|
|
|
|
|
abort("Unable to load $remote_file_name");
|
|
|
|
|
return(-1);
|
|
|
|
|
}
|
|
|
|
|
$remote_info = json_decode($remote_info_contents, true);
|
|
|
|
|
}
|
2022-12-05 18:11:32 +01:00
|
|
|
|
|
2022-12-06 17:55:31 +01:00
|
|
|
|
if ($check_git || $do_git) {
|
2022-12-02 23:55:58 +01:00
|
|
|
|
|
2022-12-14 13:38:16 +01:00
|
|
|
|
$retval = git("log HEAD --", $output,$verbose,false,"");
|
2022-12-06 17:55:31 +01:00
|
|
|
|
// Not a git repository -> Create it and then go ahead
|
2022-12-13 19:23:37 +01:00
|
|
|
|
if ($retval == 128) {
|
|
|
|
|
if (!$do_git) {
|
|
|
|
|
abort("Git not initialized, use -do to initialize.");
|
|
|
|
|
return(-1);
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-06 21:05:41 +01:00
|
|
|
|
echo_out("Setting up git...");
|
2022-12-14 13:38:16 +01:00
|
|
|
|
$retval = git("init $mainfolder", $output,$verbose,$verbose,"Error while initializing git!");
|
2022-12-06 17:55:31 +01:00
|
|
|
|
if ($retval != 0) {
|
|
|
|
|
abort("");
|
|
|
|
|
return(-1);
|
|
|
|
|
}
|
2022-12-14 13:38:16 +01:00
|
|
|
|
$retval = git("add $mainfolder", $output,$verbose,$verbose,"Error while initializing git!");
|
2022-12-06 17:55:31 +01:00
|
|
|
|
if ($retval != 0) {
|
|
|
|
|
abort("");
|
|
|
|
|
return(-1);
|
|
|
|
|
}
|
2022-12-06 21:05:41 +01:00
|
|
|
|
$retval = git("fetch ".$remote_info['host']." ".$remote_info['branch'],$output,$verbose,$verbose,"Error while initializing git!");
|
2022-12-06 17:55:31 +01:00
|
|
|
|
if ($retval != 0) {
|
|
|
|
|
abort("");
|
|
|
|
|
return(-1);
|
|
|
|
|
}
|
2022-12-02 23:55:58 +01:00
|
|
|
|
|
2022-12-14 13:38:16 +01:00
|
|
|
|
$retval = git("checkout FETCH_HEAD -f --", $output,$verbose,$verbose,"Error while initializing git!");
|
2022-12-06 17:55:31 +01:00
|
|
|
|
if ($retval != 0) {
|
|
|
|
|
abort("");
|
|
|
|
|
return(-1);
|
|
|
|
|
}
|
|
|
|
|
} else if ($retval != 0) {
|
|
|
|
|
abort("Error while executing git!");
|
2022-12-06 15:43:02 +01:00
|
|
|
|
return(-1);
|
|
|
|
|
}
|
2022-12-13 19:23:37 +01:00
|
|
|
|
|
|
|
|
|
// Get changed files on system -> Should be empty
|
|
|
|
|
$modified_files = false;
|
|
|
|
|
$output = array();
|
2022-12-14 13:38:16 +01:00
|
|
|
|
$retval = git("ls-files -m $mainfolder", $output,$verbose,false,"Error while checking Git status.");
|
2022-12-13 19:23:37 +01:00
|
|
|
|
if (!empty($output)) {
|
|
|
|
|
$modified_files = true;
|
|
|
|
|
echo_out("There are modified files:\n");
|
|
|
|
|
echo_output($output);
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-06 17:55:31 +01:00
|
|
|
|
if ($verbose) {
|
2022-12-06 21:05:41 +01:00
|
|
|
|
echo_out("--------------- Upgrade history ---------------\n");
|
2022-12-14 13:38:16 +01:00
|
|
|
|
$retval = git("log --date=short-local --pretty=\"%cd (%h): %s\" HEAD --not HEAD~5 --",$output,$verbose,$verbose,"Error while showing history!");
|
2022-12-06 17:55:31 +01:00
|
|
|
|
if ($retval != 0) {
|
|
|
|
|
abort("");
|
2022-12-05 18:11:32 +01:00
|
|
|
|
return(-1);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2022-12-06 21:05:41 +01:00
|
|
|
|
echo_out("--------------- Current version ---------------\n");
|
2022-12-14 13:38:16 +01:00
|
|
|
|
$retval = git("log -1 --date=short-local --pretty=\"%cd (%h): %s\" HEAD --",$output,$verbose,true,"Error while showing history!");
|
2022-12-06 17:55:31 +01:00
|
|
|
|
if ($retval != 0) {
|
|
|
|
|
return(-1);
|
|
|
|
|
}
|
2022-12-05 18:11:32 +01:00
|
|
|
|
}
|
2022-12-02 23:55:58 +01:00
|
|
|
|
|
2022-12-06 17:55:31 +01:00
|
|
|
|
if ($do_git) {
|
|
|
|
|
|
|
|
|
|
if ($modified_files && !$force) {
|
|
|
|
|
abort("Clear modified files or use -f");
|
|
|
|
|
return(-1);
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-06 21:05:41 +01:00
|
|
|
|
echo_out("--------------- Pulling files... ---------------\n");
|
2022-12-06 17:55:31 +01:00
|
|
|
|
|
|
|
|
|
if ($force) {
|
2022-12-06 21:05:41 +01:00
|
|
|
|
$retval = git("reset --hard",$output,$verbose,$verbose,"Error while resetting modified files!");
|
2022-12-06 17:55:31 +01:00
|
|
|
|
if ($retval != 0) {
|
2022-12-06 21:05:41 +01:00
|
|
|
|
abort("");
|
2022-12-06 17:55:31 +01:00
|
|
|
|
return(-1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-06 21:05:41 +01:00
|
|
|
|
$retval = git("pull ".$remote_info['host']." ".$remote_info['branch'],$output,$verbose,$verbose,"Error while pulling files!");
|
2022-12-06 17:55:31 +01:00
|
|
|
|
if ($retval != 0) {
|
2022-12-06 21:05:41 +01:00
|
|
|
|
abort("");
|
2022-12-06 17:55:31 +01:00
|
|
|
|
return(-1);
|
|
|
|
|
}
|
2022-12-05 18:11:32 +01:00
|
|
|
|
|
2022-12-06 21:05:41 +01:00
|
|
|
|
$retval = git("reset --hard",$output,$verbose,$verbose,"Error while applying files!");
|
2022-12-05 18:11:32 +01:00
|
|
|
|
if ($retval != 0) {
|
2022-12-06 21:05:41 +01:00
|
|
|
|
abort("");
|
2022-12-05 18:11:32 +01:00
|
|
|
|
return(-1);
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-06 21:05:41 +01:00
|
|
|
|
echo_out("--------------- Files upgrade completed ---------------\n");
|
|
|
|
|
$retval = git("log -1 ",$output,$verbose,$verbose,"Error while checking files!");
|
2022-12-06 17:55:31 +01:00
|
|
|
|
if ($retval != 0) {
|
2022-12-06 21:05:41 +01:00
|
|
|
|
abort("");
|
2022-12-06 17:55:31 +01:00
|
|
|
|
return(-1);
|
|
|
|
|
}
|
2022-12-05 18:11:32 +01:00
|
|
|
|
echo_output($output);
|
2022-12-06 17:55:31 +01:00
|
|
|
|
} // $do_git
|
|
|
|
|
else { // Dry run
|
2022-12-06 21:05:41 +01:00
|
|
|
|
echo_out("--------------- Dry run, use -do to upgrade ---------------\n");
|
|
|
|
|
echo_out("--------------- Fetching files... ---------------\n");
|
2022-12-05 18:11:32 +01:00
|
|
|
|
|
2022-12-06 21:05:41 +01:00
|
|
|
|
$retval = git("fetch ".$remote_info['host']." ".$remote_info['branch'],$output,$verbose,$verbose,"Error while fetching files!");
|
2022-12-06 17:55:31 +01:00
|
|
|
|
if ($retval != 0) {
|
|
|
|
|
abort("");
|
|
|
|
|
}
|
2022-12-05 18:11:32 +01:00
|
|
|
|
|
2022-12-06 21:05:41 +01:00
|
|
|
|
echo_out("--------------- Pending upgrades: ---------------\n");
|
2022-12-02 23:55:58 +01:00
|
|
|
|
|
2022-12-06 21:05:41 +01:00
|
|
|
|
$retval = git("log --date=short-local --pretty=\"%cd (%h): %s\" FETCH_HEAD --not HEAD",$output,$verbose,true,"Error while fetching files!");
|
2022-12-06 17:55:31 +01:00
|
|
|
|
if (empty($output)) {
|
2022-12-06 21:05:41 +01:00
|
|
|
|
echo_out("No upgrades pending.\n");
|
2022-12-06 17:55:31 +01:00
|
|
|
|
}
|
|
|
|
|
if ($retval != 0) {
|
|
|
|
|
abort("");
|
|
|
|
|
}
|
|
|
|
|
} // Dry run
|
|
|
|
|
} // $check_git
|
|
|
|
|
|
2022-12-08 15:01:32 +01:00
|
|
|
|
if ($check_db || $do_db || $export_db) {
|
2022-12-13 19:23:37 +01:00
|
|
|
|
|
|
|
|
|
if ($connection) {
|
|
|
|
|
$connection_file_name = $directory."/data/connection.json";
|
|
|
|
|
$connection_file_contents = file_get_contents($connection_file_name);
|
|
|
|
|
if (!$connection_file_contents) {
|
|
|
|
|
abort("Unable to load $connection_file_name");
|
|
|
|
|
return(-1);
|
|
|
|
|
}
|
|
|
|
|
$connection_info = json_decode($connection_file_contents, true);
|
|
|
|
|
|
|
|
|
|
$host = $connection_info['host'];
|
|
|
|
|
$user = $connection_info['user'];
|
|
|
|
|
$passwd = $connection_info['passwd'];
|
|
|
|
|
$schema = $connection_info['schema'];
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
|
|
class DatabaseConnectionInfo {
|
|
|
|
|
function __construct($dir) {
|
|
|
|
|
require($dir."/../conf/user.inc.php");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$dbci = new DatabaseConnectionInfo($directory);
|
|
|
|
|
|
|
|
|
|
$host = $dbci->WFdbhost;
|
|
|
|
|
$user = $dbci->WFdbuser;
|
|
|
|
|
$passwd = $dbci->WFdbpass;
|
|
|
|
|
$schema = $dbci->WFdbname;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
require_once($directory.'/../vendor/mustal/mustal_mysql_upgrade_tool.php');
|
|
|
|
|
|
2022-12-06 21:05:41 +01:00
|
|
|
|
echo_out("--------------- Loading from database '$schema@$host'... ---------------\n");
|
2022-12-05 18:11:32 +01:00
|
|
|
|
$db_def = mustal_load_tables_from_db($host, $schema, $user, $passwd, $mustal_replacers);
|
2022-12-02 23:55:58 +01:00
|
|
|
|
|
2022-12-05 18:11:32 +01:00
|
|
|
|
if (empty($db_def)) {
|
2022-12-08 15:01:32 +01:00
|
|
|
|
echo_out("Could not load from $schema@$host\n");
|
2022-12-05 18:11:32 +01:00
|
|
|
|
exit;
|
|
|
|
|
}
|
2022-12-08 15:01:32 +01:00
|
|
|
|
|
|
|
|
|
if ($export_db) {
|
|
|
|
|
$export_file_name = "exported_db_schema.json";
|
|
|
|
|
if (mustal_save_tables_to_json($db_def, $datafolder, $export_file_name, true) == 0) {
|
|
|
|
|
echo_out("Database exported to $datafolder/$export_file_name\n");
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
echo_out("Could not export database to $datafolder/$export_file_name\n");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-05 18:11:32 +01:00
|
|
|
|
$compare_differences = array();
|
2022-12-02 23:55:58 +01:00
|
|
|
|
|
2022-12-06 21:05:41 +01:00
|
|
|
|
echo_out("--------------- Loading from JSON... ---------------\n");
|
2022-12-05 18:11:32 +01:00
|
|
|
|
$compare_def = mustal_load_tables_from_json($datafolder, $schema_file_name);
|
2022-12-02 23:55:58 +01:00
|
|
|
|
|
2022-12-05 18:11:32 +01:00
|
|
|
|
if (empty($compare_def)) {
|
|
|
|
|
abort("Could not load from JSON $schema_file_name\n");
|
|
|
|
|
return(-1);
|
|
|
|
|
}
|
2022-12-08 15:01:32 +01:00
|
|
|
|
echo_out("Table count database ".count($db_def['tables'])." vs. JSON ".count($compare_def['tables'])."\n");
|
|
|
|
|
echo_out("--------------- Comparing JSON '".$compare_def['database']."@".$compare_def['host']."' vs. database '$schema@$host' ---------------\n");
|
|
|
|
|
$compare_differences = mustal_compare_table_array($db_def,"in DB",$compare_def,"in JSON",false,true);
|
|
|
|
|
if ($verbose) {
|
2022-12-06 17:55:31 +01:00
|
|
|
|
foreach ($compare_differences as $compare_difference) {
|
|
|
|
|
$comma = "";
|
|
|
|
|
foreach ($compare_difference as $key => $value) {
|
2022-12-06 21:05:41 +01:00
|
|
|
|
echo_out($comma."$key => [$value]");
|
2022-12-06 17:55:31 +01:00
|
|
|
|
$comma = ", ";
|
|
|
|
|
}
|
2022-12-06 21:05:41 +01:00
|
|
|
|
echo_out("\n");
|
2022-12-06 17:55:31 +01:00
|
|
|
|
}
|
2022-12-06 15:43:02 +01:00
|
|
|
|
}
|
2022-12-08 15:01:32 +01:00
|
|
|
|
echo_out((empty($compare_differences)?0:count($compare_differences))." differences.\n");
|
2022-12-06 15:43:02 +01:00
|
|
|
|
|
2022-12-08 15:01:32 +01:00
|
|
|
|
echo_out("--------------- Comparing database '$schema@$host' vs. JSON '".$compare_def['database']."@".$compare_def['host']."' ---------------\n");
|
|
|
|
|
$compare_differences = mustal_compare_table_array($compare_def,"in JSON",$db_def,"in DB",true,true);
|
|
|
|
|
if ($verbose) {
|
|
|
|
|
foreach ($compare_differences as $compare_difference) {
|
|
|
|
|
$comma = "";
|
|
|
|
|
foreach ($compare_difference as $key => $value) {
|
|
|
|
|
echo_out($comma."$key => [$value]");
|
|
|
|
|
$comma = ", ";
|
|
|
|
|
}
|
|
|
|
|
echo_out("\n");
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-12-06 21:05:41 +01:00
|
|
|
|
echo_out((empty($compare_differences)?0:count($compare_differences))." differences.\n");
|
2022-12-02 23:55:58 +01:00
|
|
|
|
|
2022-12-06 21:05:41 +01:00
|
|
|
|
echo_out("--------------- Calculating database upgrade for '$schema@$host'... ---------------\n");
|
2022-12-02 23:55:58 +01:00
|
|
|
|
|
2022-12-05 18:11:32 +01:00
|
|
|
|
$upgrade_sql = array();
|
|
|
|
|
$result = mustal_calculate_db_upgrade($compare_def, $db_def, $upgrade_sql, $mustal_replacers);
|
2022-12-02 23:55:58 +01:00
|
|
|
|
|
2022-12-06 13:13:14 +01:00
|
|
|
|
if (!empty($result)) {
|
|
|
|
|
abort(count($result)." errors.\n");
|
|
|
|
|
if ($verbose) {
|
|
|
|
|
foreach($result as $error) {
|
2022-12-08 15:01:32 +01:00
|
|
|
|
echo_out("Code: ".$error[0]." '".$error[1]."'\n");
|
2022-12-06 13:13:14 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2022-12-05 18:11:32 +01:00
|
|
|
|
return(-1);
|
|
|
|
|
}
|
2022-12-02 23:55:58 +01:00
|
|
|
|
|
2022-12-06 17:55:31 +01:00
|
|
|
|
if ($verbose) {
|
|
|
|
|
foreach($upgrade_sql as $statement) {
|
2022-12-06 21:05:41 +01:00
|
|
|
|
echo_out($statement."\n");
|
2022-12-06 17:55:31 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-06 21:05:41 +01:00
|
|
|
|
echo_out(count($upgrade_sql)." upgrade statements\n");
|
2022-12-02 23:55:58 +01:00
|
|
|
|
|
2022-12-06 17:55:31 +01:00
|
|
|
|
if ($do_db) {
|
2022-12-06 21:05:41 +01:00
|
|
|
|
echo_out("--------------- Executing database upgrade for '$schema@$host' database... ---------------\n");
|
2022-12-06 17:55:31 +01:00
|
|
|
|
// First get the contents of the database table structure
|
|
|
|
|
$mysqli = mysqli_connect($host, $user, $passwd, $schema);
|
2022-12-02 23:55:58 +01:00
|
|
|
|
|
2022-12-06 17:55:31 +01:00
|
|
|
|
/* Check if the connection succeeded */
|
|
|
|
|
if (!$mysqli) {
|
|
|
|
|
echo ("Failed to connect!\n");
|
|
|
|
|
} else {
|
2022-12-02 23:55:58 +01:00
|
|
|
|
|
2022-12-06 17:55:31 +01:00
|
|
|
|
$counter = 0;
|
|
|
|
|
$error_counter = 0;
|
|
|
|
|
$number_of_statements = count($upgrade_sql);
|
2022-12-02 23:55:58 +01:00
|
|
|
|
|
2022-12-06 17:55:31 +01:00
|
|
|
|
foreach ($upgrade_sql as $sql) {
|
2022-12-02 23:55:58 +01:00
|
|
|
|
|
2022-12-06 17:55:31 +01:00
|
|
|
|
$counter++;
|
2022-12-06 21:05:41 +01:00
|
|
|
|
echo_out("\rUpgrade step $counter of $number_of_statements... ");
|
2022-12-02 23:55:58 +01:00
|
|
|
|
|
2022-12-06 17:55:31 +01:00
|
|
|
|
$query_result = mysqli_query($mysqli, $sql);
|
|
|
|
|
if (!$query_result) {
|
|
|
|
|
$error = " not ok: ". mysqli_error($mysqli);
|
2022-12-06 21:05:41 +01:00
|
|
|
|
echo_out($error);
|
|
|
|
|
echo_out("\n");
|
2022-12-06 17:55:31 +01:00
|
|
|
|
// file_put_contents("./errors.txt",date()." ".$error.$sql."\n",FILE_APPEND);
|
|
|
|
|
$error_counter++;
|
|
|
|
|
} else {
|
2022-12-06 21:05:41 +01:00
|
|
|
|
echo_out("ok.\r");
|
2022-12-06 17:55:31 +01:00
|
|
|
|
}
|
2022-12-02 23:55:58 +01:00
|
|
|
|
|
2022-12-06 17:55:31 +01:00
|
|
|
|
}
|
2022-12-02 23:55:58 +01:00
|
|
|
|
|
2022-12-06 21:05:41 +01:00
|
|
|
|
echo_out("\n");
|
|
|
|
|
echo_out("$error_counter errors.\n");
|
2022-12-06 17:55:31 +01:00
|
|
|
|
if ($error_counter > 0) {
|
2022-12-06 21:05:41 +01:00
|
|
|
|
// echo_out("See 'errors.txt'\n");
|
2022-12-06 17:55:31 +01:00
|
|
|
|
}
|
2022-12-02 23:55:58 +01:00
|
|
|
|
|
2022-12-06 21:05:41 +01:00
|
|
|
|
echo_out("--------------- Checking database upgrade for '$schema@$host'... ---------------\n");
|
2022-12-06 17:55:31 +01:00
|
|
|
|
$db_def = mustal_load_tables_from_db($host, $schema, $user, $passwd, $mustal_replacers);
|
|
|
|
|
|
2022-12-06 21:05:41 +01:00
|
|
|
|
echo_out("--------------- Comparing database '$schema@$host' vs. JSON '".$compare_def['database']."@".$compare_def['host']."' ---------------\n");
|
2022-12-06 17:55:31 +01:00
|
|
|
|
$compare_differences = mustal_compare_table_array($compare_def,"in JSON",$db_def,"in DB",true,true);
|
2022-12-06 21:05:41 +01:00
|
|
|
|
echo_out((empty($compare_differences)?0:count($compare_differences))." differences.\n");
|
2022-12-06 17:55:31 +01:00
|
|
|
|
}
|
|
|
|
|
} // $do_db
|
|
|
|
|
} // $check_db
|
|
|
|
|
|
|
|
|
|
/*
|
2022-12-06 21:05:41 +01:00
|
|
|
|
echo_out("--------------- Locking system ---------------\n");
|
2022-12-06 17:55:31 +01:00
|
|
|
|
if (file_exists($lockfile_name)) {
|
2022-12-06 21:05:41 +01:00
|
|
|
|
echo_out("System is already locked.\n");
|
2022-12-06 17:55:31 +01:00
|
|
|
|
} else {
|
|
|
|
|
file_put_contents($lockfile_name," ");
|
2022-12-05 18:11:32 +01:00
|
|
|
|
}
|
2022-12-02 23:55:58 +01:00
|
|
|
|
|
2022-12-06 21:05:41 +01:00
|
|
|
|
echo_out("--------------- Unlocking system ---------------\n");
|
2022-12-06 17:55:31 +01:00
|
|
|
|
unlink($lockfile_name);
|
|
|
|
|
*/
|
|
|
|
|
|
2022-12-06 21:05:41 +01:00
|
|
|
|
echo_out("--------------- Done! ---------------\n");
|
|
|
|
|
echo_out("--------------- ".date("Y-m-d H:i:s")." ---------------\n");
|
2022-12-05 18:11:32 +01:00
|
|
|
|
return(0);
|
|
|
|
|
}
|
2022-12-02 23:55:58 +01:00
|
|
|
|
|
2022-12-06 15:43:02 +01:00
|
|
|
|
function info() {
|
2022-12-06 21:05:41 +01:00
|
|
|
|
echo_out("OpenXE upgrade tool\n");
|
|
|
|
|
echo_out("Copyright 2022 (c) OpenXE project\n");
|
|
|
|
|
echo_out("\n");
|
|
|
|
|
echo_out("Upgrade files and database\n");
|
|
|
|
|
echo_out("Options:\n");
|
|
|
|
|
echo_out("\t-s: check/do system upgrades\n");
|
|
|
|
|
echo_out("\t-db: check/do database upgrades\n");
|
2022-12-08 15:01:32 +01:00
|
|
|
|
echo_out("\t-e: export database schema\n");
|
2022-12-06 21:05:41 +01:00
|
|
|
|
echo_out("\t-do: execute all upgrades\n");
|
|
|
|
|
echo_out("\t-v: verbose output\n");
|
|
|
|
|
echo_out("\t-f: force override of existing files\n");
|
2022-12-14 16:18:06 +01:00
|
|
|
|
echo_out("\t-o: update from origin instead of remote.json\n");
|
2022-12-09 08:13:12 +01:00
|
|
|
|
echo_out("\t-connection use connection.json in data folder instead of user.inc.php\n");
|
2022-12-06 21:05:41 +01:00
|
|
|
|
echo_out("\t-clean: (not yet implemented) create the needed SQL to remove items from the database not in the JSON\n");
|
|
|
|
|
echo_out("\n");
|
2022-12-06 15:43:02 +01:00
|
|
|
|
}
|
|
|
|
|
|
2022-12-02 23:55:58 +01:00
|
|
|
|
|