Skip to content

Commit

Permalink
#518 - Migrate DB in container only when connection parameters are set
Browse files Browse the repository at this point in the history
  • Loading branch information
maraf committed Jan 11, 2024
1 parent 1ec22b9 commit 63c7c97
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 52 deletions.
106 changes: 54 additions & 52 deletions docker/src/bin/migrate.php
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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);
}

?>
1 change: 1 addition & 0 deletions src/app/scripts/php/classes/Module.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 63c7c97

Please sign in to comment.