From e17c28adf8af9d8c2bf2f97a3b666cc7778ee392 Mon Sep 17 00:00:00 2001 From: norahmollo Date: Thu, 9 Oct 2014 10:36:34 -0400 Subject: [PATCH] PM-401 Unify-Database Unify Database and Restore changes --- gulliver/system/class.dbMaintenance.php | 10 ++-- workflow/engine/bin/tasks/cliUpgrade.php | 66 +++++++++++++---------- workflow/engine/classes/class.wsTools.php | 57 ++++++++++++-------- 3 files changed, 81 insertions(+), 52 deletions(-) diff --git a/gulliver/system/class.dbMaintenance.php b/gulliver/system/class.dbMaintenance.php index 79615a571..edf866889 100755 --- a/gulliver/system/class.dbMaintenance.php +++ b/gulliver/system/class.dbMaintenance.php @@ -523,9 +523,11 @@ class DataBaseMaintenance fwrite( $file, $data ); } } - - foreach ($tablesBpmn as $table) { - fwrite( $file, $sqlTablesBpmn[$table] ); + + if (count ($sqlTablesBpmn) > 0) { + foreach ($tablesBpmn as $table) { + fwrite( $file, $sqlTablesBpmn[$table] ); + } } fclose( $file ); @@ -622,7 +624,7 @@ class DataBaseMaintenance * @return string $tableSchema */ function getSchemaFromTable ($tablename) - { + { //$tableSchema = "/* Structure for table `$tablename` */\n"; //$tableSchema .= "DROP TABLE IF EXISTS `$tablename`;\n\n"; $tableSchema = ""; diff --git a/workflow/engine/bin/tasks/cliUpgrade.php b/workflow/engine/bin/tasks/cliUpgrade.php index 51f65c613..532e3871d 100755 --- a/workflow/engine/bin/tasks/cliUpgrade.php +++ b/workflow/engine/bin/tasks/cliUpgrade.php @@ -197,8 +197,18 @@ function listFiles($dir) { function run_unify_database($args) { - $workspaces = get_workspaces_from_args($args); + $workspaces = array(); + if (sizeof($args) > 2) { + $filename = array_pop($args); + foreach ($args as $arg) { + $workspaces[] = new workspaceTools($arg); + } + } else if (sizeof($args) > 0) { + $workspace = new workspaceTools($args[0]); + $workspaces[] = $workspace; + } + CLI::logging("UPGRADE", PROCESSMAKER_PATH . "upgrade.log"); CLI::logging("Checking workspaces...\n"); //setting flag to true to check into sysGeneric.php @@ -214,23 +224,6 @@ function run_unify_database($args) } } - foreach ($workspaces as $workspace) { - - if (! $workspace->workspaceExists()) { - echo "Workspace {$workspace->name} not found\n"; - return false; - } - - $ws = $workspace->name; - $sContent = file_get_contents (PATH_DB . $ws . PATH_SEP . 'db.php'); - - if (strpos($sContent, 'rb_')) { - $workspace->onedb = false; - } else { - $workspace->onedb = true; - } - } - $first = true; $errors = false; $countWorkspace = 0; @@ -240,6 +233,20 @@ function run_unify_database($args) try { $countWorkspace++; + if (! $workspace->workspaceExists()) { + echo "Workspace {$workspace->name} not found\n"; + return false; + } + + $ws = $workspace->name; + $sContent = file_get_contents (PATH_DB . $ws . PATH_SEP . 'db.php'); + + if (strpos($sContent, 'rb_')) { + $workspace->onedb = false; + } else { + $workspace->onedb = true; + } + if ($workspace->onedb) { CLI::logging("Workspace $workspace->name already one Database...\n"); } else { @@ -262,23 +269,28 @@ function run_unify_database($args) list ($dbHost, $dbUser, $dbPass) = @explode( SYSTEM_HASH, G::decrypt( HASH_INSTALLATION, SYSTEM_HASH ) ); $link = mysql_connect( $dbHost, $dbUser, $dbPass ); - foreach ($metadata->databases as $db) { + foreach ($metadata['databases'] as $db) { $dbName = 'wf_'.$workspace->name; - CLI::logging( "+> Restoring {$db->name} to $dbName database\n" ); - $restore = $workspace->executeSQLScript( $dbName, "$tempDirectory/{$db->name}.sql" ); - - CLI::logging( "+> Remove {$db->name} database\n" ); + CLI::logging( "+> Restoring {$db['name']} to $dbName database\n" ); - $sql = "DROP DATABASE IF EXISTS {$db->name};"; - if (! @mysql_query( $sql )) { - throw new Exception( mysql_error() ); + $aParameters = array('dbHost'=>$dbHost,'dbUser'=>$dbUser,'dbPass'=>$dbPass); + + $restore = $workspace->executeScript( $dbName, "$tempDirectory/{$db['name']}.sql", $aParameters); + + if ($restore) { + CLI::logging( "+> Remove {$db['name']} database\n" ); + + $sql = "DROP DATABASE IF EXISTS {$db['name']};"; + if (! @mysql_query( $sql )) { + throw new Exception( mysql_error() ); + } } } CLI::logging( "Removing temporary files\n" ); G::rm_dir( $tempDirectory ); - $newDBNames = $workspace->resetDBInfo( $dbHost, true ); + $newDBNames = $workspace->resetDBInfo( $dbHost, true, true ); CLI::logging( CLI::info( "Done restoring databases" ) . "\n" ); } diff --git a/workflow/engine/classes/class.wsTools.php b/workflow/engine/classes/class.wsTools.php index b1323e394..f5625c164 100755 --- a/workflow/engine/classes/class.wsTools.php +++ b/workflow/engine/classes/class.wsTools.php @@ -233,12 +233,13 @@ class workspaceTools $value = isset($matches['value']) ? $matches['value'] : $matches[3]; if($this->onedb){ - $dbPrefix = array('DB_NAME' => 'wf_', 'DB_USER' => 'wf_', 'DB_RBAC_NAME' => 'wf_', 'DB_RBAC_USER' => 'wf_', 'DB_REPORT_NAME' => 'wf_', 'DB_REPORT_USER' => 'wf_'); + $dbInfo = $this->getDBInfo(); + $dbPrefix = array('DB_NAME' => 'wf_', 'DB_USER' => 'wf_', 'DB_RBAC_NAME' => 'wf_', 'DB_RBAC_USER' => 'wf_', 'DB_REPORT_NAME' => 'wf_', 'DB_REPORT_USER' => 'wf_'); if (array_search($key, array('DB_PASS', 'DB_RBAC_PASS', 'DB_REPORT_PASS'))) { - $value = $this->dbInfo['DB_PASS']; + $value = $dbInfo['DB_PASS']; } } else{ - $dbPrefix = array('DB_NAME' => 'wf_', 'DB_USER' => 'wf_', 'DB_RBAC_NAME' => 'rb_', 'DB_RBAC_USER' => 'rb_', 'DB_REPORT_NAME' => 'rp_', 'DB_REPORT_USER' => 'rp_'); + $dbPrefix = array('DB_NAME' => 'wf_', 'DB_USER' => 'wf_', 'DB_RBAC_NAME' => 'rb_', 'DB_RBAC_USER' => 'rb_', 'DB_REPORT_NAME' => 'rp_', 'DB_REPORT_USER' => 'rp_'); } if (array_search($key, array('DB_HOST', 'DB_RBAC_HOST', 'DB_REPORT_HOST')) !== false) { @@ -272,7 +273,7 @@ class workspaceTools * @param bool $resetDBNames if true, also reset all database names * @return array contains the new database names as values */ - public function resetDBInfo($newHost, $resetDBNames = true) + public function resetDBInfo($newHost, $resetDBNames = true, $onedb = false) { if (count(explode(":", $newHost)) < 2) { $newHost .= ':3306'; @@ -280,7 +281,7 @@ class workspaceTools $this->newHost = $newHost; $this->resetDBNames = $resetDBNames; $this->resetDBDiff = array(); - $this->onedb = false; + $this->onedb = $onedb; if (!$this->workspaceExists()) { throw new Exception("Could not find db.php in the workspace"); @@ -289,14 +290,7 @@ class workspaceTools if ($sDbFile === false) { throw new Exception("Could not read database information from db.php"); - } else { - if (strpos($sDbFile, 'rb_')) { - $this->onedb = false; - } else { - $this->onedb = true; - } } - /* Match all defines in the config file. Check updateDBCallback to know what * keys are changed and what groups are matched. * This regular expression will match any "define ('', '');" @@ -434,7 +428,7 @@ class workspaceTools * @return database connection */ private function getDatabase($rbac = false) - { + { if (isset($this->db) && $this->db->isConnected() && $rbac == false) { return $this->db; } @@ -847,6 +841,7 @@ class workspaceTools if (!empty($changes['tablesToAdd'])) { CLI::logging("-> " . count($changes['tablesToAdd']) . " tables to add\n"); } + foreach ($changes['tablesToAdd'] as $sTable => $aColumns) { $oDataBase->executeQuery($oDataBase->generateCreateTableSQL($sTable, $aColumns)); if (isset($changes['tablesToAdd'][$sTable]['INDEXES'])) { @@ -859,6 +854,7 @@ class workspaceTools if (!empty($changes['tablesToAlter'])) { CLI::logging("-> " . count($changes['tablesToAlter']) . " tables to alter\n"); } + foreach ($changes['tablesToAlter'] as $sTable => $aActions) { foreach ($aActions as $sAction => $aAction) { foreach ($aAction as $sColumn => $vData) { @@ -974,6 +970,9 @@ class workspaceTools $dbNetView = new NET($this->dbHost); $dbNetView->loginDbServer($this->dbUser, $this->dbPass); try { + if (!defined('DB_ADAPTER')) { + require_once($this->dbPath); + } $sMySQLVersion = $dbNetView->getDbServerVersion('mysql'); } catch (Exception $oException) { $sMySQLVersion = 'Unknown'; @@ -1315,6 +1314,12 @@ class workspaceTools } } + public function executeScript($database, $filename, $parameters) + { + $this->executeSQLScript($database, $filename, $parameters); + return true; + } + static public function restoreLegacy($directory) { throw new Exception("Use gulliver to restore backups from old versions"); @@ -1435,6 +1440,13 @@ class workspaceTools throw new Exception("Backup version {$metadata->version} not supported"); } $backupWorkspace = $metadata->WORKSPACE_NAME; + + if (strpos($metadata->DB_RBAC_NAME, 'rb_') === false) { + $onedb = true; + } else { + $onedb = false; + } + if (isset($dstWorkspace)) { $workspaceName = $dstWorkspace; $createWorkspace = true; @@ -1449,7 +1461,7 @@ class workspaceTools CLI::logging("> Restoring " . CLI::info($backupWorkspace) . " to " . CLI::info($workspaceName) . "\n"); } $workspace = new workspaceTools($workspaceName); - + if ($workspace->workspaceExists()) { if ($overwrite) { @@ -1492,14 +1504,17 @@ class workspaceTools throw new Exception('Could not connect to system database: ' . mysql_error()); } - $newDBNames = $workspace->resetDBInfo($dbHost, $createWorkspace); + $dbName = ''; + $newDBNames = $workspace->resetDBInfo($dbHost, $createWorkspace, $onedb); foreach ($metadata->databases as $db) { - $dbName = $newDBNames[$db->name]; - CLI::logging("+> Restoring database {$db->name} to $dbName\n"); - $workspace->executeSQLScript($dbName, "$tempDirectory/{$db->name}.sql",$aParameters); - $workspace->createDBUser($dbName, $db->pass, "localhost", $dbName); - $workspace->createDBUser($dbName, $db->pass, "%", $dbName); + if ($dbName != $newDBNames[$db->name]) { + $dbName = $newDBNames[$db->name]; + CLI::logging("+> Restoring database {$db->name} to $dbName\n"); + $workspace->executeSQLScript($dbName, "$tempDirectory/{$db->name}.sql",$aParameters); + $workspace->createDBUser($dbName, $db->pass, "localhost", $dbName); + $workspace->createDBUser($dbName, $db->pass, "%", $dbName); + } } $version = explode('-', $metadata->PM_VERSION); @@ -1516,7 +1531,7 @@ class workspaceTools if ( $versionOld < $versionPresent || strpos($versionPresent, "Branch")) { $start = microtime(true); CLI::logging("> Updating database...\n"); - $workspace->upgradeDatabase(); + $workspace->upgradeDatabase($onedb); $stop = microtime(true); $final = $stop - $start; CLI::logging("<*> Database Upgrade Process took $final seconds.\n");