From: moodler <moodler>
Date: Mon, 9 Aug 2004 16:00:10 +0000 (+0000)
Subject: Added Petri's patch that allows us to finally change type of PostgreSQL columns!
X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=d05024a72bf35ec49478711e75aefe28ed8da847;p=moodle.git

Added Petri's patch that allows us to finally change type of PostgreSQL columns!

Yay!   Thanks, Petri!

Bug 1721
---

diff --git a/lib/datalib.php b/lib/datalib.php
index 7fbcbabf0c..2e27832d07 100644
--- a/lib/datalib.php
+++ b/lib/datalib.php
@@ -136,7 +136,8 @@ function table_column($table, $oldfield, $field, $type="integer", $size="10",
             $dbver = substr($dbinfo['version'],0,3);
             
             //to prevent conflicts with reserved words
-            $field = "\"$field\"";
+            $realfield = "\"$field\"";
+            $field = "\"${field}_alter_column_tmp\"";
             $oldfield = "\"$oldfield\"";
 
             switch (strtolower($type)) {
@@ -163,14 +164,14 @@ function table_column($table, $oldfield, $field, $type="integer", $size="10",
             //    $after = "AFTER '$after'";
             //}
 
-            if ($oldfield != "\"\"") {
-                if ($field != $oldfield) {
-                    execute_sql("ALTER TABLE {$CFG->prefix}$table RENAME COLUMN $oldfield TO $field");
-                }
-            } else {
-                execute_sql("ALTER TABLE {$CFG->prefix}$table ADD COLUMN $field $type");
-                execute_sql("UPDATE {$CFG->prefix}$table SET $field=$default");
-            }
+            //Use transactions
+            execute_sql("BEGIN");
+
+            //Allways use temporaly column
+            execute_sql("ALTER TABLE {$CFG->prefix}$table ADD COLUMN $field $type");
+            //Add default values
+            execute_sql("UPDATE {$CFG->prefix}$table SET $field=$default");
+             
 
             if ($dbver >= "7.3") {
                 // modifying 'not null' is posible before 7.3
@@ -183,8 +184,16 @@ function table_column($table, $oldfield, $field, $type="integer", $size="10",
                 }
             }
 
-            return execute_sql("ALTER TABLE {$CFG->prefix}$table ALTER COLUMN $field SET DEFAULT $default");
+            execute_sql("ALTER TABLE {$CFG->prefix}$table ALTER COLUMN $field SET DEFAULT $default");
+            
+            if ( $oldfield != "\"\"" ) {
+                execute_sql("UPDATE {$CFG->prefix}$table SET $field = $oldfield");
+                execute_sql("ALTER TABLE  {$CFG->prefix}$table drop column $oldfield");
+            }
 
+            execute_sql("ALTER TABLE {$CFG->prefix}$table RENAME COLUMN $field TO $realfield"); 
+            
+            return execute_sql("COMMIT");
             break;
 
         default: