You are here

protected function Schema::getNormalizedIndexes in Drupal 9

Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/Database/Driver/mysql/Schema.php \Drupal\Core\Database\Driver\mysql\Schema::getNormalizedIndexes()

Gets normalized indexes from a table specification.

Shortens indexes to 191 characters if they apply to utf8mb4-encoded fields, in order to comply with the InnoDB index limitation of 756 bytes.

Parameters

array $spec: The table specification.

Return value

array List of shortened indexes.

Throws

\Drupal\Core\Database\SchemaException Thrown if field specification is missing.

2 calls to Schema::getNormalizedIndexes()
Schema::addIndex in core/lib/Drupal/Core/Database/Driver/mysql/Schema.php
Add an index.
Schema::createKeysSql in core/lib/Drupal/Core/Database/Driver/mysql/Schema.php

File

core/lib/Drupal/Core/Database/Driver/mysql/Schema.php, line 315

Class

Schema
MySQL implementation of \Drupal\Core\Database\Schema.

Namespace

Drupal\Core\Database\Driver\mysql

Code

protected function getNormalizedIndexes(array $spec) {
  $indexes = isset($spec['indexes']) ? $spec['indexes'] : [];
  foreach ($indexes as $index_name => $index_fields) {
    foreach ($index_fields as $index_key => $index_field) {

      // Get the name of the field from the index specification.
      $field_name = is_array($index_field) ? $index_field[0] : $index_field;

      // Check whether the field is defined in the table specification.
      if (isset($spec['fields'][$field_name])) {

        // Get the MySQL type from the processed field.
        $mysql_field = $this
          ->processField($spec['fields'][$field_name]);
        if (in_array($mysql_field['mysql_type'], $this->mysqlStringTypes)) {

          // Check whether we need to shorten the index.
          if ((!isset($mysql_field['type']) || $mysql_field['type'] != 'varchar_ascii') && (!isset($mysql_field['length']) || $mysql_field['length'] > 191)) {

            // Limit the index length to 191 characters.
            $this
              ->shortenIndex($indexes[$index_name][$index_key]);
          }
        }
      }
      else {
        throw new SchemaException("MySQL needs the '{$field_name}' field specification in order to normalize the '{$index_name}' index");
      }
    }
  }
  return $indexes;
}