changeset 519:6aa9356ead52

Improve migration.
author Matti Hamalainen <ccr@tnsp.org>
date Sun, 08 Dec 2013 17:43:52 +0200
parents af2f39a1dd79
children 6e9d03f10328
files admajax.php createdb.php
diffstat 2 files changed, 46 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/admajax.php	Sun Dec 08 17:28:55 2013 +0200
+++ b/admajax.php	Sun Dec 08 17:43:52 2013 +0200
@@ -1207,7 +1207,7 @@
               break;
           }
           echo "</td>\n".
-          "  <td>".$item["desc"]."</td>\n".
+          "  <td>".$item["sdesc"]."</td>\n".
           " </tr>\n";
         }
         echo "</table>\n".stGetSaveButton();
@@ -1215,7 +1215,7 @@
         foreach (stExecSQL("SELECT * FROM settings WHERE vtype=".VT_TEXT) as $item)
         {
           echo
-            "<h2>".chentities($item["desc"])."</h2>\n".
+            "<h2>".chentities($item["sdesc"])."</h2>\n".
             stGetFormTextArea(10, 60, "", $item["key"], $prefix, $item["vtext"]).
             "\n<br />\n".
             stGetSaveButton();
--- a/createdb.php	Sun Dec 08 17:28:55 2013 +0200
+++ b/createdb.php	Sun Dec 08 17:43:52 2013 +0200
@@ -171,7 +171,7 @@
     array("vstr"         , "VARCHAR(128)"),
     array("vtext"        , "TEXT"),
     array("vint"         , "INT"),
-    array("desc"         , "VARCHAR(128)"),
+    array("sdesc"        , "VARCHAR(128)"),
   ),
 
   // Site settings
@@ -181,7 +181,7 @@
     array("vstr"         , "VARCHAR(128)"),
     array("vtext"        , "TEXT"),
     array("vint"         , "INT"),
-    array("desc"         , "VARCHAR(128)"),
+    array("sdesc"        , "VARCHAR(128)"),
   ),
 
   "news" => array(
@@ -245,7 +245,7 @@
     array("vstr"         , "VARCHAR(128)"),
     array("vtext"        , "TEXT"),
     array("vint"         , "INT"),
-    array("desc"         , "VARCHAR(128)"),
+    array("sdesc"        , "VARCHAR(128)"),
   ),
 
   "displaySlides" => array(
@@ -456,6 +456,9 @@
 {
   echo ($upgrade ? "Adding settings to" : "Upgrading settings in")." '".$table."' table.";
 
+  $status = TRUE;
+  stDBExecSQL($outDB, "BEGIN TRANSACTION");
+
   foreach ($settings as $key => $data)
   {
     // Get setting type
@@ -481,7 +484,7 @@
         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.",desc) VALUES (%s,%d,".$type.",%s)",
+        "INSERT INTO ".$table." (key,vtype,".$var.",sdesc) VALUES (%s,%d,".$type.",%s)",
         $key, $data[0], stUpgradeMap("value", $key, $res[$var]), $data[2]);
     }
     else
@@ -489,23 +492,32 @@
       // Normal insertion of default data
       echo "+";
       $sql = stDBPrepareSQL($outDB,
-        "INSERT INTO ".$table." (key,vtype,".$var.",desc) VALUES (%s,%d,".$type.",%s)",
+        "INSERT INTO ".$table." (key,vtype,".$var.",sdesc) VALUES (%s,%d,".$type.",%s)",
         $key, $data[0], $data[1], $data[2]);
     }
 
     if (stDBExecSQL($outDB, $sql) === FALSE)
-      return FALSE;
+    {
+      $status = FALSE;
+      break;
+    }
   }
   echo "\n";
-  return TRUE;
-}
+
+  stDBExecSQL($outDB, "COMMIT");
+  return $status;
+} 
 
 
-function stAddTestData($dbh)
+function stAddTestData($outDB)
 {
   global $siteTestData;
+
   echo "Adding test data.\n";
 
+  $status = TRUE;
+  stDBExecSQL($outDB, "BEGIN TRANSACTION");
+
   foreach ($siteTestData as $table => $data)
   {
     echo " - ".$table."...\n";
@@ -514,11 +526,15 @@
       for ($n = 2; $n < count($data); $n++)
       {
         $arr = array_merge(
-          array($dbh, "INSERT INTO ".$table." (".$data[0].") VALUES (".$data[1].")"),
+          array($outDB, "INSERT INTO ".$table." (".$data[0].") VALUES (".$data[1].")"),
           $data[$n]);
 
         $sql = call_user_func_array('stDBPrepareSQL', $arr);
-        stDBExecSQL($dbh, $sql);
+        if (stDBExecSQL($outDB, $sql) === false)
+        {
+          $status = false;
+          break;
+        }
       }
     }
     else
@@ -526,6 +542,9 @@
       echo "  Invalid table / data definition.\n";
     }
   }
+
+  stDBExecSQL($outDB, "COMMIT");
+  return $status;
 }
 
 
@@ -544,10 +563,15 @@
   }
 }
 
+
 function stMigrateTables($inDB, $outDB, $excluded)
 {
   global $sqlTables;
+
   echo "Migrating tables...\n";
+  $status = TRUE;
+  stDBExecSQL($outDB, "BEGIN TRANSACTION");
+  
   foreach ($sqlTables as $name => $schema)
   if (!in_array($name, $excluded))
   {
@@ -568,12 +592,19 @@
       }
 
       $sql = "INSERT INTO ".$name." (".implode(",", $acols).") VALUES (".implode(",", $avals).")";
-      if (stDBExecSQL($outDB, $sql) === false) die("Error!");
+      if (stDBExecSQL($outDB, $sql) === false)
+      {
+        $status = FALSE;
+        break;
+      }
       echo ".";
     }
     echo "\n";
   }
-  return TRUE;
+
+  stDBExecSQL($outDB, "COMMIT");
+
+  return $status;
 }