PM-401 Unify-Database
Unify Database and Restore changes
This commit is contained in:
@@ -523,9 +523,11 @@ class DataBaseMaintenance
|
|||||||
fwrite( $file, $data );
|
fwrite( $file, $data );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach ($tablesBpmn as $table) {
|
if (count ($sqlTablesBpmn) > 0) {
|
||||||
fwrite( $file, $sqlTablesBpmn[$table] );
|
foreach ($tablesBpmn as $table) {
|
||||||
|
fwrite( $file, $sqlTablesBpmn[$table] );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fclose( $file );
|
fclose( $file );
|
||||||
@@ -622,7 +624,7 @@ class DataBaseMaintenance
|
|||||||
* @return string $tableSchema
|
* @return string $tableSchema
|
||||||
*/
|
*/
|
||||||
function getSchemaFromTable ($tablename)
|
function getSchemaFromTable ($tablename)
|
||||||
{
|
{
|
||||||
//$tableSchema = "/* Structure for table `$tablename` */\n";
|
//$tableSchema = "/* Structure for table `$tablename` */\n";
|
||||||
//$tableSchema .= "DROP TABLE IF EXISTS `$tablename`;\n\n";
|
//$tableSchema .= "DROP TABLE IF EXISTS `$tablename`;\n\n";
|
||||||
$tableSchema = "";
|
$tableSchema = "";
|
||||||
|
|||||||
@@ -197,8 +197,18 @@ 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");
|
||||||
//setting flag to true to check into sysGeneric.php
|
//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;
|
$first = true;
|
||||||
$errors = false;
|
$errors = false;
|
||||||
$countWorkspace = 0;
|
$countWorkspace = 0;
|
||||||
@@ -240,6 +233,20 @@ function run_unify_database($args)
|
|||||||
try {
|
try {
|
||||||
$countWorkspace++;
|
$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) {
|
if ($workspace->onedb) {
|
||||||
CLI::logging("Workspace $workspace->name already one Database...\n");
|
CLI::logging("Workspace $workspace->name already one Database...\n");
|
||||||
} else {
|
} else {
|
||||||
@@ -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" );
|
|
||||||
|
|
||||||
$sql = "DROP DATABASE IF EXISTS {$db->name};";
|
$aParameters = array('dbHost'=>$dbHost,'dbUser'=>$dbUser,'dbPass'=>$dbPass);
|
||||||
if (! @mysql_query( $sql )) {
|
|
||||||
throw new Exception( mysql_error() );
|
$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" );
|
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" );
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -233,12 +233,13 @@ class workspaceTools
|
|||||||
$value = isset($matches['value']) ? $matches['value'] : $matches[3];
|
$value = isset($matches['value']) ? $matches['value'] : $matches[3];
|
||||||
|
|
||||||
if($this->onedb){
|
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'))) {
|
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_');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (array_search($key, array('DB_HOST', 'DB_RBAC_HOST', 'DB_REPORT_HOST')) !== false) {
|
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
|
* @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>');"
|
||||||
@@ -434,7 +428,7 @@ class workspaceTools
|
|||||||
* @return database connection
|
* @return database connection
|
||||||
*/
|
*/
|
||||||
private function getDatabase($rbac = false)
|
private function getDatabase($rbac = false)
|
||||||
{
|
{
|
||||||
if (isset($this->db) && $this->db->isConnected() && $rbac == false) {
|
if (isset($this->db) && $this->db->isConnected() && $rbac == false) {
|
||||||
return $this->db;
|
return $this->db;
|
||||||
}
|
}
|
||||||
@@ -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;
|
||||||
@@ -1449,7 +1461,7 @@ class workspaceTools
|
|||||||
CLI::logging("> Restoring " . CLI::info($backupWorkspace) . " to " . CLI::info($workspaceName) . "\n");
|
CLI::logging("> Restoring " . CLI::info($backupWorkspace) . " to " . CLI::info($workspaceName) . "\n");
|
||||||
}
|
}
|
||||||
$workspace = new workspaceTools($workspaceName);
|
$workspace = new workspaceTools($workspaceName);
|
||||||
|
|
||||||
if ($workspace->workspaceExists()) {
|
if ($workspace->workspaceExists()) {
|
||||||
|
|
||||||
if ($overwrite) {
|
if ($overwrite) {
|
||||||
@@ -1492,14 +1504,17 @@ 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) {
|
||||||
$dbName = $newDBNames[$db->name];
|
if ($dbName != $newDBNames[$db->name]) {
|
||||||
CLI::logging("+> Restoring database {$db->name} to $dbName\n");
|
$dbName = $newDBNames[$db->name];
|
||||||
$workspace->executeSQLScript($dbName, "$tempDirectory/{$db->name}.sql",$aParameters);
|
CLI::logging("+> Restoring database {$db->name} to $dbName\n");
|
||||||
$workspace->createDBUser($dbName, $db->pass, "localhost", $dbName);
|
$workspace->executeSQLScript($dbName, "$tempDirectory/{$db->name}.sql",$aParameters);
|
||||||
$workspace->createDBUser($dbName, $db->pass, "%", $dbName);
|
$workspace->createDBUser($dbName, $db->pass, "localhost", $dbName);
|
||||||
|
$workspace->createDBUser($dbName, $db->pass, "%", $dbName);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$version = explode('-', $metadata->PM_VERSION);
|
$version = explode('-', $metadata->PM_VERSION);
|
||||||
@@ -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");
|
||||||
|
|||||||
Reference in New Issue
Block a user