Fixes a bug with sequences when a new postgres database is just created, a=chris

Chris Pollett [2014-06-28 19:Jun:th]
Fixes a bug with sequences when a new postgres database is just created, a=chris
Filename
configs/createdb.php
models/profile_model.php
diff --git a/configs/createdb.php b/configs/createdb.php
index a66d2a79f..50dd27a3b 100755
--- a/configs/createdb.php
+++ b/configs/createdb.php
@@ -734,9 +734,33 @@ $db->execute("INSERT INTO TRANSLATION_LOCALE VALUES
         (1004, 16, '新闻' )");
 $db->execute("INSERT INTO TRANSLATION_LOCALE VALUES
         (1004, 20, 'اخبار' )");
+
+if(stristr(DB_HOST, "pgsql") !== false) {
+    /* For postgres count initial values of SERIAL sequences
+       will be screwed up unless do
+     */
+    $auto_tables = array("ACTIVITY" =>"ACTIVITY_ID",
+        "GROUP_ITEM" =>"GROUP_ITEM_ID", "GROUP_PAGE" => "GROUP_PAGE_ID",
+        "GROUPS" => "GROUP_ID", "LOCALE"=> "LOCALE_ID", "ROLE" => "ROLE_ID",
+        "TRANSLATION" => "TRANSLATION_ID", "USERS" => "USER_ID");
+    foreach($auto_tables as $table => $auto_column) {
+        $sql = "SELECT MAX($auto_column) AS NUM FROM $table";
+        $result = $db->execute($sql);
+        $row = $db->fetchArray($result);
+        $next = $row['NUM'];
+        $sequence = strtolower("{$table}_{$auto_column}_seq");
+        $sql = "SELECT setval('$sequence', $next)";
+        $db->execute($sql);
+        $sql = "SELECT nextval('$sequence')";
+        $db->execute($sql);
+    }
+}
+
 $db->disconnect();
 if(in_array(DBMS, array('sqlite','sqlite3' ))){
     chmod(CRAWL_DIR."/data/".DB_NAME.".db", 0666);
 }
+
+
 echo "Create DB succeeded\n";
 ?>
diff --git a/models/profile_model.php b/models/profile_model.php
index df7843842..504959208 100755
--- a/models/profile_model.php
+++ b/models/profile_model.php
@@ -408,6 +408,25 @@ EOT;
             if(!$this->copyTable($table_or_index, $default_dbm, $test_dbm))
                 {return false;}
         }
+        if(stristr($dbinfo["DB_HOST"], "pgsql") !== false) {
+            /* For postgres count initial values of SERIAL sequences
+               will be screwed up unless do
+             */
+            $auto_tables = array("ACTIVITY" =>"ACTIVITY_ID",
+                "GROUP_ITEM" =>"GROUP_ITEM_ID", "GROUP_PAGE" => "GROUP_PAGE_ID",
+                "GROUPS" => "GROUP_ID", "LOCALE"=> "LOCALE_ID",
+                "ROLE" => "ROLE_ID", "TRANSLATION" => "TRANSLATION_ID",
+                "USERS" => "USER_ID");
+            foreach($auto_tables as $table => $auto_column) {
+                $sql = "SELECT MAX($auto_column) AS NUM FROM $table";
+                $result = $test_dbm->execute($sql);
+                $row = $test_dbm->fetchArray($result);
+                $next = $row['NUM'];
+                $sequence = strtolower("{$table}_{$auto_column}_seq");
+                $sql = "SELECT setval('$sequence', $next)";
+                $test_dbm->execute($sql);
+            }
+        }
         return true;
     }
     /**
ViewGit