You are here

protected function SchemaIntrospectionTestTrait::getIndexColumnNames in Drupal 9

Same name and namespace in other branches
  1. 8 core/tests/Drupal/Tests/Core/Database/SchemaIntrospectionTestTrait.php \Drupal\Tests\Core\Database\SchemaIntrospectionTestTrait::getIndexColumnNames()

Returns the column names used by the indexes of a table.

Parameters

string $table_name: A table name.

string $index_type: The type of the index. Can be one of 'index', 'unique' or 'primary'.

Return value

array A multi-dimensional array containing the column names for each index of the given type.

2 calls to SchemaIntrospectionTestTrait::getIndexColumnNames()
SchemaIntrospectionTestTrait::assertIndexOnColumns in core/tests/Drupal/Tests/Core/Database/SchemaIntrospectionTestTrait.php
Checks that an index covering exactly the given column names exists.
SchemaIntrospectionTestTrait::assertNoIndexOnColumns in core/tests/Drupal/Tests/Core/Database/SchemaIntrospectionTestTrait.php
Checks that an index covering exactly the given column names doesn't exist.

File

core/tests/Drupal/Tests/Core/Database/SchemaIntrospectionTestTrait.php, line 63

Class

SchemaIntrospectionTestTrait
Provides methods for testing database schema characteristics.

Namespace

Drupal\Tests\Core\Database

Code

protected function getIndexColumnNames($table_name, $index_type) {
  assert(in_array($index_type, [
    'index',
    'unique',
    'primary',
  ], TRUE));
  $schema = \Drupal::database()
    ->schema();
  $introspect_index_schema = new \ReflectionMethod(get_class($schema), 'introspectIndexSchema');
  $introspect_index_schema
    ->setAccessible(TRUE);
  $index_schema = $introspect_index_schema
    ->invoke($schema, $table_name);

  // Filter the indexes by type.
  if ($index_type === 'primary') {
    $indexes = [
      $index_schema['primary key'],
    ];
  }
  elseif ($index_type === 'unique') {
    $indexes = array_values($index_schema['unique keys']);
  }
  else {
    $indexes = array_values($index_schema['indexes']);
  }
  return $indexes;
}