You are here

function data_alter_table in Data 6

Same name and namespace in other branches
  1. 8 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.

1 call to data_alter_table()
data_ui_adjust_form_submit_alter_table in data_ui/data_ui.admin.inc
Submit handler for data_ui_adjust_form().

File

./data.module, line 398
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;
  }
}