? 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 15:35:37 -0000 @@ -790,6 +790,189 @@ 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_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("ALTER TABLE {". $table ."} DROP CONSTRAINT {". $table ."}_pkey"); + } +} + +/** + * 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"); + } + $ret[] = update_sql("ALTER TABLE {". $table ."} DROP ". $column ."_old"); + } +} + + function update_sql($sql) { $edit = $_POST["edit"]; $result = db_query($sql);