You are here

function db_change_column in Drupal 6

Same name and namespace in other branches
  1. 4 update.php \db_change_column()
  2. 5 update.php \db_change_column()

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.

1 call to db_change_column()
statistics_update_1000 in modules/statistics/statistics.install
Changes session ID field to VARCHAR(64) to add support for SHA-1 hashes.

File

./update.php, line 106
Administrative page for handling updates from one Drupal version to another.

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");
}