You are here

protected function DbDumpTest::getTableIndexes in Drupal 8

Same name and namespace in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Command/DbDumpTest.php \Drupal\KernelTests\Core\Command\DbDumpTest::getTableIndexes()
  2. 10 core/tests/Drupal/KernelTests/Core/Command/DbDumpTest.php \Drupal\KernelTests\Core\Command\DbDumpTest::getTableIndexes()

Returns indexes for a given table.

Parameters

string $table: The table to find indexes for.

Return value

array The 'primary key', 'unique keys', and 'indexes' portion of the Drupal table schema.

1 call to DbDumpTest::getTableIndexes()
DbDumpTest::testScriptLoad in core/tests/Drupal/KernelTests/Core/Command/DbDumpTest.php
Test loading the script back into the database.

File

core/tests/Drupal/KernelTests/Core/Command/DbDumpTest.php, line 253

Class

DbDumpTest
Tests for the database dump commands.

Namespace

Drupal\KernelTests\Core\Command

Code

protected function getTableIndexes($table) {
  $query = Database::getConnection()
    ->query("SHOW INDEX FROM {" . $table . "}");
  $definition = [];
  while ($row = $query
    ->fetchAssoc()) {
    $index_name = $row['Key_name'];
    $column = $row['Column_name'];

    // Key the arrays by the index sequence for proper ordering (start at 0).
    $order = $row['Seq_in_index'] - 1;

    // If specified, add length to the index.
    if ($row['Sub_part']) {
      $column = [
        $column,
        $row['Sub_part'],
      ];
    }
    if ($index_name === 'PRIMARY') {
      $definition['primary key'][$order] = $column;
    }
    elseif ($row['Non_unique'] == 0) {
      $definition['unique keys'][$index_name][$order] = $column;
    }
    else {
      $definition['indexes'][$index_name][$order] = $column;
    }
  }
  return $definition;
}