Changeset 433 for trunk/upgrade.php

Show
Ignore:
Timestamp:
03/31/07 18:45:48 (20 months ago)
Author:
michiel
Message:

rewrote the upgrade routines.
removed test upgrade php file. Why the hell did I commit it the first place.

Re #124

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/upgrade.php

    r410 r433  
    2121 * Uprade mvblog to the latest version 
    2222 */ 
    23 require_once("common/mvblog_common.php"); 
    24 $common = new Mvblog_common(); 
    25 $latest_patch = $common->settings["dbversion"]; 
    26 /** 
    27  * Find out what patchfiles we need to commit. 
    28  */ 
    29 function get_patchfiles($current_version = 0, $sqltype="mysql") { 
    30         /* read the db patchfiles into an array */ 
    31         $patchfiles = array(); 
    32         if ($dh = opendir(sprintf("upgrades/%s", $sqltype))) { 
    33                 while (false !== ($v = readdir($dh))) { 
    34                         if (!is_dir($v)) { 
    35                                 if (substr($v, 0, strpos($v, "to")) >= $current_version) 
    36                                         $patchfiles[] = $v; 
    37                         } 
    38                 } 
    39         } 
    40         return $patchfiles; 
    41 } 
     23if (array_key_exists("mode", $_REQUEST)) 
     24        $mode = $_REQUEST["mode"]; 
     25else 
     26        $mode = ""; 
    4227 
    43 /** 
    44  * Update/create dbversion setting in the database 
    45  */ 
    46 function set_latest_version($version=0) { 
    47         /* check if the field is already in the database */ 
    48         $sql = "SELECT COUNT(*) FROM settings WHERE settingname = 'dbversion'"; 
    49         $res = $GLOBALS["common"]->db->query($sql); 
    50         $res->fetchInto($row); 
    51         if ($row[0]) { 
    52                 /* update the entry */ 
    53                 $sql = sprintf("UPDATE settings SET settingvalue = '%s' WHERE settingname = 'dbversion'", $version); 
    54         } else { 
    55                 /* create the entry */ 
    56                 $sql = sprintf("INSERT INTO settings (settingname, settingvalue) VALUES ('dbversion', '%s')", $version); 
    57         } 
    58         $res = $GLOBALS["common"]->db->query($sql); 
    59 } 
     28require_once("common/mvblog_upgrade.php"); 
     29$updater = new Mvblog_upgrade(); 
    6030 
    61 echo "looking up files for db type <b>".$common->db->phptype."</b> starting at version <b>".$common->settings["dbversion"]."</b>...<br>"; 
    62 $patches = get_patchfiles($common->settings["dbversion"], $common->db->phptype); 
     31if (array_key_exists("dbversion", $updater->settings["dbversion"]) && $updater->settings["dbversion"]) 
     32        $latest_version = $updater->settings["dbversion"]; 
     33else 
     34        $latest_version = 0; 
     35 
     36echo "looking up files for db type <b>".$updater->db->phptype."</b> starting at version <b>".$latest_version."</b>...<br>"; 
     37$patches = $updater->get_patchfiles($mode); 
    6338if (!count($patches)) 
    6439        die("nothing to do. Your mvblog is already up-to-date."); 
    6540 
    6641foreach ($patches as $patch) { 
    67         echo "applying patchfile $patch..."; 
    68         require_once("upgrades/".$common->db->phptype."/$patch"); 
    69         foreach ($sql as $query) { 
    70                 $common->db->query($query); 
    71                 if (PEAR::isError($res)) 
    72                         die($res->getMessage()); 
    73         } 
    74         echo "done.<br>"; 
     42        if (!$updater->apply_dbpatch($patch, $mode)) 
     43                die("something went wrong with patchfile $patch. Rollback to backup."); 
     44        echo "applied patchfile $patch. <br>"; 
    7545        /* put last version in var so we can update settings table later */ 
    76         $latest = substr($patch, strpos($patch, "to")+2, -4); 
    77         if ($latest > $latest_version) 
    78                 $latest_version = $latest; 
     46        if ($updater->lastpatch > $latest_version) 
     47                $latest_version = $updater->lastpatch; 
    7948} 
    80 set_latest_version($latest_version); 
     49$updater->set_latest_version($latest_version); 
    8150echo "Your MvBlog installation is updated. Have fun."; 
    8251?>