You are here

protected function SchemaComparator::preprocessTableSchema in Schema 8

Make sure the given schema is consistent.

Parameters

$t_name:

$table:

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

File

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

Class

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

Namespace

Drupal\schema\Comparison

Code

protected function preprocessTableSchema($t_name, &$table) {
  $_db_type = db_driver();
  $primary_key = empty($table['primary key']) ? array() : $table['primary key'];
  foreach ($table['fields'] as $f_name => &$field) {

    // MySQL Specification: If the column is defined as part of a PRIMARY
    // KEY but not explicitly as NOT NULL, MySQL creates it as a NOT NULL
    // column (because PRIMARY KEY columns must be NOT NULL), but also
    // assigns it a DEFAULT clause using the implicit default value.
    // @see http://dev.mysql.com/doc/refman/5.5/en/data-type-defaults.html
    // @todo Remove once this is fixed in core.
    // @see https://www.drupal.org/node/2394069
    if (in_array($f_name, $primary_key)) {
      $field['not null'] = TRUE;
    }

    // Many Schema types can map to the same engine type (e.g. in
    // PostgresSQL, text:{small,medium,big} are all just text).  When
    // we inspect the database, we see the common type, but the
    // reference we are comparing against can have a specific type.
    // We therefore run the reference's specific type through the
    // type conversion cycle to get its common type for comparison.
    //
    // Sadly, we need a special-case hack for 'serial'.
    $serial = $field['type'] == 'serial' ? TRUE : FALSE;
    $name = isset($table['name']) ? $table['name'] : $t_name;
    $dbtype = schema_engine_type($field, $name, $f_name);
    list($field['type'], $field['size']) = schema_schema_type($dbtype, $name, $f_name);
    if ($serial) {
      $field['type'] = 'serial';
    }

    // If an engine-specific type is specified, use it.  XXX $inspect
    // will contain the schema type for the engine type, if one
    // exists, whereas dbtype_type contains the engine type.
    if (isset($field[$_db_type . '_type'])) {
      $field['type'] = $field[$_db_type . '_type'];
    }

    // Column comments are trimmed to a specific length by the database schema
    // layer. Make sure we match the trimmed value, so we can properly compare
    // actual and declared value.
    if (!empty($field['description'])) {
      $field['description'] = $this->inspector
        ->prepareColumnComment($field['description'], FALSE);
    }
  }
}