? cvs_get_vanilla.sh
? drupal-head-revisions_43+pgsql_2.diff
? database/updates.inc-1.114
? database/updates.inc-pgsql_fix4.diff
Index: database/updates.inc
===================================================================
RCS file: /cvs/drupal/drupal/database/updates.inc,v
retrieving revision 1.131
diff -u -p -r1.131 updates.inc
--- database/updates.inc	8 Sep 2005 19:17:34 -0000	1.131
+++ database/updates.inc	9 Sep 2005 13:44:04 -0000
@@ -790,6 +790,198 @@ function update_147() {
   return $ret;
 }
 
+
+/**
+ * According to http://dev.mysql.com/doc/mysql/en/create-table.htmlhttp://dev.mysql.com/doc/mysql/en/create-table.html
+ * KEY is a synonym to INDEX
+ * TODO: add multiple-column index?
+ */
+function db_add_key(&$ret, $table, $column) {
+  if ($GLOBALS['db_type'] == 'mysql') {
+    $ret[] = update_sql("ALTER TABLE {". $table ."} ADD KEY $column ($column)");
+  }
+  else {
+    $ret[] = update_sql("CREATE INDEX {". $table ."}_". $column ."_idx on {". $table ."}($column)");
+  }  
+}
+
+function db_drop_column(&$ret, $table, $column) {
+  if ($GLOBALS['db_type'] == 'mysql' or drupal_pg_version() >= 703) {
+    $ret[] = update_sql("ALTER TABLE {". $table ."} DROP $column");
+  }
+}
+
+function db_add_primary_key(&$ret, $table, $column) {
+  if ($GLOBALS['db_type'] == 'mysql') {
+    $ret[] = update_sql("ALTER TABLE {". $table ."} ADD PRIMARY KEY $column ($column)");
+  }
+  elseif ($GLOBALS['db_type'] == 'pgsql') {
+    $ret[] = update_sql("ALTER TABLE {". $table ."} ADD PRIMARY KEY ($column)");
+  }
+}
+
+function db_drop_primary_key(&$ret, $table) {
+  if ($GLOBALS['db_type'] == 'mysql') {
+    $ret[] = update_sql("ALTER TABLE {". $table ."} DROP PRIMARY KEY");
+  }
+  elseif ($GLOBALS['db_type'] == 'pgsql') {
+    $ret[] = update_sql("DROP INDEX {". $table ."}_pkey");
+    // $ret[] = update_sql("ALTER TABLE {". $table ."} DROP CONSTRAINT {". $table ."}_pkey"); // Not working in 7.2
+  }
+}
+
+/**
+ * Adds a column to a database. Uses syntax appropriate for used db (MySQL/PostgreSQL).
+ * Saves result of SQL commands in $ret array.
+ *
+ * Note: when you add a column with NOT NULL and you are not sure if there are rows in table already,
+ *  you MUST also add DEFAULT. Otherwise PostgreSQL won't work if the table is not empty. If NOT NULL and 
+ *  DEFAULT is set the Postgresql version will set values of the added column in old rows to the DEFAULT value.
+ *
+ * Differences between MySQL and PostgreSQL:
+ * - PostgreSQL do not support _unsigned_, it's ignored.
+ * - all _tinyint_, _smallint_ etc integer-types (any type with 'int' in the name) are converted
+ *    to INTEGER for PostgreSQL.
+ * - all *text* types (longtext, mediumtext etc) are converted to TEXT for PostgreSQL
+ *
+ * TODO: add more attributes? Like primary key, key (mysql: synonym for unique), unique, index, 
+ *       auto increment ? (mysql: must be indexed and must have a default)
+ *
+ * @param $ret 
+ *  Array to which results will be added.
+ * @param $table 
+ *  Name of the table, without {}
+ * @param $column 
+ *  Name of the column
+ * @param $type
+ *  Type of column
+ * @param $attributes
+ *  Additional optional attributes. Recognized atributes:
+ *    - unsigned    => TRUE/FALSE
+ *    - not null    => TRUE/FALSE
+ *    - default     => NULL/FALSE/value (without '')
+ * @return 
+ *  nothing, but modifies $ret parametr.
+ */
+function db_add_column(&$ret, $table, $column, $type, $attributes = array()) {
+  $pg_type = (preg_match('/int/i', $type)) ? 'int' : $type;
+  $pg_type = (preg_match('/text/i', $type)) ? 'text' : $type;
+  if (array_key_exists('unsigned', $attributes) and $attributes['unsigned']) {
+    $unsigned = 'unsigned';
+  }  
+  if (array_key_exists('not null', $attributes) and $attributes['not null']) {
+    $not_null = 'NOT NULL';
+  }
+  if (array_key_exists('default', $attributes)) {
+    if (is_null($attributes['default'])) {
+      $default_val = 'NULL'; 
+      $default = 'default NULL';
+    }
+    elseif ($attributes['defaul'] === FALSE) {
+      $default = '';
+    } 
+    else {
+      $default_val = "'$attributes[default]'"; 
+      $default = "default '$attributes[default]'";
+    }
+  }
+  
+  if ($GLOBALS['db_type'] == 'mysql') {
+    $ret[] = update_sql("ALTER TABLE {". $table ."} ADD $column $type $unsigned $not_null $default");
+  }
+  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) {
+      if ($default) {
+        $ret[] = update_sql("UPDATE {". $table ."} SET $column = $default_val");
+      }
+      $ret[] = update_sql("ALTER TABLE {". $table ."} ALTER $column SET NOT NULL");
+    }
+  }
+}
+
+/**
+ * Changes a column definition. Uses syntax appropriate for used db (MySQL/PostgreSQL).
+ * Saves result of SQL commands in $ret array.
+ *
+ * Differences between MySQL and PostgreSQL:
+ * - PostgreSQL do not support _unsigned_, it's ignored.
+ * - all _tinyint_, _smallint_ etc integer-types (any type with 'int' in the name) are converted
+ *    to INTEGER for PostgreSQL.
+ * - all *text* types (longtext, mediumtext etc) are converted to TEXT for PostgreSQL
+ * - FIXME: when changing a column in mysql the PRIMARY KEY attribute is retained. In Postgres, due to the 
+ *    nature of changing process (i.e. dropping a column) this attribute is dropped. How to workaround it?
+ *    Need to check other attributes too (e.g. NOT NULL, index etc).
+ *
+ * TODO: add more attributes? Like primary key, key (mysql: synonym for unique), unique, index, 
+ *       auto increment ? (mysql: must be indexed and must have a default)
+ *
+ * TODO: when setting NOT NOLL, and DEFAULT is set, update the table to default WHERE column IS NULL ?
+ *
+ * @param $ret 
+ *  Array to which results will be added.
+ * @param $table 
+ *  Name of the table, without {}
+ * @param $column 
+ *  Name of the column to change
+ * @param $column_new
+ *  New name for the column (set to the same as $column if you don't want to change the name)
+ * @param $type
+ *  Type of column
+ * @param $attributes
+ *  Additional optional attributes. Recognized atributes:
+ *    - unsigned    => TRUE/FALSE
+ *    - not null    => TRUE/FALSE
+ *    - default     => NULL/FALSE/value (without '')
+ * @return 
+ *  nothing, but modifies $ret parametr.
+ */
+function db_change_column(&$ret, $table, $column, $column_new, $type, $attributes = array()) {
+  $pg_type = (preg_match('/int/i', $type)) ? 'int' : $type;
+  $pg_type = (preg_match('/text/i', $type)) ? 'text' : $type;
+  if (array_key_exists('unsigned', $attributes) and $attributes['unsigned']) {
+    $unsigned = 'unsigned';
+  }  
+  if (array_key_exists('not null', $attributes) and $attributes['not null']) {
+    $not_null = 'NOT NULL';
+  }
+  if (array_key_exists('default', $attributes)) {
+    if (is_null($attributes['default'])) {
+      $default_val = 'NULL'; 
+      $default = 'default NULL';
+    }
+    elseif ($attributes['default'] === FALSE) {
+      $default = '';
+    } 
+    else {
+      $default_val = "'$attributes[default]'"; 
+      $default = "default '$attributes[default]'";
+    }
+  }
+  
+  if ($GLOBALS['db_type'] == 'mysql') {
+    $ret[] = update_sql("ALTER TABLE {". $table ."} CHANGE $column $column_new $type $unsigned $not_null $default");
+  }
+  elseif ($GLOBALS['db_type'] == 'pgsql') {
+    $ret[] = update_sql("ALTER TABLE {". $table ."} RENAME $column TO ". $column ."_old");
+    $ret[] = update_sql("ALTER TABLE {". $table ."} ADD $column_new $pg_type");
+    $ret[] = update_sql("UPDATE {". $table ."} SET $column_new = ". $column ."_old");
+    if ($default) {
+      $ret[] = update_sql("ALTER TABLE {". $table ."} ALTER $column_new SET $default");
+    }
+    if ($not_null) {
+      $ret[] = update_sql("ALTER TABLE {". $table ."} ALTER $column_new SET NOT NULL");
+    }
+    if (drupal_pg_version() >= 703) {
+      $ret[] = update_sql("ALTER TABLE {". $table ."} DROP ". $column ."_old");
+    }
+  }
+}
+
+
 function update_sql($sql) {
   $edit = $_POST["edit"];
   $result = db_query($sql);
Index: includes/database.pgsql.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/database.pgsql.inc,v
retrieving revision 1.14
diff -u -p -r1.14 database.pgsql.inc
--- includes/database.pgsql.inc	29 Aug 2005 19:32:55 -0000	1.14
+++ includes/database.pgsql.inc	9 Sep 2005 13:44:04 -0000
@@ -272,6 +272,28 @@ function db_unlock_tables() {
 }
 
 /**
+ * Returns PostgreSQL database version.
+ * Currently only main and sub version are considered, but this can be extended if needed.
+ * @return
+ *  Number in the form of xyy, where x is main version, yy is sub version. For example 7.4 is returned
+ *  as 704, 8.0.3 as 800. When 7.12 comes out it will be returned as 712.
+ * @return
+ *  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]);
+}
+
+/**
  * @} End of "ingroup database".
  */
 
