Skip to content

fix!: SchemaBuilder::getIndexes was returning incorrect values #274

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions src/Query/Processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,21 @@ public function processColumns($results)

/**
* {@inheritDoc}
* @param array{ index_name: string }&array<string, mixed> $results
* @return array<array-key, string>
* @param list<array<string, mixed>> $results
* @return list<array{name: string, columns: list<string>, type: string, unique: bool, primary: bool}>
*/
public function processIndexes($results)
{
return array_map(function ($result) {
return ((object) $result)->index_name;
$result = (object) $result;

return [
'name' => $name = $result->name,
'columns' => $result->columns ? explode(',', $result->columns) : [],
'type' => strtolower($result->type),
'unique' => (bool) $result->unique,
'primary' => $name === 'PRIMARY_KEY',
];
Comment on lines +114 to +120
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}, $results);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Schema/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public function dropAllTables()
$queries = [];
foreach ($sortedTables as $tableData) {
$tableName = $tableData['name'];
$indexes = $this->getIndexes($tableName);
$indexes = $this->getIndexListing($tableName);
$blueprint = $this->createBlueprint($tableName);
foreach ($indexes as $index) {
if ($index === 'PRIMARY_KEY') {
Expand Down
18 changes: 14 additions & 4 deletions src/Schema/Grammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,20 @@ public function compileTables()
*/
public function compileIndexes($table)
{
return sprintf(
'select index_name as `index_name` from information_schema.indexes where table_schema = \'\' and table_name = %s',
$this->quoteString($table),
);
return implode(' ', [
'select',
implode(', ', [
'i.index_name as `name`',
'string_agg(c.column_name, \',\') as `columns`',
'i.index_type as `type`',
'i.is_unique as `unique`',
]),
'from information_schema.indexes as i',
'join information_schema.index_columns as c on i.table_schema = c.table_schema and i.table_name = c.table_name and i.index_name = c.index_name',
'where i.table_schema = ' . $this->quoteString(''),
'and i.table_name = ' . $this->quoteString($table),
'group by i.index_name, i.index_type, i.is_unique',
]);
}

/**
Expand Down
55 changes: 47 additions & 8 deletions tests/Schema/BuilderTestLast.php
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ public function test_getColumns(): void
], Arr::first($sb->getColumns($table)));
}

public function test_getAllTables(): void
public function test_getTableListing(): void
{
$conn = $this->getDefaultConnection();
$sb = $conn->getSchemaBuilder();
Expand All @@ -256,14 +256,53 @@ public function test_getAllTables(): void
$table->primary('id');
});

/** @var array{ name: string, type: string } $row */
$row = Arr::first(
$sb->getTables(),
static fn(array $row): bool => $row['name'] === $table,
);
$this->assertContains($table, $sb->getTableListing());
}

$this->assertSame($table, $row['name']);
$this->assertSame('BASE TABLE', $row['type']);
public function test_getIndexes(): void
{
$conn = $this->getDefaultConnection();
$sb = $conn->getSchemaBuilder();
$table = $this->generateTableName(class_basename(__CLASS__));
$sb->create($table, function (Blueprint $table) {
$table->uuid('id')->primary();
$table->uuid('something');
$table->index('something');
});

$this->assertSame([
[
'name' => strtolower($table) . '_something_index',
'columns' => ['something'],
'type' => 'index',
'unique' => false,
'primary' => false,
],
[
'name' => 'PRIMARY_KEY',
'columns' => ['id'],
'type' => 'primary_key',
'unique' => true,
'primary' => true,
],
], $sb->getIndexes($table));
}

public function test_getIndexListing(): void
{
$conn = $this->getDefaultConnection();
$sb = $conn->getSchemaBuilder();
$table = $this->generateTableName(class_basename(__CLASS__));
$sb->create($table, function (Blueprint $table) {
$table->uuid('id')->primary();
$table->uuid('something');
$table->index('something');
});

$this->assertSame([
strtolower($table) . '_something_index',
'PRIMARY_KEY',
], $sb->getIndexListing($table));
}

public function test_dropAllTables(): void
Expand Down