Permalink
Browse files

Improve DB upgrader

  • Loading branch information...
1 parent f2aef1d commit f51c4c7f638ced4d42187a96bbc77f60529f611c @protich committed Apr 23, 2012
Showing with 62 additions and 48 deletions.
  1. +1 −1 include/class.config.php
  2. +20 −8 setup/inc/class.migrater.php
  3. +36 −38 setup/inc/class.upgrader.php
  4. +5 −1 setup/upgrade.php
@@ -93,7 +93,7 @@ function getSchemaSignature() {
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;
}
@@ -21,22 +21,31 @@ class.migrater.php
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 @@ function getRollup($stops) {
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;
}
}
+?>
@@ -15,6 +15,7 @@ class.upgrader.php
**********************************************************************/
require_once INC_DIR.'class.setup.php';
+require_once INC_DIR.'class.migrater.php';
class Upgrader extends SetupWizard {
@@ -40,7 +41,8 @@ function Upgrader($signature, $prefix, $sqldir) {
//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 @@ function setState($state) {
}
function getPatches() {
- return $this->migrater->getRollup($this->getStops());
+ return $this->migrater->getPatches();
}
function getNextPatch() {
@@ -202,59 +204,55 @@ function upgrade() {
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;
View
@@ -49,7 +49,9 @@ function staffLoginPage($msg) {
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 @@ function staffLoginPage($msg) {
$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');

0 comments on commit f51c4c7

Please sign in to comment.