diff managedb.php @ 551:bed441ced78e

Improve migration and upgrade procedures.
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 16 Dec 2013 01:42:48 +0200
parents e5732b25c839
children 14078b65641c
line wrap: on
line diff
--- a/managedb.php	Mon Dec 16 01:29:54 2013 +0200
+++ b/managedb.php	Mon Dec 16 01:42:48 2013 +0200
@@ -495,6 +495,8 @@
 
   foreach ($settings as $key => $data)
   {
+    $sql = false;
+
     // Get setting type
     switch ($data[0])
     {
@@ -505,24 +507,28 @@
       default:      die("Invalid type in default settings '".$key."', type=".$data[0]."\n");
     }
 
-    // Map the key, in case the name has changed
-    $inKey = stUpgradeMap("key", $key);
-    $inTable = stUpgradeMap("table", $table);
-
     // Check if we are upgrading
-    if ($upgrade && ($res = stDBFetchSQL($inDB, stDBPrepareSQL($inDB, "SELECT * FROM ".$inTable." WHERE key=%s", $inKey))) !== FALSE)
+    if ($upgrade)
     {
-      // Yup, upgrade the data, if we can
-      echo ".";
+      // Map the key, in case the name has changed
+      $inKey = stUpgradeMap("key", $key);
+      $inTable = stUpgradeMap("table", $table);
 
-      if ($res["vtype"] != $data[0])
-        die("Oops! Data type of '".$key."' does not match in table '".$table.". DB upgrade failed.\n");
+      if (($res = stDBFetchSQL($inDB, stDBPrepareSQL($inDB, "SELECT * FROM ".$inTable." WHERE key=%s", $inKey))) !== FALSE)
+      {
+        // Yup, upgrade the data, if we can
+        echo ".";
 
-      $sql = stDBPrepareSQL($outDB,
-        "INSERT INTO ".$table." (key,vtype,".$var.",sdesc) VALUES (%s,%d,".$type.",%s)",
-        $key, $data[0], $res[$var], $data[2]);
+        if ($res["vtype"] != $data[0])
+          die("Oops! Data type of '".$key."' does not match in table '".$table.". DB upgrade failed.\n");
+
+        $sql = stDBPrepareSQL($outDB,
+          "INSERT INTO ".$table." (key,vtype,".$var.",sdesc) VALUES (%s,%d,".$type.",%s)",
+          $key, $data[0], $res[$var], $data[2]);
+      }
     }
-    else
+
+    if ($sql === false)
     {
       // Normal insertion of default data
       echo "+";
@@ -599,7 +605,7 @@
 }
 
 
-function stMigrateTables($inDB, $outDB, $excluded)
+function stMigrateTables($inDB, $outDB, $upgrade, $excluded)
 {
   global $sqlTables;
 
@@ -609,7 +615,7 @@
   
   foreach ($sqlTables as $name => $schema)
   {
-    $inTable = stUpgradeMap("table", $name);
+    $inTable = $upgrade ? stUpgradeMap("table", $name) : $name;
 
     if (!in_array($inTable, $excluded) && !in_array($name, $excluded))
     {
@@ -718,6 +724,8 @@
 
 // Act according to specified command
 $addTestData = FALSE;
+$upgrade = FALSE;
+
 switch (stCArgLC(1))
 {
   case "test":
@@ -748,6 +756,8 @@
     break;
 
   case "upgrade":
+    $upgrade = TRUE;
+
   case "migrate":
     //
     // Attempt to upgrade database
@@ -790,16 +800,16 @@
         exit;
 
       // Migrate data from setting tables ..
-      if (!stAddSettings($inDB, $outDB, $siteDefaults, "settings", TRUE))
+      if (!stAddSettings($inDB, $outDB, $siteDefaults, "settings", $upgrade))
         exit;
 
-      if (!stAddSettings($inDB, $outDB, $siteDisplayVars, "display_vars", TRUE))
+      if (!stAddSettings($inDB, $outDB, $siteDisplayVars, "display_vars", $upgrade))
         exit;
 
-      stAddSettings($inDB, $outDB, $dbMeta, "dbmeta", TRUE);
+      stAddSettings($inDB, $outDB, $dbMeta, "dbmeta", $upgrade);
 
       // Migrate other tables
-      if (!stMigrateTables($inDB, $outDB, array("settings", "display_vars", "dbmeta")))
+      if (!stMigrateTables($inDB, $outDB, $upgrade, array("settings", "display_vars", "dbmeta")))
         exit;
 
       echo "Setting dbVersion.\n";