You are here

function content_db_change_column in Content Construction Kit (CCK) 5

Change a column definition.

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

$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)

2 calls to content_db_change_column()
content_alter_db_field in ./content_admin.inc
Perform adds, alters, and drops as needed to synchronize the database with new field definitions.
content_update_1006 in ./content.install
Set text db columns to accept NULL values for mysql (see http://drupal.org/node/108094)

File

./content_admin.inc, line 1444
Administrative interface for content type creation.

Code

function content_db_change_column($table, $column, $column_new, $type, $attributes = array()) {
  switch ($GLOBALS['db_type']) {
    case 'pgsql':
      $mappings = array(
        'int' => 'integer',
        'mediumint' => 'integer',
        'bigint' => 'integer',
        'tinyint' => 'smallint',
        'float' => 'float',
        'varchar' => 'varchar',
        'text' => 'text',
        'mediumtext' => 'text',
        'longtext' => 'text',
      );
      if (isset($mappings[$type])) {
        $type = $mappings[$type];
      }
      else {
        watchdog('database', t('No PostgreSQL mapping found for %type data type.', array(
          '%type' => $type,
        )), WATCHDOG_WARNING);
      }
      if ($type != 'varchar') {
        unset($attributes['length']);
      }
      break;
    case 'mysql':
    case 'mysqli':
      break;
  }
  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 (array_key_exists('length', $attributes)) {
    $type .= '(' . $attributes['length'] . ')';
  }
  if (array_key_exists('unsigned', $attributes) && $attributes['unsigned']) {
    switch ($GLOBALS['db_type']) {
      case 'pgsql':
        $type = str_replace('integer', 'int_unsigned', $type);
        break;
      default:
        $type .= ' unsigned';
        break;
    }
  }
  switch ($GLOBALS['db_type']) {
    case 'pgsql':
      db_query("ALTER TABLE {" . $table . "} RENAME {$column} TO " . $column . "_old");
      db_query("ALTER TABLE {" . $table . "} ADD {$column_new} {$type}");
      db_query("UPDATE {" . $table . "} SET {$column_new} = " . $column . "_old");
      if ($default) {
        db_query("ALTER TABLE {" . $table . "} ALTER {$column_new} SET {$default}");
      }
      if ($not_null) {
        db_query("ALTER TABLE {" . $table . "} ALTER {$column_new} SET NOT NULL");
      }
      db_query("ALTER TABLE {" . $table . "} DROP " . $column . "_old");
      break;
    case 'mysql':
    case 'mysqli':

      // MySQL allows no DEFAULT value for text (and blob) columns
      if (in_array($type, array(
        'text',
        'mediumtext',
        'longtext',
      ))) {
        $default = '';

        // We also allow NULL values to account for CCK's per field INSERTs
        $not_null = '';
      }
      db_query('ALTER TABLE {' . $table . '} CHANGE ' . $column . ' ' . $column_new . ' ' . $type . ' ' . $not_null . ' ' . $default);
      break;
  }
}