function Schema::getNormalizedIndexes

Same name and namespace in other branches
  1. 9 core/modules/mysql/src/Driver/Database/mysql/Schema.php \Drupal\mysql\Driver\Database\mysql\Schema::getNormalizedIndexes()
  2. 8.9.x core/lib/Drupal/Core/Database/Driver/mysql/Schema.php \Drupal\Core\Database\Driver\mysql\Schema::getNormalizedIndexes()
  3. 10 core/modules/mysql/src/Driver/Database/mysql/Schema.php \Drupal\mysql\Driver\Database\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/modules/mysql/src/Driver/Database/mysql/Schema.php
Add an index.
Schema::createKeysSql in core/modules/mysql/src/Driver/Database/mysql/Schema.php

File

core/modules/mysql/src/Driver/Database/mysql/Schema.php, line 310

Class

Schema
MySQL implementation of <a href="/api/drupal/core%21lib%21Drupal%21Core%21Database%21Schema.php/class/Schema/11.x" title="Provides a base implementation for Database Schema." class="local">\Drupal\Core\Database\Schema</a>.

Namespace

Drupal\mysql\Driver\Database\mysql

Code

protected function getNormalizedIndexes(array $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;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.