diff managedb.php @ 553:f13e7a513db1

Fix sequences after database upgrade or migration.
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 16 Dec 2013 02:51:19 +0200
parents 14078b65641c
children 206196b610fd
line wrap: on
line diff
--- a/managedb.php	Mon Dec 16 01:48:22 2013 +0200
+++ b/managedb.php	Mon Dec 16 02:51:19 2013 +0200
@@ -611,40 +611,67 @@
 
   echo "Migrating tables...\n";
   $status = TRUE;
+  $dbName = $outDB->getAttribute(PDO::ATTR_DRIVER_NAME);
   stDBExecSQL($outDB, "BEGIN TRANSACTION");
   
-  foreach ($sqlTables as $name => $schema)
+  foreach ($sqlTables as $table => $schema)
   {
-    $inTable = $upgrade ? stUpgradeMap("table", $name) : $name;
+    // Do not migrate excluded tables (or excluded tables with old names)
+    $sequences = array();
+    $inTable = $upgrade ? stUpgradeMap("table", $table) : $table;
+    if (in_array($inTable, $excluded) || in_array($table, $excluded))
+      continue;
 
-    if (!in_array($inTable, $excluded) && !in_array($name, $excluded))
+    // Process each row of the input table
+    echo " - '".$table."' ";
+    foreach (stDBExecSQL($inDB, "SELECT * FROM ".$inTable) as $row)
     {
-      echo " - '".$name."' ";
-
-      foreach (stDBExecSQL($inDB, "SELECT * FROM ".$inTable) as $row)
+      // Convert to new schema, as needed
+      $avals = array();
+      $acols = array();
+      
+      foreach ($schema as $col)
       {
-        $avals = array();
-        $acols = array();
-        
-        foreach ($schema as $col)
+        // If input has schema column, add it to output
+        if (isset($row[$col[0]]))
         {
-          if (isset($row[$col[0]]))
+          $avals[] = stGetSQLTypeParam($outDB, $col[1], $row[$col[0]]);
+          $acols[] = $col[0];
+        }
+
+        // If the column should have autoincrement, add it to sequences
+        if (in_array("AUTOINCREMENT", $col))
+          $sequences[] = $col[0];
+      }
+
+      $sql = "INSERT INTO ".$table." (".implode(",", $acols).") VALUES (".implode(",", $avals).")";
+      if (stDBExecSQL($outDB, $sql) === false)
+      {
+        $status = FALSE;
+        break;
+      }
+      echo ".";
+    }
+
+    // Perform some specialities here
+    switch ($dbName)
+    {
+      case "pgsql":
+        // For PostgreSQL we need to update sequences
+        foreach ($sequences as $seq)
+        {
+          if (($max_id = stDBFetchSQLColumn($outDB, "SELECT MAX(".$seq.") FROM ".$table)) !== false &&
+              ($seq_id = stDBFetchSQLColumn($outDB, "SELECT nextval('".$table."_".$seq."_seq')")) !== false &&
+              ($seq_id <= $max_id))
           {
-            $avals[] = stGetSQLTypeParam($outDB, $col[1], $row[$col[0]]);
-            $acols[] = $col[0];
+            echo "SEQFIX";
+            stDBExecSQL($outDB, "SELECT setval('".$table."_".$seq."_seq', ".$max_id.")");
           }
         }
+        break;
+    }
 
-        $sql = "INSERT INTO ".$name." (".implode(",", $acols).") VALUES (".implode(",", $avals).")";
-        if (stDBExecSQL($outDB, $sql) === false)
-        {
-          $status = FALSE;
-          break;
-        }
-        echo ".";
-      }
-      echo "\n";
-    }
+    echo "\n";
   }
 
   stDBExecSQL($outDB, "COMMIT");
@@ -811,7 +838,7 @@
       // Migrate other tables
       if (!stMigrateTables($inDB, $outDB, $upgrade, array("settings", "display_vars", "dbmeta")))
         exit;
-
+      
       echo "Setting dbVersion.\n";
       stSetDBMeta($outDB, "dbVersion", $dbVersion);
       echo "Upgrade complete.\n";