function db_change_column

8.0.x update_6.inc db_change_column(&$ret, $table, $column, $column_new, $type, $attributes = array())
6.x update_6.inc db_change_column(&$ret, $table, $column, $column_new, $type, $attributes = array())
7.x update_6.inc db_change_column(&$ret, $table, $column, $column_new, $type, $attributes = array())
3.x update_6.inc db_change_column(&$ret, $table, $column, $column_new, $type, $attributes = array())
4.x update_6.inc db_change_column(&$ret, $table, $column, $column_new, $type, $attributes = array())
5.x update_6.inc db_change_column(&$ret, $table, $column, $column_new, $type, $attributes = array())

Change a column definition using syntax appropriate for PostgreSQL. Save result of SQL commands in $ret array.

Remember that changing a column definition involves adding a new column and dropping an old one. This means that any indices, primary keys and sequences from serial-type columns are dropped and might need to be recreated.

Parameters

$ret: Array to which results will be added.

$table: Name of the table, without {}

$column: Name of the column to change

$column_new: New name for the column (set to the same as $column if you don't want to change the name)

$type: Type of column

$attributes: Additional optional attributes. Recognized attributes: not null => TRUE|FALSE default => NULL|FALSE|value (with or without '', it won't be added)

Return value

nothing, but modifies $ret parameter.

File

commands/core/drupal/update_6.inc, line 94
Update.php for provisioned sites. This file is a derivative of the standard drupal update.php, which has been modified to allow being run from the command line.

Code

function db_change_column(&$ret, $table, $column, $column_new, $type, $attributes = array()) {
  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]";
    }
  }

  $ret[] = update_sql("ALTER TABLE {" . $table . "} RENAME $column TO " . $column . "_old");
  $ret[] = update_sql("ALTER TABLE {" . $table . "} ADD $column_new $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");
}