? cvs_get_vanilla.sh
? database/updates.inc-1.114
Index: database/database.pgsql
===================================================================
RCS file: /cvs/drupal/drupal/database/database.pgsql,v
retrieving revision 1.133
diff -u -p -r1.133 database.pgsql
--- database/database.pgsql	16 Aug 2005 18:06:18 -0000	1.133
+++ database/database.pgsql	20 Aug 2005 14:30:35 -0000
@@ -458,10 +458,6 @@ CREATE INDEX node_counter_totalcount_idx
 CREATE INDEX node_counter_daycount_idx ON node_counter(daycount);
 CREATE INDEX node_counter_timestamp_idx ON node_counter(timestamp);
 
---
--- Table structure for table 'url_alias'
---
-
 CREATE TABLE profile_fields (
   fid serial,
   title varchar(255) default NULL,
@@ -492,13 +488,17 @@ CREATE TABLE profile_values (
 CREATE INDEX profile_values_uid ON profile_values (uid);
 CREATE INDEX profile_values_fid ON profile_values (fid);
 
+--
+-- Table structure for table 'url_alias'
+--
+
 CREATE TABLE url_alias (
   pid serial,
   src varchar(128) NOT NULL default '',
   dst varchar(128) NOT NULL default '',
   PRIMARY KEY (pid)
 );
-CREATE INDEX url_alias_dst_idx ON url_alias(dst);
+CREATE UNIQUE INDEX url_alias_dst_idx ON url_alias(dst);
 CREATE INDEX url_alias_src ON url_alias(src);
 --
 -- Table structure for permission
@@ -569,10 +569,9 @@ CREATE INDEX search_index_word_idx ON se
 --
 
 CREATE TABLE search_total (
-  word varchar(50) NOT NULL default '',
-  count float default NULL
+  word varchar(50) NOT NULL default '' PRIMARY KEY,
+  count int default NULL
 );
-CREATE INDEX search_total_word_idx ON search_total(word);
 
 --
 -- Table structure for sessions
Index: database/updates.inc
===================================================================
RCS file: /cvs/drupal/drupal/database/updates.inc,v
retrieving revision 1.128
diff -u -p -r1.128 updates.inc
--- database/updates.inc	16 Aug 2005 20:17:54 -0000	1.128
+++ database/updates.inc	20 Aug 2005 14:30:35 -0000
@@ -360,11 +360,9 @@ function update_121() {
 }
 
 function update_122() {
-
   $ret = array();
   $ret[] = update_sql("ALTER TABLE {blocks} ADD types text");
   return $ret;
-
 }
 
 function update_123() {
@@ -490,28 +488,17 @@ function update_128() {
 
 function update_129() {
   $ret = array();
-
-  if ($GLOBALS['db_type'] == 'mysql') {
-    $ret[] = update_sql("ALTER TABLE {vocabulary} ADD tags tinyint(3) unsigned default '0' NOT NULL");
-  }
-  elseif ($GLOBALS['db_type'] == 'pgsql') {
-    $ret[] = update_sql("ALTER TABLE {vocabulary} ADD tags smallint default '0' NOT NULL");
-  }
-
+  db_add_column($ret, 'vocabulary', 'tags', 'TINYINT', TRUE, "0", TRUE); 
   return $ret;
 }
 
 function update_130() {
   $ret = array();
-  if ($GLOBALS['db_type'] == 'mysql') {
-    $ret[] = update_sql("ALTER TABLE {sessions} ADD cache int(11) NOT NULL default '0' AFTER timestamp");
-  }
-  elseif ($GLOBALS['db_type'] == 'pgsql') {
-    $ret[] = update_sql("ALTER TABLE {sessions} ADD cache int(11) NOT NULL default '0' AFTER timestamp");
-  }
+  db_add_column($ret, 'sessions', 'cache', 'int', TRUE, "0"); 
   return $ret;
 }
 
+// If I understand we don't want title to be UNIQE
 function update_131() {
   $ret = array();
 
@@ -520,8 +507,8 @@ function update_131() {
     $ret[] = update_sql("ALTER TABLE {boxes} ADD INDEX title (title)");
   }
   elseif ($GLOBALS['db_type'] == 'pgsql') {
-    $ret[] = update_sql("DROP INDEX boxes_title_idx");;
-    $ret[] = update_sql("CREATE INDEX title ON {boxes} (title)");
+    $ret[] = update_sql("ALTER TABLE {boxes} DROP CONSTRAINT {boxes}_title_key");
+    $ret[] = update_sql("CREATE INDEX {boxes}_title_idx ON {boxes}(title)");
   }
 
   return $ret;
@@ -536,9 +523,8 @@ function update_132() {
   if ($GLOBALS['db_type'] == 'pgsql') {
     $ret[] = update_sql('DROP TABLE {search_total}');
     $ret[] = update_sql("CREATE TABLE {search_total} (
-              word varchar(50) NOT NULL default '',
-              count float default NULL)");
-    $ret[] = update_sql('CREATE INDEX {search_total}_word_idx ON {search_total}(word)');
+              word varchar(50) NOT NULL default '' PRIMARY KEY,
+              count int default NULL)");
 
     /**
      * Wipe the search index
@@ -551,31 +537,33 @@ function update_132() {
 }
 
 function update_133() {
-  $ret[] = update_sql("CREATE TABLE {contact} (
-    subject varchar(255) NOT NULL default '',
-    recipients longtext NOT NULL default '',
-    reply longtext NOT NULL default ''
-    )");
-
+  $ret = array();
   if ($GLOBALS['db_type'] == 'mysql') {
-    $ret[] = update_sql("ALTER TABLE {users} ADD login int(11) NOT NULL default '0'");
+    $ret[] = update_sql("CREATE TABLE {contact} (
+      subject varchar(255) NOT NULL default '',
+      recipients longtext NOT NULL default '',
+      reply longtext NOT NULL default ''
+      )");
   }
   elseif ($GLOBALS['db_type'] == 'pgsql') {
-    $ret[] = update_sql("ALTER TABLE {users} ADD login integer");
-    $ret[] = update_sql("ALTER TABLE {users} ALTER COLUMN login SET NOT NULL");
-    $ret[] = update_sql("ALTER TABLE {users} ALTER COLUMN login SET DEFAULT '0'");
-  }
+    $ret[] = update_sql("CREATE TABLE {contact} (
+      subject varchar(255) NOT NULL default '',
+      recipients text NOT NULL default '',
+      reply text NOT NULL default ''
+      )");
 
+  }
+  db_add_column($ret, 'users', 'login', 'int', TRUE, "0");
   return $ret;
 }
 
 function update_134() {
+  $ret = array();
   if ($GLOBALS['db_type'] == 'mysql') {
     $ret[] = update_sql('ALTER TABLE {blocks} DROP types');
   }
-  else {
-    // Postgres can only drop columns since 7.4
-    #$ret[] = update_sql('ALTER TABLE {blocks} DROP types');
+  elseif ($GLOBALS['db_type'] == 'pgsql' and drupal_pg_version() >= 703) {
+    $ret[] = update_sql('ALTER TABLE {blocks} DROP types');
   }
   return $ret;
 }
@@ -589,9 +577,17 @@ function update_135() {
   return array();
 }
 
+// FIXME: mysql: "changed" was a KEY(), is it dropped after CHANGE?
 function update_136() {
   $ret = array();
-  $ret[] = update_sql("ALTER TABLE {users} CHANGE COLUMN changed access int(11) NOT NULL default '0'");
+  if ($GLOBALS['db_type'] == 'mysql') {
+    $ret[] = update_sql("ALTER TABLE {users} CHANGE COLUMN changed access int(11) NOT NULL default '0'");
+  }
+  elseif ($GLOBALS['db_type'] == 'pgsql') {
+    $ret[] = update_sql("ALTER TABLE {users} RENAME COLUMN changed TO access");
+    $ret[] = update_sql("DROP INDEX {users}_changed_idx");
+    $ret[] = update_sql('CREATE INDEX {users}_access_idx ON {users}(access)');
+  }
   $ret[] = update_sql('UPDATE {users} SET access = login WHERE login > created');
   $ret[] = update_sql('UPDATE {users} SET access = created WHERE access = 0');
   return $ret;
@@ -599,18 +595,7 @@ function update_136() {
 
 function update_137() {
   $ret = array();
-
-  if ($GLOBALS['db_type'] == 'mysql') {
-    $ret[] = update_sql("ALTER TABLE {locales_source} CHANGE location location varchar(255) NOT NULL default ''");
-  }
-  elseif ($GLOBALS['db_type'] == 'pgsql') {
-    $ret[] = update_sql("ALTER TABLE {locales_source} RENAME location TO location_old");
-    $ret[] = update_sql("ALTER TABLE {locales_source} ADD location varchar(255)");
-    $ret[] = update_sql("UPDATE {locales_source} SET location = location_old");
-    $ret[] = update_sql("ALTER TABLE {locales_source} ALTER location SET NOT NULL");
-    $ret[] = update_sql("ALTER TABLE {locales_source} ALTER location SET DEFAULT ''");
-    $ret[] = update_sql("ALTER TABLE {locales_source} DROP location_old");
-  }
+  db_change_column($ret, 'locales_source', 'location', 'VARCHAR(255)', TRUE, "");
   return $ret;
 }
 
@@ -623,7 +608,7 @@ function update_138() {
 
 function update_139() {
   $ret = array();
-  $ret[] = update_sql("ALTER TABLE {accesslog} ADD timer int(10) unsigned NOT NULL default '0'");
+  db_add_column($ret, 'accesslog', 'timer', 'int', TRUE, "0", TRUE); 
   return $ret;
 }
 
@@ -634,7 +619,7 @@ function update_140() {
     $ret[] = update_sql("ALTER TABLE {url_alias} ADD INDEX (src)");
   }
   elseif ($GLOBALS['db_type'] == 'pgsql') {
-    $ret[] = update_sql("CREATE INDEX url_alias_src ON {url_alias}(src)");
+    $ret[] = update_sql("CREATE INDEX {url_alias}_src ON {url_alias}(src)");
   }
   return $ret;
 }
@@ -649,7 +634,7 @@ function update_141() {
 
 function update_142() {
   $ret = array();
-  $ret[] = update_sql("ALTER TABLE {watchdog} ADD COLUMN referer varchar(128) NOT NULL");
+  db_add_column($ret, 'watchdog', 'referer', 'VARCHAR(128)', TRUE, ''); 
   return $ret;
 }
 
@@ -669,25 +654,16 @@ function update_143() {
 
 function update_144() {
   $ret = array();
-  if ($GLOBALS['db_type'] == 'mysql') {
-    $ret[] = update_sql("ALTER TABLE {node} CHANGE type type VARCHAR(32) NOT NULL");
-  }
-  elseif ($GLOBALS['db_type'] == 'pgsql') {
-    $ret[] = update_sql("ALTER TABLE {node} RENAME type TO type_old");
-    $ret[] = update_sql("ALTER TABLE {node} ADD type varchar(32)");
-    $ret[] = update_sql("ALTER TABLE {node} ALTER type SET NOT NULL");
-    $ret[] = update_sql("ALTER TABLE {node} ALTER type SET DEFAULT ''");
-    $ret[] = update_sql("UPDATE {node} SET type = type_old");
-    $ret[] = update_sql("ALTER TABLE {node} DROP type_old");
-  }
+  db_change_column($ret, 'node', 'type', 'VARCHAR(32)', TRUE); 
   return $ret;
 }
 
 function update_145() {
   $default_theme = variable_get('theme_default', 'bluemarine');
   $ret = array();
-  $ret[] = update_sql("ALTER TABLE {blocks} CHANGE region region varchar(64) default 'left' NOT NULL");
-  $ret[] = update_sql("ALTER TABLE {blocks} ADD theme varchar(255) NOT NULL default ''");
+  
+  db_change_column($ret, 'blocks', 'region', 'VARCHAR(64)', TRUE, "left");
+  db_add_column($ret, 'blocks', 'theme', 'VARCHAR(255)', TRUE, "");
 
   // Intialize block data for default theme
   $ret[] = update_sql("UPDATE {blocks} SET region = 'left' WHERE region = '0'");
@@ -705,6 +681,98 @@ function update_145() {
   return $ret;
 }
 
+
+/**
+ * Adds a column.
+ */
+// TODO: what to do when adding column with NOT NULL and default is not set?
+function db_add_column(&$ret, $table, $column, $type, $not_null = FALSE, $default = FALSE, $unsigned = FALSE) {
+  $pg_type = (preg_match('/int/i', $type)) ? 'int' : $type;
+  if (is_null($default)) {
+    $default_val = 'NULL'; 
+    $default = 'DEFAULT NULL';
+  }
+  elseif ($defaul === FALSE) {
+    $default = '';
+  } 
+  else {
+    $default_val = "'$default'"; 
+    $default = "DEFAULT '$default'";
+  }
+  if ($not_null) { $not_null = "NOT NULL"; }
+  if ($unsigned) { $unsigned = 'unsigned'; }
+  
+  if ($GLOBALS['db_type'] == 'mysql') {
+    $ret[] = update_sql("ALTER TABLE {$table} ADD $column $type $not_null $default $unsigned");
+  }
+  elseif ($GLOBALS['db_type'] == 'pgsql') {
+    $ret[] = update_sql("ALTER TABLE {$table} ADD $column $pg_type");
+    if ($default) {
+      $ret[] = update_sql("ALTER TABLE {$table} ALTER $column SET $default");
+    }
+    if ($not_null) {
+      $ret[] = update_sql("UPDATE {$table} SET $column = $default_val");
+      $ret[] = update_sql("ALTER TABLE {$table} ALTER $column SET NOT NULL");
+    }
+  }
+}
+
+/**
+ * Changes $column definition.
+ */
+// TODO: Question: when changing a column and adding NOT NULL - should we set existing NULL values to DEFAULT, if it exists?
+function db_change_column(&$ret, $table, $column, $type, $not_null = FALSE, $default = FALSE, $unsigned = FALSE) {
+  $pg_type = (preg_match('/int/i', $type)) ? 'int' : $type;
+  if (is_null($default)) {
+    $default_val = 'NULL'; 
+    $default = 'DEFAULT NULL';
+  }
+  elseif ($defaul === FALSE) {
+    $default = '';
+  } 
+  else {
+    $default_val = "'$default'"; 
+    $default = "DEFAULT '$default'";
+  }
+  if ($GLOBALS['db_type'] == 'mysql') {
+    if ($not_null) { $not_null = "NOT NULL"; }
+    if ($unsigned) { $unsigned = 'unsigned'; }
+    $ret[] = update_sql("ALTER TABLE {$table} CHANGE $column $column $type $not_null $default $unsigned");
+  }
+  elseif ($GLOBALS['db_type'] == 'pgsql') {
+    $ret[] = update_sql("ALTER TABLE {$table} RENAME $column TO {$column}_old");
+    $ret[] = update_sql("ALTER TABLE {$table} ADD $column $pg_type");
+    $ret[] = update_sql("UPDATE {$table} SET $column = {$column}_old");
+    if ($not_null) {
+      $ret[] = update_sql("ALTER TABLE {$table} ALTER $column SET NOT NULL");
+    }
+    if ($default) {
+      $ret[] = update_sql("ALTER TABLE {$table} ALTER $column SET $default");
+    }
+    if (drupal_pg_version() >= 703) {
+      $ret[] = update_sql("ALTER TABLE {$table} DROP {$column}_old");
+    }
+  }
+}
+
+/**
+ * Returns PostgreSQL database version. 
+ * Currently only main and sub version are considered, but this can be extended if needed.
+ * Returns 0 if it can not recognize the version.
+ */
+function drupal_pg_version() {
+  static $version = NULL;
+  
+  if ($version !== NULL) { return $version; }
+
+  $result = db_result(db_query('SELECT version()'));
+  if (! $result or ! preg_match("/(\d+)\.(\d+)/", $result, $matches) ) {
+    return $version = 0;
+  }
+  
+  return $version = sprintf('%d%02d', $matches[1], $matches[2]);
+}
+
 function update_sql($sql) {
   $edit = $_POST["edit"];
   $result = db_query($sql);
