You are here

public function Schema::addIndex in Zircon Profile 8.0

Same name in this branch
  1. 8.0 core/lib/Drupal/Core/Database/Schema.php \Drupal\Core\Database\Schema::addIndex()
  2. 8.0 core/lib/Drupal/Core/Database/Driver/sqlite/Schema.php \Drupal\Core\Database\Driver\sqlite\Schema::addIndex()
  3. 8.0 core/lib/Drupal/Core/Database/Driver/pgsql/Schema.php \Drupal\Core\Database\Driver\pgsql\Schema::addIndex()
  4. 8.0 core/lib/Drupal/Core/Database/Driver/mysql/Schema.php \Drupal\Core\Database\Driver\mysql\Schema::addIndex()
Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/Database/Driver/pgsql/Schema.php \Drupal\Core\Database\Driver\pgsql\Schema::addIndex()

Add an index.

@todo remove the $spec argument whenever schema introspection is added.

Parameters

$table: The table to be altered.

$name: The name of the index.

$fields: An array of field names or field information; if field information is passed, it's an array whose first element is the field name and whose second is the maximum length in the index. For example, the following will use the full length of the `foo` field, but limit the `bar` field to 4 characters:

$fields = [
  'foo',
  [
    'bar',
    4,
  ],
];

array $spec: The table specification for the table to be altered. This is used in order to be able to ensure that the index length is not too long. This schema definition can usually be obtained through hook_schema(), or in case the table was created by the Entity API, through the schema handler listed in the entity class definition. For reference, see SqlContentEntityStorageSchema::getDedicatedTableSchema() and SqlContentEntityStorageSchema::getSharedTableFieldSchema().

In order to prevent human error, it is recommended to pass in the complete table specification. However, in the edge case of the complete table specification not being available, we can pass in a partial table definition containing only the fields that apply to the index:

$spec = [
  // Example partial specification for a table:
  'fields' => [
    'example_field' => [
      'description' => 'An example field',
      'type' => 'varchar',
      'length' => 32,
      'not null' => TRUE,
      'default' => '',
    ],
  ],
  'indexes' => [
    'table_example_field' => [
      'example_field',
    ],
  ],
];

Note that the above is a partial table definition and that we would usually pass a complete table definition as obtained through hook_schema() instead.

Throws

\Drupal\Core\Database\SchemaObjectDoesNotExistException If the specified table doesn't exist.

\Drupal\Core\Database\SchemaObjectExistsException If the specified table already has an index by that name.

Overrides Schema::addIndex

See also

Schema API

hook_schema()

1 call to Schema::addIndex()
Schema::_createKeys in core/lib/Drupal/Core/Database/Driver/pgsql/Schema.php

File

core/lib/Drupal/Core/Database/Driver/pgsql/Schema.php, line 667
Contains \Drupal\Core\Database\Driver\pgsql\Schema.

Class

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

Namespace

Drupal\Core\Database\Driver\pgsql

Code

public function addIndex($table, $name, $fields, array $spec) {
  if (!$this
    ->tableExists($table)) {
    throw new SchemaObjectDoesNotExistException(t("Cannot add index @name to table @table: table doesn't exist.", array(
      '@table' => $table,
      '@name' => $name,
    )));
  }
  if ($this
    ->indexExists($table, $name)) {
    throw new SchemaObjectExistsException(t("Cannot add index @name to table @table: index already exists.", array(
      '@table' => $table,
      '@name' => $name,
    )));
  }
  $this->connection
    ->query($this
    ->_createIndexSql($table, $name, $fields));
  $this
    ->resetTableInformation($table);
}