changeset 504:a491865e0684

Implement tables migration.
author Matti Hamalainen <ccr@tnsp.org>
date Sat, 07 Dec 2013 19:35:01 +0200
parents 3232f682f0d2
children f66aa0cdb666
files createdb.php
diffstat 1 files changed, 37 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/createdb.php	Sat Dec 07 18:56:52 2013 +0200
+++ b/createdb.php	Sat Dec 07 19:35:01 2013 +0200
@@ -371,7 +371,7 @@
   foreach ($data as $col)
     $res[] = implode(" ", $col);
 
-  return implode(",", $res);
+  return implode(", ", $res);
 }
 
 
@@ -493,6 +493,21 @@
 }
 
 
+function stGetSQLTypeParam($dbh, $def, $value)
+{
+  switch (substr($def, 0, 3))
+  {
+    case "INT":
+      return intval($value);
+
+    case "VAR":
+    case "TEX":
+      return $dbh->quote($value);
+
+    default: die("Unknown type ".$col[1].".\n");
+  }
+}
+
 function stMigrateTables($inDB, $outDB, $excluded)
 {
   global $sqlTables;
@@ -500,7 +515,27 @@
   foreach ($sqlTables as $name => $schema)
   if (!in_array($name, $excluded))
   {
-    echo " - '".$name."'\n";
+    echo " - '".$name."' ";
+
+    foreach (stDBExecSQL($inDB, "SELECT * FROM ".$name) as $row)
+    {
+      $avals = array();
+      $acols = array();
+      
+      foreach ($schema as $col)
+      {
+        if (isset($row[$col[0]]))
+        {
+          $avals[] = stGetSQLTypeParam($outDB, $col[1], $row[$col[0]]);
+          $acols[] = $col[0];
+        }
+      }
+
+      $sql = "INSERT INTO ".$name." (".implode(",", $acols).") VALUES (".implode(",", $avals).")";
+      if (stDBExecSQL($outDB, $sql) === false) die("Error!");
+      echo ".";
+    }
+    echo "\n";
   }
   return TRUE;
 }