function DbDumpTest::getTableIndexes

Same name in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Command/DbDumpTest.php \Drupal\KernelTests\Core\Command\DbDumpTest::getTableIndexes()
  2. 8.9.x core/tests/Drupal/KernelTests/Core/Command/DbDumpTest.php \Drupal\KernelTests\Core\Command\DbDumpTest::getTableIndexes()
  3. 10 core/modules/mysql/tests/src/Kernel/mysql/DbDumpTest.php \Drupal\Tests\mysql\Kernel\mysql\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/mysql/tests/src/Kernel/mysql/DbDumpTest.php
Tests loading the script back into the database.

File

core/modules/mysql/tests/src/Kernel/mysql/DbDumpTest.php, line 247

Class

DbDumpTest
Tests for the database dump commands.

Namespace

Drupal\Tests\mysql\Kernel\mysql

Code

protected function getTableIndexes($table) : array {
    $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;
}

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