From f51c4c7f638ced4d42187a96bbc77f60529f611c Mon Sep 17 00:00:00 2001 From: Peter Rotich <peter@osticket.com> Date: Mon, 23 Apr 2012 14:33:59 -0400 Subject: [PATCH] Improve DB upgrader --- include/class.config.php | 2 +- setup/inc/class.migrater.php | 28 ++++++++++---- setup/inc/class.upgrader.php | 74 ++++++++++++++++++------------------ setup/upgrade.php | 6 ++- 4 files changed, 62 insertions(+), 48 deletions(-) diff --git a/include/class.config.php b/include/class.config.php index e54c0d739..95a5204e3 100644 --- a/include/class.config.php +++ b/include/class.config.php @@ -93,7 +93,7 @@ class Config { if($this->config['schema_signature']) return $this->config['schema_signature']; elseif($this->config['ostversion']) //old version 1.6 st. - return md5($this->config['ostversion']); + return md5(strtoupper($this->config['ostversion'])); return null; } diff --git a/setup/inc/class.migrater.php b/setup/inc/class.migrater.php index 6475ac03c..de8a3d959 100644 --- a/setup/inc/class.migrater.php +++ b/setup/inc/class.migrater.php @@ -21,22 +21,31 @@ 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; + } - function getRollup($stops) { - $cfg->reload(); - $start = $cfg->getSchemaSignature(); + function getPatches($stop=null) { + + $start= $this->start; + $stop = $stop?$stop:$this->end; $patches = array(); while (true) { - $next = glob($this->sqldir . $substr($start,0,8) + $next = glob($this->sqldir . substr($start, 0, 8) . '-*.patch.sql'); if (count($next) == 1) { $patches[] = $next[0]; - $start = substr($next[0], 0, 8); - } elseif ($count($next) == 0) { + $start = substr(basename($next[0]), 9, 8); + } elseif (count($next) == 0) { # There are no patches leaving the current signature. We # have to assume that we've applied all the available # patches. @@ -47,9 +56,12 @@ class DatabaseMigrater { break; } - if (array_key_exists($next[0], $stops)) + # Break if we've reached our target stop. + if(!$start || !strncasecmp($start, $stop, 8)) break; } + return $patches; } } +?> diff --git a/setup/inc/class.upgrader.php b/setup/inc/class.upgrader.php index 971a542c9..cd785a9d2 100644 --- a/setup/inc/class.upgrader.php +++ b/setup/inc/class.upgrader.php @@ -15,6 +15,7 @@ **********************************************************************/ require_once INC_DIR.'class.setup.php'; +require_once INC_DIR.'class.migrater.php'; class Upgrader extends SetupWizard { @@ -40,7 +41,8 @@ class Upgrader extends SetupWizard { //Tasks to perform - saved on the session. $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() { @@ -85,7 +87,7 @@ class Upgrader extends SetupWizard { } function getPatches() { - return $this->migrater->getRollup($this->getStops()); + return $this->migrater->getPatches(); } function getNextPatch() { @@ -202,59 +204,55 @@ class Upgrader extends SetupWizard { if($this->getPendingTasks() || !($patches=$this->getPatches())) return false; - foreach ($patches as $patch) + foreach ($patches as $patch) { if (!$this->load_sql_file($patch, $this->getTablePrefix())) return false; - //TODO: Log the upgrade - - //Load up post install tasks. - $shash = substr(basename($patch), 9, 8); - $phash = substr(basename($patch), 0, 17); + //TODO: Log the upgrade + + //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('func' => 'sometask', - 'desc' => 'Some Task.... blah'); 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', 'desc' => 'Migrating attachments to database, it might take a while depending on the number of files.'); 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 **********************/ - function sometask($tId) { - - $this->setTaskStatus($tId, 'Doing... '.time(). ' #'.$_SESSION['sometask']); + //Check if cleanup p + $file=$this->getSQLDir().$phash.'.cleanup.sql'; + if(file_exists($file)) + $tasks[] = array('func' => 'cleanup', 'desc' => 'Post-upgrade cleanup!'); - sleep(2); - $_SESSION['sometask']+=1; - if($_SESSION['sometask']<4) - return 22; - $_SESSION['sometask']=0; - - return 0; //Change to 1 for testing... + return $tasks; } + /************* TASKS **********************/ 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. return 0; diff --git a/setup/upgrade.php b/setup/upgrade.php index bd961286a..324c467ef 100644 --- a/setup/upgrade.php +++ b/setup/upgrade.php @@ -49,7 +49,9 @@ if($_POST && $_POST['s'] && !$upgrader->isAborted()) { case 'prereq': //XXX: check if it's upgradable version?? 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()) $upgrader->setState('upgrade'); else @@ -89,6 +91,8 @@ switch(strtolower($upgrader->getState())) { $inc='upgrade-aborted.inc.php'; elseif(!$cfg->isUpgradePending()) $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'); -- GitLab