? cvs_get_vanilla.sh ? database/.updates.inc.swp 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 16:53:47 -0000 @@ -490,28 +490,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'", 'UNSIGNED'); 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; } +// FIXME: index should have different name and follow pgsql convention function update_131() { $ret = array(); @@ -527,6 +516,7 @@ function update_131() { return $ret; } +// FIXME: incompatibile table definition with mysql, need a patch for database.pgsql function update_132() { /** * PostgreSQL only update. @@ -537,7 +527,7 @@ function update_132() { $ret[] = update_sql('DROP TABLE {search_total}'); $ret[] = update_sql("CREATE TABLE {search_total} ( word varchar(50) NOT NULL default '', - count float default NULL)"); + count int default NULL)"); $ret[] = update_sql('CREATE INDEX {search_total}_word_idx ON {search_total}(word)'); /** @@ -550,22 +540,25 @@ function update_132() { return $ret; } +// FIXME: add table contact to database.pgsql 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; } @@ -573,9 +566,8 @@ function update_134() { 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 +581,15 @@ 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('UPDATE {users} SET access = login WHERE login > created'); $ret[] = update_sql('UPDATE {users} SET access = created WHERE access = 0'); return $ret; @@ -599,18 +597,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 +610,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'", "UNSIGNED"); return $ret; } @@ -649,10 +636,13 @@ 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; } +// FIXME: Postgresql does not drop primary key when changing column name. So we should not add new index for postgres. +// But the old index associated with old name will still have the old name even the change. +// Should be try to change the index name to match the new name? function update_143() { $ret = array(); @@ -669,25 +659,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"); - } + $ret = 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 +686,74 @@ function update_145() { return $ret; } + +/** + * Adds a column. + */ +// TODO: add type conversion +function db_add_column(&$ret, $table, $column, $type, $not_null = FALSE, $default = FALSE, $mysql_specific = '') { + $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 $mysql_specific"); + } + 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"); + } + } +} + +/** + * Changes $column definition. + */ +function db_change_column(&$ret, $table, $column, $type, $not_null = FALSE, $default = FALSE, $mysql_specific = '') { + 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 $mysql_specific"); + } + 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 {$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);