From 63c7c97a5c7dd7161b34c5ec5871904a554ebddc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Fi=C5=A1era?= Date: Thu, 11 Jan 2024 14:43:46 +0100 Subject: [PATCH] #518 - Migrate DB in container only when connection parameters are set --- docker/src/bin/migrate.php | 106 ++++++++++--------- src/app/scripts/php/classes/Module.class.php | 1 + 2 files changed, 55 insertions(+), 52 deletions(-) diff --git a/docker/src/bin/migrate.php b/docker/src/bin/migrate.php index ec7e5dd5..731c5ae3 100644 --- a/docker/src/bin/migrate.php +++ b/docker/src/bin/migrate.php @@ -5,58 +5,59 @@ require_once(__DIR__ . "/" . "../app/scripts/php/includes/settings.inc.php"); // Migrate DB - - require_once(APP_SCRIPTS_PHP_PATH . "libs/Database.class.php"); - - $db = new Database(false); - $db->disableCache(); - $db->getDataAccess()->connect(WEB_DB_HOSTNAME, WEB_DB_USER, WEB_DB_PASSWORD, WEB_DB_DATABASE, false, false); - $db->getDataAccess()->transaction(); - - // Duplicated in setup.php - if (count($db->fetchAll("SHOW TABLES LIKE 'system_property';")) == 0) { - echo "Import db schema and default data" . PHP_EOL; - - $importFilePath = APP_PATH . "data/default/default_330.sql"; - $importFile = fopen($importFilePath, 'r') or die('Cannot open file: ' . $importFilePath); - $user = [ - "name" => "admin", - "surname" => "admin", - "login" => "admin", - "password" => sha1("admin" . "admin") // Duplicated in User.class.php - ]; - $batch = ''; - while (($line = fgets($importFile)) !== false) { - // Skip it if it's a comment - if (substr($line, 0, 2) == '--' || $line == '') - continue; - - // Add this line to the current segment - $batch .= $line; - - // If it has a semicolon at the end, it's the end of the query - if (substr(trim($line), -1, 1) == ';') { - - $batch = str_replace("{user-name}", $user['name'], $batch); - $batch = str_replace("{user-surname}", $user['surname'], $batch); - $batch = str_replace("{user-login}", $user['login'], $batch); - $batch = str_replace("{user-password}", $user['password'], $batch); - - // Perform the query - $db->execute($batch); - - // Reset temp variable to empty - $batch = ''; + if (WEB_DB_HOSTNAME) { + require_once(APP_SCRIPTS_PHP_PATH . "libs/Database.class.php"); + + $db = new Database(false); + $db->disableCache(); + $db->getDataAccess()->connect(WEB_DB_HOSTNAME, WEB_DB_USER, WEB_DB_PASSWORD, WEB_DB_DATABASE, false, false); + $db->getDataAccess()->transaction(); + + // Duplicated in setup.php + if (count($db->fetchAll("SHOW TABLES LIKE 'system_property';")) == 0) { + echo "Import db schema and default data" . PHP_EOL; + + $importFilePath = APP_PATH . "data/default/default_330.sql"; + $importFile = fopen($importFilePath, 'r') or die('Cannot open file: ' . $importFilePath); + $user = [ + "name" => "admin", + "surname" => "admin", + "login" => "admin", + "password" => sha1("admin" . "admin") // Duplicated in User.class.php + ]; + $batch = ''; + while (($line = fgets($importFile)) !== false) { + // Skip it if it's a comment + if (substr($line, 0, 2) == '--' || $line == '') + continue; + + // Add this line to the current segment + $batch .= $line; + + // If it has a semicolon at the end, it's the end of the query + if (substr(trim($line), -1, 1) == ';') { + + $batch = str_replace("{user-name}", $user['name'], $batch); + $batch = str_replace("{user-surname}", $user['surname'], $batch); + $batch = str_replace("{user-login}", $user['login'], $batch); + $batch = str_replace("{user-password}", $user['password'], $batch); + + // Perform the query + $db->execute($batch); + + // Reset temp variable to empty + $batch = ''; + } } } - } - - $dbObject = $db; - - require_once(APP_SCRIPTS_PHP_PATH . "includes/autoupdate.inc.php"); - - $db->getDataAccess()->disconnect(); + + $dbObject = $db; + + require_once(APP_SCRIPTS_PHP_PATH . "includes/autoupdate.inc.php"); + + $db->getDataAccess()->disconnect(); + } // Regenerate module scripts @@ -66,9 +67,10 @@ ModuleGenerator::all(); // Drop system property cache + if (WEB_DB_HOSTNAME) { + require_once(APP_SCRIPTS_PHP_PATH . "classes/manager/SystemProperty.class.php"); - require_once(APP_SCRIPTS_PHP_PATH . "classes/manager/SystemProperty.class.php"); - - unlink(CACHE_SYSTEMPROPERTY_PATH . SystemPropertyGenerator::loaderFileName); + unlink(CACHE_SYSTEMPROPERTY_PATH . SystemPropertyGenerator::loaderFileName); + } ?> \ No newline at end of file diff --git a/src/app/scripts/php/classes/Module.class.php b/src/app/scripts/php/classes/Module.class.php index 6d43829f..df31f535 100644 --- a/src/app/scripts/php/classes/Module.class.php +++ b/src/app/scripts/php/classes/Module.class.php @@ -2,6 +2,7 @@ require_once("CodeWriter.class.php"); require_once("manager/Version.class.php"); + require_once("utils/FileUtils.class.php"); class Module { private static $all = null;