Skip to content
Snippets Groups Projects
Commit 881bb2a4 authored by Jared Hancock's avatar Jared Hancock
Browse files

Add database migration helper

Implement attachment migration for attachments
Add support for more information stored about the patches in the individual
patche files with a Javadoc style syntax
parent 550015dc
No related branches found
No related tags found
No related merge requests found
<?php
/*********************************************************************
class.migrater.php
SQL database migrater. This provides the engine capable of rolling the
database for an osTicket installation forward (and perhaps even
backward) in time using a set of included migration scripts. Each script
will roll the database between two database checkpoints. Where possible,
the migrater will roll several checkpoint scripts into one to be applied
together.
Jared Hancock <jared@osticket.com>
Copyright (c) 2006-2012 osTicket
http://www.osticket.com
Released under the GNU General Public License WITHOUT ANY WARRANTY.
See LICENSE.TXT for details.
vim: expandtab sw=4 ts=4 sts=4:
**********************************************************************/
class DatabaseMigrater {
function DatabaseMigrater($sqldir) {
$this->sqldir = $sqldir;
}
function getRollup($stops) {
$cfg->reload();
$start = $cfg->getSchemaSignature();
$patches = array();
while (true) {
$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) {
# There are no patches leaving the current signature. We
# have to assume that we've applied all the available
# patches.
break;
} else {
# Problem -- more than one patch exists from this snapshot.
# We probably need a graph approach to solve this.
break;
}
if (array_key_exists($next[0], $stops))
break;
}
return $patches;
}
}
...@@ -144,6 +144,12 @@ class Upgrader extends SetupWizard { ...@@ -144,6 +144,12 @@ 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);
}
function getStops() {
return array('7be60a84' => 'migrateAttachments2DB');
} }
function onError($error) { function onError($error) {
...@@ -183,32 +189,32 @@ class Upgrader extends SetupWizard { ...@@ -183,32 +189,32 @@ class Upgrader extends SetupWizard {
$this->state = $state; $this->state = $state;
} }
function getNextPatch() { function getPatches() {
return $this->migrater->getRollup($this->getStops());
if(!($patch=glob($this->getSQLDir().$this->getShash().'-*.patch.sql')))
return null;
return $patch[0];
} }
function getThisPatch() { function getNextPatch() {
$p = $this->getPatches();
if(!($patch=glob($this->getSQLDir().'*-'.$this->getShash().'.patch.sql'))) return (count($p)) ? $p[0] : false;
return null;
return $patch[0];
} }
function getNextVersion() { function getNextVersion() {
if(!$patch=$this->getNextPatch()) if(!$patch=$this->getNextPatch())
return '(Latest)'; return '(Latest)';
if(preg_match('/\*(.*)\*/', file_get_contents($patch), $matches)) $info = $this->readPatchInfo($patch);
$info=$matches[0]; return $info['version'];
else }
$info=substr(basename($patch), 9, 8);
function readPatchInfo($patch) {
$info = array();
if (preg_match('/\*(.*)\*/', file_get_contents($patch), $matches)) {
if (preg_match('/@([\w\d_-]+)\s+(.*)$/', $matches[0], $matches2))
foreach ($matches2 as $match)
$info[$match[0]] = $match[1];
}
if (!isset($info['version']))
$info['version'] = substr(basename($patch), 9, 8);
return $info; return $info;
} }
...@@ -237,19 +243,6 @@ class Upgrader extends SetupWizard { ...@@ -237,19 +243,6 @@ class Upgrader extends SetupWizard {
return null; return null;
} }
function getPatches($ToSignature) {
$signature = $this->getSignature();
$patches = array();
while(($patch=glob($this->getSQLDir().substr($signature,0,8).'-*.patch.sql')) && $i++) {
$patches = array_merge($patches, $patch);
if(!($signature=substr(basename($patch[0]), 10, 8)) || !strncasecmp($signature, $ToSignature, 8))
break;
}
return $patches;
}
function getNumPendingTasks() { function getNumPendingTasks() {
return count($this->getPendingTasks()); return count($this->getPendingTasks());
...@@ -311,11 +304,12 @@ class Upgrader extends SetupWizard { ...@@ -311,11 +304,12 @@ class Upgrader extends SetupWizard {
function upgrade() { function upgrade() {
if($this->getPendingTasks() || !($patch=$this->getNextPatch())) if($this->getPendingTasks() || !($patches=$this->getPatches()))
return false; return false;
if(!$this->load_sql_file($patch, $this->getTablePrefix())) foreach ($patches as $patch)
return false; if (!$this->load_sql_file($patch, $this->getTablePrefix()))
return false;
//TODO: Log the upgrade //TODO: Log the upgrade
...@@ -380,6 +374,10 @@ class Upgrader extends SetupWizard { ...@@ -380,6 +374,10 @@ class Upgrader extends SetupWizard {
function migrateAttachments2DB($tId=0) { function migrateAttachments2DB($tId=0) {
echo "Process attachments here - $tId"; echo "Process attachments here - $tId";
$att_migrater = new AttachmentMigrater();
$att_migrater->start_migration();
# XXX: Loop here (with help of task manager)
$att_migrater->do_batch();
return 0; return 0;
} }
} }
......
/* v1.7-DPR2-P2 */ /**
* @version v1.7-DPR2-P2
*/
UPDATE `%TABLE_PREFIX%sla` UPDATE `%TABLE_PREFIX%sla`
SET `created` = NOW(), SET `created` = NOW(),
`updated` = NOW() `updated` = NOW()
......
/* v1.7-DPR1 (P1) */ /**
* @version v1.7-DPR1 (P1)
*/
UPDATE `%TABLE_PREFIX%email_template` UPDATE `%TABLE_PREFIX%email_template`
SET `ticket_overlimit_subj` = 'Open Tickets Limit Reached' SET `ticket_overlimit_subj` = 'Open Tickets Limit Reached'
WHERE `tpl_id` = 1 AND `cfg_id` = 1; WHERE `tpl_id` = 1 AND `cfg_id` = 1;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment