Skip to content
Snippets Groups Projects
Commit f51c4c7f authored by Peter Rotich's avatar Peter Rotich
Browse files

Improve DB upgrader

parent f2aef1d0
Branches
Tags
No related merge requests found
...@@ -93,7 +93,7 @@ class Config { ...@@ -93,7 +93,7 @@ class Config {
if($this->config['schema_signature']) if($this->config['schema_signature'])
return $this->config['schema_signature']; return $this->config['schema_signature'];
elseif($this->config['ostversion']) //old version 1.6 st. elseif($this->config['ostversion']) //old version 1.6 st.
return md5($this->config['ostversion']); return md5(strtoupper($this->config['ostversion']));
return null; return null;
} }
......
...@@ -21,22 +21,31 @@ ...@@ -21,22 +21,31 @@
class DatabaseMigrater { class DatabaseMigrater {
function DatabaseMigrater($sqldir) { var $start;
var $end;
var $sqldir;
function DatabaseMigrater($start, $end, $sqldir) {
$this->start = $start;
$this->end = $end;
$this->sqldir = $sqldir; $this->sqldir = $sqldir;
} }
function getRollup($stops) { function getPatches($stop=null) {
$cfg->reload();
$start = $cfg->getSchemaSignature(); $start= $this->start;
$stop = $stop?$stop:$this->end;
$patches = array(); $patches = array();
while (true) { while (true) {
$next = glob($this->sqldir . $substr($start,0,8) $next = glob($this->sqldir . substr($start, 0, 8)
. '-*.patch.sql'); . '-*.patch.sql');
if (count($next) == 1) { if (count($next) == 1) {
$patches[] = $next[0]; $patches[] = $next[0];
$start = substr($next[0], 0, 8); $start = substr(basename($next[0]), 9, 8);
} elseif ($count($next) == 0) { } elseif (count($next) == 0) {
# There are no patches leaving the current signature. We # There are no patches leaving the current signature. We
# have to assume that we've applied all the available # have to assume that we've applied all the available
# patches. # patches.
...@@ -47,9 +56,12 @@ class DatabaseMigrater { ...@@ -47,9 +56,12 @@ class DatabaseMigrater {
break; break;
} }
if (array_key_exists($next[0], $stops)) # Break if we've reached our target stop.
if(!$start || !strncasecmp($start, $stop, 8))
break; break;
} }
return $patches; return $patches;
} }
} }
?>
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
**********************************************************************/ **********************************************************************/
require_once INC_DIR.'class.setup.php'; require_once INC_DIR.'class.setup.php';
require_once INC_DIR.'class.migrater.php';
class Upgrader extends SetupWizard { class Upgrader extends SetupWizard {
...@@ -40,7 +41,8 @@ class Upgrader extends SetupWizard { ...@@ -40,7 +41,8 @@ class Upgrader extends SetupWizard {
//Tasks to perform - saved on the session. //Tasks to perform - saved on the session.
$this->tasks = &$_SESSION['ost_upgrader'][$this->getShash()]['tasks']; $this->tasks = &$_SESSION['ost_upgrader'][$this->getShash()]['tasks'];
$this->migrater = DatabaseMigrater($this->sqldir); //Database migrater
$this->migrater = new DatabaseMigrater($this->signature, SCHEMA_SIGNATURE, $this->sqldir);
} }
function getStops() { function getStops() {
...@@ -85,7 +87,7 @@ class Upgrader extends SetupWizard { ...@@ -85,7 +87,7 @@ class Upgrader extends SetupWizard {
} }
function getPatches() { function getPatches() {
return $this->migrater->getRollup($this->getStops()); return $this->migrater->getPatches();
} }
function getNextPatch() { function getNextPatch() {
...@@ -202,59 +204,55 @@ class Upgrader extends SetupWizard { ...@@ -202,59 +204,55 @@ class Upgrader extends SetupWizard {
if($this->getPendingTasks() || !($patches=$this->getPatches())) if($this->getPendingTasks() || !($patches=$this->getPatches()))
return false; return false;
foreach ($patches as $patch) foreach ($patches as $patch) {
if (!$this->load_sql_file($patch, $this->getTablePrefix())) if (!$this->load_sql_file($patch, $this->getTablePrefix()))
return false; return false;
//TODO: Log the upgrade //TODO: Log the upgrade
//Load up post install tasks.
$shash = substr(basename($patch), 9, 8);
$phash = substr(basename($patch), 0, 17);
//clear previous patch info -
unset($_SESSION['ost_upgrader'][$this->getSHash()]);
//Load up post-upgrade tasks.... if any.
$phash = substr(basename($patch), 0, 17);
if(!($tasks=$this->getTasksForPatch($phash)))
continue;
//We have tasks to perform - set the tasks and break.
$shash = substr($phash, 9, 8);
$_SESSION['ost_upgrader'][$shash]['tasks'] = $tasks;
$_SESSION['ost_upgrader'][$shash]['state'] = 'upgrade';
break;
}
return true;
}
function getTasksForPatch($phash) {
$tasks=array(); $tasks=array();
$tasks[] = array('func' => 'sometask',
'desc' => 'Some Task.... blah');
switch($phash) { //Add patch specific scripted tasks. switch($phash) { //Add patch specific scripted tasks.
case 'xxxx': //V1.6 ST- 1.7 * case 'd4fe13b1-7be60a84': //V1.6 ST- 1.7 *
$tasks[] = array('func' => 'migrateAttachments2DB', $tasks[] = array('func' => 'migrateAttachments2DB',
'desc' => 'Migrating attachments to database, it might take a while depending on the number of files.'); 'desc' => 'Migrating attachments to database, it might take a while depending on the number of files.');
break; break;
} }
$tasks[] = array('func' => 'cleanup',
'desc' => 'Post-upgrade cleanup!');
//Load up tasks - NOTE: writing directly to the session - back to the future majic.
$_SESSION['ost_upgrader'][$shash]['tasks'] = $tasks;
$_SESSION['ost_upgrader'][$shash]['state'] = 'upgrade';
//clear previous patch info -
unset($_SESSION['ost_upgrader'][$this->getSHash()]);
return true;
}
/************* TASKS **********************/ //Check if cleanup p
function sometask($tId) { $file=$this->getSQLDir().$phash.'.cleanup.sql';
if(file_exists($file))
$this->setTaskStatus($tId, 'Doing... '.time(). ' #'.$_SESSION['sometask']); $tasks[] = array('func' => 'cleanup', 'desc' => 'Post-upgrade cleanup!');
sleep(2);
$_SESSION['sometask']+=1;
if($_SESSION['sometask']<4)
return 22;
$_SESSION['sometask']=0; return $tasks;
return 0; //Change to 1 for testing...
} }
/************* TASKS **********************/
function cleanup($tId=0) { function cleanup($tId=0) {
$file=$this->getSQLDir().$this->getSchemaSignature().'-cleanup.sql'; $file=$this->getSQLDir().$this->getSHash().'-cleanup.sql';
if(!file_exists($file)) //No cleanup script. if(!file_exists($file)) //No cleanup script.
return 0; return 0;
......
...@@ -49,7 +49,9 @@ if($_POST && $_POST['s'] && !$upgrader->isAborted()) { ...@@ -49,7 +49,9 @@ if($_POST && $_POST['s'] && !$upgrader->isAborted()) {
case 'prereq': case 'prereq':
//XXX: check if it's upgradable version?? //XXX: check if it's upgradable version??
if(!$cfg->isUpgradePending()) if(!$cfg->isUpgradePending())
$errors['err']=' Nothing to do! System already upgraded to the current version'; $errors['err']=' Nothing to do! System already upgraded to the current version';
elseif(!$upgrader->isUpgradable())
$errors['err']='The upgrader does NOT support upgrading from the current vesion!';
elseif($upgrader->check_prereq()) elseif($upgrader->check_prereq())
$upgrader->setState('upgrade'); $upgrader->setState('upgrade');
else else
...@@ -89,6 +91,8 @@ switch(strtolower($upgrader->getState())) { ...@@ -89,6 +91,8 @@ switch(strtolower($upgrader->getState())) {
$inc='upgrade-aborted.inc.php'; $inc='upgrade-aborted.inc.php';
elseif(!$cfg->isUpgradePending()) elseif(!$cfg->isUpgradePending())
$errors['err']='Nothing to do! System already upgraded to the latest version'; $errors['err']='Nothing to do! System already upgraded to the latest version';
elseif(!$upgrader->isUpgradable())
$errors['err']='The upgrader does NOT support upgrading from the current vesion!';
} }
require(INC_DIR.'header.inc.php'); require(INC_DIR.'header.inc.php');
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment