You are here

protected function SchemaComparator::compareTable in Schema 8

Compares a table's declared and actual schema.

Parameters

$ref_name: The table name.

$ref: The table schema definition.

$inspect: The actual database schema.

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

File

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

Class

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

Namespace

Drupal\schema\Comparison

Code

protected function compareTable($ref_name, $ref, $inspect) {
  $result = $this->result
    ->getTableComparison($ref_name, $ref);
  $result
    ->setActualTableComment($inspect['description']);

  // DB layer trims table comments to a maximum length; in order to be able to
  // properly determine a difference between the declared and actual comment,
  // we need to do the same for the declared table comment.
  $declared = $result
    ->getDeclaredTableComment();
  $declared = $this->inspector
    ->prepareTableComment($declared, FALSE);
  $result
    ->setDeclaredTableComment($declared);

  // List of column keys to compare. The schema definition can contain
  // additional keys (e.g. serialize) which have not effect on the actual
  // database schema.
  $col_keys = array_flip(array(
    'description',
    'type',
    'size',
    'not null',
    'default',
    'length',
    'unsigned',
    'precision',
    'scale',
    'binary',
  ));
  foreach ($ref['fields'] as $colname => $col) {

    // Check if field exists in database.
    if (!isset($inspect['fields'][$colname])) {
      $result
        ->addMissingColumn($colname, $col);
      continue;
    }

    // Account for schemas that contain unnecessary 'default' => NULL
    if (isset($col['default']) && is_null($col['default']) && !isset($inspect['fields'][$colname]['default'])) {
      unset($col['default']);
    }

    // Limit the declared schema to the set of keys defined above.
    $col = array_intersect_key($col, $col_keys);

    // Compare column schema keys.
    $kdiffs = array();
    foreach ($col_keys as $key => $val) {
      if (!(isset($col[$key]) && !is_null($col[$key]) && $col[$key] !== FALSE && isset($inspect['fields'][$colname][$key]) && $inspect['fields'][$colname][$key] !== FALSE && $col[$key] == $inspect['fields'][$colname][$key] || (!isset($col[$key]) || is_null($col[$key]) || $col[$key] === FALSE) && (!isset($inspect['fields'][$colname][$key]) || $inspect['fields'][$colname][$key] === FALSE))) {

        // One way or another, difference between the two so note it to explicitly identify it later.
        $kdiffs[] = $key;
      }
    }
    if (count($kdiffs) != 0) {
      $result
        ->addColumnDifferences($colname, $kdiffs, $col, $inspect['fields'][$colname]);
    }
    unset($inspect['fields'][$colname]);
  }

  // Keep track of extra columns in database.
  foreach ($inspect['fields'] as $colname => $col) {
    $result
      ->addExtraColumn($colname, $col);
  }
  if (isset($ref['primary key'])) {
    if (!isset($inspect['primary key'])) {
      $result
        ->addMissingPrimaryKey($ref['primary key']);
    }
    elseif ($ref['primary key'] !== $inspect['primary key']) {
      $result
        ->addPrimaryKeyDifference($ref['primary key'], $inspect['primary key']);
    }
  }
  elseif (isset($inspect['primary key'])) {
    $result
      ->addExtraPrimaryKey($inspect['primary key']);
  }
  foreach (array(
    'unique keys',
    'indexes',
  ) as $type) {
    if (isset($ref[$type])) {
      foreach ($ref[$type] as $keyname => $key) {
        if (!isset($inspect[$type][$keyname])) {
          $result
            ->addMissingIndex($keyname, $type, $key);
          continue;
        }

        // $key is column list
        if ($key !== $inspect[$type][$keyname]) {
          $result
            ->addIndexDifferences($keyname, $type, $key, $inspect[$type][$keyname]);
        }
        unset($inspect[$type][$keyname]);
      }
    }
    if (isset($inspect[$type])) {
      foreach ($inspect[$type] as $keyname => $col) {
        $result
          ->addExtraIndex($keyname, $type, $col);
      }
    }
  }
}