? cvs_get_vanilla.sh 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 19 Aug 2005 14:31:29 -0000 @@ -705,6 +705,75 @@ function update_145() { return $ret; } + +/** + * Adds a column. + */ +function db_add_column($table, $column, $type, $default = FALSE, $not_null = FALSE) { + $default = ($default === FALSE ? '' : "DEFAULT $default"); + $not_null = ($not_null === FALSE ? '' : "NOT NULL"); + + if ($GLOBALS['db_type'] == 'mysql') { + $ret[] = update_sql("ALTER TABLE {$table} ADD $column $type $not_null $default"); + } + elseif ($GLOBALS['db_type'] == 'pgsql') { + $ret[] = update_sql("ALTER TABLE {$table} ADD $column $type"); + if ($not_null) { + $ret[] = update_sql("ALTER TABLE {$table} ALTER $column SET NOT NULL"); + } + if ($default !== FALSE) { + $ret[] = update_sql("ALTER TABLE {$table} ALTER $column SET DEFAULT $default"); + } + } + return $ret; +} + +/** + * Changes $column definition. + */ +function db_change_column($table, $column, $type, $default = FALSE, $not_null = FALSE) { + if ($GLOBALS['db_type'] == 'mysql') { + $default = ($default === FALSE ? '' : "DEFAULT $default"); + $not_null = ($not_null === FALSE ? '' : "NOT NULL"); + $ret[] = update_sql("ALTER TABLE {$table} CHANGE $column $column $type $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 $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 !== FALSE) { + $ret[] = update_sql("ALTER TABLE {$table} ALTER $column SET DEFAULT $default"); + } + if (drupal_pg_version() >= 703) { + $ret[] = update_sql("ALTER TABLE {$table} DROP {$table}_old"); + } + } + return $ret; +} + +/** + * 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);