You are here

function schema_compare_schemas in Schema 5

Same name and namespace in other branches
  1. 8 schema.module \schema_compare_schemas()
  2. 6 schema.module \schema_compare_schemas()
  3. 7 schema.module \schema_compare_schemas()
2 calls to schema_compare_schemas()
schema_report in ./schema.module
schema_requirements in ./schema.install

File

./schema.module, line 187

Code

function schema_compare_schemas($ref, $inspect = NULL) {
  if (!isset($inspect)) {
    $inspect = schema_invoke('inspect');
  }
  $info = array();

  // Error checks to consider adding:
  // All type serial columns must be in an index or key.
  // All columns in a primary or unique key must be NOT NULL.
  // Error check: column type and default type must match
  foreach ($ref as $t_name => $table) {
    foreach ($table['fields'] as $c_name => $col) {
      switch ($col['type']) {
        case 'int':
        case 'float':
        case 'numeric':
          if (isset($col['default']) && (!is_numeric($col['default']) || is_string($col['default']))) {
            $info['warn'][] = t('%table.%column is type %type but its default %default is PHP type %phptype', array(
              '%table' => $t_name,
              '%column' => $c_name,
              '%type' => $col['type'],
              '%default' => $col['default'],
              '%phptype' => gettype($col['default']),
            ));
          }
          break;
        default:
          if (isset($col['default']) && !is_string($col['default'])) {
            $info['warn'][] = t('%table.%column is type %type but its default %default is PHP type %phptype', array(
              '%table' => $t_name,
              '%column' => $c_name,
              '%type' => $col['type'],
              '%default' => $col['default'],
              '%phptype' => gettype($col['default']),
            ));
          }
          break;
      }
    }
  }

  // Error check: 'text' and 'blob' columns cannot have a default
  // value.  Do not warn about core tables because in this D5
  // version those are core tables and do not follow all rules.
  foreach ($ref as $t_name => $table) {
    foreach ($table['fields'] as $c_name => $col) {
      switch ($col['type']) {
        case 'text':
        case 'blob':
          if ($table['module'] != 'core' && isset($col['default'])) {
            $info['warn'][] = t('%table.%column is type %type and may not have a default value', array(
              '%table' => $t_name,
              '%column' => $c_name,
              '%type' => $col['type'],
            ));
          }
          break;
      }
    }
  }

  // Error check: primary keys must be 'not null'
  foreach ($ref as $t_name => $table) {
    if (isset($table['primary key'])) {
      $keys = db_field_names($table['primary key']);
      foreach ($keys as $key) {
        if (!isset($table['fields'][$key]['not null']) || $table['fields'][$key]['not null'] != TRUE) {
          $info['warn'][] = t('%table.%column is part of the primary key but is not specified to be \'not null\'.', array(
            '%table' => $t_name,
            '%column' => $key,
          ));
        }
      }
    }
  }
  foreach ($ref as $name => $table) {
    $module = $table['module'];
    if (!isset($inspect[$name])) {
      $info['missing'][$module][$name] = array(
        'status' => 'missing',
      );
    }
    else {
      $status = schema_compare_table($table, $inspect[$name]);
      $info[$status['status']][$module][$name] = $status;
      unset($inspect[$name]);
    }
  }
  foreach ($inspect as $name => $table) {
    $info['extra'][] = $name;
  }
  return $info;
}