You are here

protected function SchemaComparator::checkTable in Schema 8

Checks a given table schema definition for inconsistencies, and adds warnings to the result field.

Currently implemented error checks:

  • fields need to be defined on all tables.
  • column type and default type must match.
  • 'text' and 'blob' columns cannot have a default value.
  • primary keys must be 'not null'

@todo 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.

Parameters

$t_name: The table name.

$table: The table schema definition.

1 call to SchemaComparator::checkTable()
SchemaComparator::executeCompare in src/Comparison/SchemaComparator.php
Generates comparison information and stores it in the $result field.

File

src/Comparison/SchemaComparator.php, line 121
Contains Drupal\schema\Comparison\SchemaComparator.

Class

SchemaComparator
Compares a declared schema array with the complete database schema.

Namespace

Drupal\schema\Comparison

Code

protected function checkTable($t_name, $table) {

  // Error check: fields need to be defined on all tables.
  if (!isset($table['fields']) || !is_array($table['fields'])) {
    $this->result
      ->addWarning(t('Table %table: Missing or invalid \'fields\' array.', array(
      '%table' => $t_name,
    )));
  }
  else {
    foreach ($table['fields'] as $c_name => $col) {

      // Error check: column type and default type must match
      switch ($col['type']) {
        case 'int':
        case 'float':
        case 'numeric':
          if (isset($col['default']) && (!is_numeric($col['default']) || is_string($col['default']))) {
            $this->result
              ->addWarning(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'])) {
            $this->result
              ->addWarning(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
      switch ($col['type']) {
        case 'text':
        case 'blob':
          if (isset($col['default'])) {
            $this->result
              ->addWarning(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'
  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) {
        $this->result
          ->addWarning(t('%table.%column is part of the primary key but is not specified to be \'not null\'.', array(
          '%table' => $t_name,
          '%column' => $key,
        )));
      }
    }
  }
}