PM-401 Unify-Database

Unify Database and Restore changes
This commit is contained in:
norahmollo
2014-10-09 10:36:34 -04:00
parent 5b7a1a3572
commit e17c28adf8
3 changed files with 81 additions and 52 deletions

View File

@@ -524,9 +524,11 @@ class DataBaseMaintenance
} }
} }
if (count ($sqlTablesBpmn) > 0) {
foreach ($tablesBpmn as $table) { foreach ($tablesBpmn as $table) {
fwrite( $file, $sqlTablesBpmn[$table] ); fwrite( $file, $sqlTablesBpmn[$table] );
} }
}
fclose( $file ); fclose( $file );
} }

View File

@@ -197,7 +197,17 @@ function listFiles($dir) {
function run_unify_database($args) 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("UPGRADE", PROCESSMAKER_PATH . "upgrade.log");
CLI::logging("Checking workspaces...\n"); CLI::logging("Checking workspaces...\n");
@@ -214,7 +224,14 @@ function run_unify_database($args)
} }
} }
$first = true;
$errors = false;
$countWorkspace = 0;
$buildCacheView = array_key_exists("buildACV", $args);
foreach ($workspaces as $workspace) { foreach ($workspaces as $workspace) {
try {
$countWorkspace++;
if (! $workspace->workspaceExists()) { if (! $workspace->workspaceExists()) {
echo "Workspace {$workspace->name} not found\n"; echo "Workspace {$workspace->name} not found\n";
@@ -229,16 +246,6 @@ function run_unify_database($args)
} else { } else {
$workspace->onedb = true; $workspace->onedb = true;
} }
}
$first = true;
$errors = false;
$countWorkspace = 0;
$buildCacheView = array_key_exists("buildACV", $args);
foreach ($workspaces as $workspace) {
try {
$countWorkspace++;
if ($workspace->onedb) { if ($workspace->onedb) {
CLI::logging("Workspace $workspace->name already one Database...\n"); CLI::logging("Workspace $workspace->name already one Database...\n");
@@ -262,23 +269,28 @@ function run_unify_database($args)
list ($dbHost, $dbUser, $dbPass) = @explode( SYSTEM_HASH, G::decrypt( HASH_INSTALLATION, SYSTEM_HASH ) ); list ($dbHost, $dbUser, $dbPass) = @explode( SYSTEM_HASH, G::decrypt( HASH_INSTALLATION, SYSTEM_HASH ) );
$link = mysql_connect( $dbHost, $dbUser, $dbPass ); $link = mysql_connect( $dbHost, $dbUser, $dbPass );
foreach ($metadata->databases as $db) { foreach ($metadata['databases'] as $db) {
$dbName = 'wf_'.$workspace->name; $dbName = 'wf_'.$workspace->name;
CLI::logging( "+> Restoring {$db->name} to $dbName database\n" ); CLI::logging( "+> Restoring {$db['name']} to $dbName database\n" );
$restore = $workspace->executeSQLScript( $dbName, "$tempDirectory/{$db->name}.sql" );
CLI::logging( "+> Remove {$db->name} database\n" ); $aParameters = array('dbHost'=>$dbHost,'dbUser'=>$dbUser,'dbPass'=>$dbPass);
$sql = "DROP DATABASE IF EXISTS {$db->name};"; $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 )) { if (! @mysql_query( $sql )) {
throw new Exception( mysql_error() ); throw new Exception( mysql_error() );
} }
} }
}
CLI::logging( "Removing temporary files\n" ); CLI::logging( "Removing temporary files\n" );
G::rm_dir( $tempDirectory ); G::rm_dir( $tempDirectory );
$newDBNames = $workspace->resetDBInfo( $dbHost, true ); $newDBNames = $workspace->resetDBInfo( $dbHost, true, true );
CLI::logging( CLI::info( "Done restoring databases" ) . "\n" ); CLI::logging( CLI::info( "Done restoring databases" ) . "\n" );
} }

View File

@@ -233,9 +233,10 @@ class workspaceTools
$value = isset($matches['value']) ? $matches['value'] : $matches[3]; $value = isset($matches['value']) ? $matches['value'] : $matches[3];
if($this->onedb){ if($this->onedb){
$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_'); $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'))) { if (array_search($key, array('DB_PASS', 'DB_RBAC_PASS', 'DB_REPORT_PASS'))) {
$value = $this->dbInfo['DB_PASS']; $value = $dbInfo['DB_PASS'];
} }
} else{ } 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_');
@@ -272,7 +273,7 @@ class workspaceTools
* @param bool $resetDBNames if true, also reset all database names * @param bool $resetDBNames if true, also reset all database names
* @return array contains the new database names as values * @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) { if (count(explode(":", $newHost)) < 2) {
$newHost .= ':3306'; $newHost .= ':3306';
@@ -280,7 +281,7 @@ class workspaceTools
$this->newHost = $newHost; $this->newHost = $newHost;
$this->resetDBNames = $resetDBNames; $this->resetDBNames = $resetDBNames;
$this->resetDBDiff = array(); $this->resetDBDiff = array();
$this->onedb = false; $this->onedb = $onedb;
if (!$this->workspaceExists()) { if (!$this->workspaceExists()) {
throw new Exception("Could not find db.php in the workspace"); throw new Exception("Could not find db.php in the workspace");
@@ -289,14 +290,7 @@ class workspaceTools
if ($sDbFile === false) { if ($sDbFile === false) {
throw new Exception("Could not read database information from db.php"); 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 /* Match all defines in the config file. Check updateDBCallback to know what
* keys are changed and what groups are matched. * keys are changed and what groups are matched.
* This regular expression will match any "define ('<key>', '<value>');" * This regular expression will match any "define ('<key>', '<value>');"
@@ -847,6 +841,7 @@ class workspaceTools
if (!empty($changes['tablesToAdd'])) { if (!empty($changes['tablesToAdd'])) {
CLI::logging("-> " . count($changes['tablesToAdd']) . " tables to add\n"); CLI::logging("-> " . count($changes['tablesToAdd']) . " tables to add\n");
} }
foreach ($changes['tablesToAdd'] as $sTable => $aColumns) { foreach ($changes['tablesToAdd'] as $sTable => $aColumns) {
$oDataBase->executeQuery($oDataBase->generateCreateTableSQL($sTable, $aColumns)); $oDataBase->executeQuery($oDataBase->generateCreateTableSQL($sTable, $aColumns));
if (isset($changes['tablesToAdd'][$sTable]['INDEXES'])) { if (isset($changes['tablesToAdd'][$sTable]['INDEXES'])) {
@@ -859,6 +854,7 @@ class workspaceTools
if (!empty($changes['tablesToAlter'])) { if (!empty($changes['tablesToAlter'])) {
CLI::logging("-> " . count($changes['tablesToAlter']) . " tables to alter\n"); CLI::logging("-> " . count($changes['tablesToAlter']) . " tables to alter\n");
} }
foreach ($changes['tablesToAlter'] as $sTable => $aActions) { foreach ($changes['tablesToAlter'] as $sTable => $aActions) {
foreach ($aActions as $sAction => $aAction) { foreach ($aActions as $sAction => $aAction) {
foreach ($aAction as $sColumn => $vData) { foreach ($aAction as $sColumn => $vData) {
@@ -974,6 +970,9 @@ class workspaceTools
$dbNetView = new NET($this->dbHost); $dbNetView = new NET($this->dbHost);
$dbNetView->loginDbServer($this->dbUser, $this->dbPass); $dbNetView->loginDbServer($this->dbUser, $this->dbPass);
try { try {
if (!defined('DB_ADAPTER')) {
require_once($this->dbPath);
}
$sMySQLVersion = $dbNetView->getDbServerVersion('mysql'); $sMySQLVersion = $dbNetView->getDbServerVersion('mysql');
} catch (Exception $oException) { } catch (Exception $oException) {
$sMySQLVersion = 'Unknown'; $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) static public function restoreLegacy($directory)
{ {
throw new Exception("Use gulliver to restore backups from old versions"); 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"); throw new Exception("Backup version {$metadata->version} not supported");
} }
$backupWorkspace = $metadata->WORKSPACE_NAME; $backupWorkspace = $metadata->WORKSPACE_NAME;
if (strpos($metadata->DB_RBAC_NAME, 'rb_') === false) {
$onedb = true;
} else {
$onedb = false;
}
if (isset($dstWorkspace)) { if (isset($dstWorkspace)) {
$workspaceName = $dstWorkspace; $workspaceName = $dstWorkspace;
$createWorkspace = true; $createWorkspace = true;
@@ -1492,15 +1504,18 @@ class workspaceTools
throw new Exception('Could not connect to system database: ' . mysql_error()); 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) { foreach ($metadata->databases as $db) {
if ($dbName != $newDBNames[$db->name]) {
$dbName = $newDBNames[$db->name]; $dbName = $newDBNames[$db->name];
CLI::logging("+> Restoring database {$db->name} to $dbName\n"); CLI::logging("+> Restoring database {$db->name} to $dbName\n");
$workspace->executeSQLScript($dbName, "$tempDirectory/{$db->name}.sql",$aParameters); $workspace->executeSQLScript($dbName, "$tempDirectory/{$db->name}.sql",$aParameters);
$workspace->createDBUser($dbName, $db->pass, "localhost", $dbName); $workspace->createDBUser($dbName, $db->pass, "localhost", $dbName);
$workspace->createDBUser($dbName, $db->pass, "%", $dbName); $workspace->createDBUser($dbName, $db->pass, "%", $dbName);
} }
}
$version = explode('-', $metadata->PM_VERSION); $version = explode('-', $metadata->PM_VERSION);
$versionOld = ( isset($version[0])) ? $version[0] : ''; $versionOld = ( isset($version[0])) ? $version[0] : '';
@@ -1516,7 +1531,7 @@ class workspaceTools
if ( $versionOld < $versionPresent || strpos($versionPresent, "Branch")) { if ( $versionOld < $versionPresent || strpos($versionPresent, "Branch")) {
$start = microtime(true); $start = microtime(true);
CLI::logging("> Updating database...\n"); CLI::logging("> Updating database...\n");
$workspace->upgradeDatabase(); $workspace->upgradeDatabase($onedb);
$stop = microtime(true); $stop = microtime(true);
$final = $stop - $start; $final = $stop - $start;
CLI::logging("<*> Database Upgrade Process took $final seconds.\n"); CLI::logging("<*> Database Upgrade Process took $final seconds.\n");