You are here

function data_alter_table in Data 8

Same name and namespace in other branches
  1. 6 data.module \data_alter_table()
  2. 7 data.module \data_alter_table()

Helper function for adjusting a table's real schema. @todo: this should live in schema module and should use better defined $reason keys.

Throws

DataException on error.

File

./data.module, line 348
Hooks and API functions for data module.

Code

function data_alter_table($table, $field_reason) {
  list($field, $reason) = explode(': ', $field_reason);
  $schema = $table
    ->get('table_schema');
  switch ($reason) {
    case 'not in database':
      if (isset($schema['fields'][$field])) {
        $table
          ->addField($field, $schema['fields'][$field]);
      }
      break;
    case 'missing in database':
      list($type, $field) = explode(' ', $field);

      // @todo: support multiple keys.
      if ($type == 'indexes') {
        $table
          ->addIndex($field);
      }
      elseif ($type == 'unique keys') {
        $table
          ->addUniqueKey($field);
      }
      elseif ($type == 'primary key') {
        $table
          ->addPrimaryKey($schema['primary keys']);
      }
      break;
    case 'primary key:<br />declared':

      // @todo: yikes!
      $table
        ->dropPrimaryKey();
      $table
        ->changePrimaryKey($schema['primary keys']);
    case 'missing in schema':
      if ($field == 'primary key') {
        $table
          ->dropPrimaryKey();
      }
      break;
    case 'unexpected column in database':
      $table
        ->dropField($field);
      break;
  }
}