You are here

protected function DbDumpTest::getTableIndexes in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/system/src/Tests/Update/DbDumpTest.php \Drupal\system\Tests\Update\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/modules/system/src/Tests/Update/DbDumpTest.php
Test loading the script back into the database.

File

core/modules/system/src/Tests/Update/DbDumpTest.php, line 253
Contains \Drupal\system\Tests\Update\DbDumpTest.

Class

DbDumpTest
Tests for the database dump commands.

Namespace

Drupal\system\Tests\Update

Code

protected function getTableIndexes($table) {
  $query = db_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;
}