Skip to content

Commit e17aa0a

Browse files
authored
fix!: SchemaBuilder::getIndexes was returning incorrect values (#274)
1 parent faedc4a commit e17aa0a

File tree

4 files changed

+73
-16
lines changed

4 files changed

+73
-16
lines changed

src/Query/Processor.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,13 +103,21 @@ public function processColumns($results)
103103

104104
/**
105105
* {@inheritDoc}
106-
* @param array{ index_name: string }&array<string, mixed> $results
107-
* @return array<array-key, string>
106+
* @param list<array<string, mixed>> $results
107+
* @return list<array{name: string, columns: list<string>, type: string, unique: bool, primary: bool}>
108108
*/
109109
public function processIndexes($results)
110110
{
111111
return array_map(function ($result) {
112-
return ((object) $result)->index_name;
112+
$result = (object) $result;
113+
114+
return [
115+
'name' => $name = $result->name,
116+
'columns' => $result->columns ? explode(',', $result->columns) : [],
117+
'type' => strtolower($result->type),
118+
'unique' => (bool) $result->unique,
119+
'primary' => $name === 'PRIMARY_KEY',
120+
];
113121
}, $results);
114122
}
115123

src/Schema/Builder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ public function dropAllTables()
144144
$queries = [];
145145
foreach ($sortedTables as $tableData) {
146146
$tableName = $tableData['name'];
147-
$indexes = $this->getIndexes($tableName);
147+
$indexes = $this->getIndexListing($tableName);
148148
$blueprint = $this->createBlueprint($tableName);
149149
foreach ($indexes as $index) {
150150
if ($index === 'PRIMARY_KEY') {

src/Schema/Grammar.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,20 @@ public function compileTables()
5757
*/
5858
public function compileIndexes($table)
5959
{
60-
return sprintf(
61-
'select index_name as `index_name` from information_schema.indexes where table_schema = \'\' and table_name = %s',
62-
$this->quoteString($table),
63-
);
60+
return implode(' ', [
61+
'select',
62+
implode(', ', [
63+
'i.index_name as `name`',
64+
'string_agg(c.column_name, \',\') as `columns`',
65+
'i.index_type as `type`',
66+
'i.is_unique as `unique`',
67+
]),
68+
'from information_schema.indexes as i',
69+
'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',
70+
'where i.table_schema = ' . $this->quoteString(''),
71+
'and i.table_name = ' . $this->quoteString($table),
72+
'group by i.index_name, i.index_type, i.is_unique',
73+
]);
6474
}
6575

6676
/**

tests/Schema/BuilderTestLast.php

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ public function test_getColumns(): void
245245
], Arr::first($sb->getColumns($table)));
246246
}
247247

248-
public function test_getAllTables(): void
248+
public function test_getTableListing(): void
249249
{
250250
$conn = $this->getDefaultConnection();
251251
$sb = $conn->getSchemaBuilder();
@@ -256,14 +256,53 @@ public function test_getAllTables(): void
256256
$table->primary('id');
257257
});
258258

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

265-
$this->assertSame($table, $row['name']);
266-
$this->assertSame('BASE TABLE', $row['type']);
262+
public function test_getIndexes(): void
263+
{
264+
$conn = $this->getDefaultConnection();
265+
$sb = $conn->getSchemaBuilder();
266+
$table = $this->generateTableName(class_basename(__CLASS__));
267+
$sb->create($table, function (Blueprint $table) {
268+
$table->uuid('id')->primary();
269+
$table->uuid('something');
270+
$table->index('something');
271+
});
272+
273+
$this->assertSame([
274+
[
275+
'name' => strtolower($table) . '_something_index',
276+
'columns' => ['something'],
277+
'type' => 'index',
278+
'unique' => false,
279+
'primary' => false,
280+
],
281+
[
282+
'name' => 'PRIMARY_KEY',
283+
'columns' => ['id'],
284+
'type' => 'primary_key',
285+
'unique' => true,
286+
'primary' => true,
287+
],
288+
], $sb->getIndexes($table));
289+
}
290+
291+
public function test_getIndexListing(): void
292+
{
293+
$conn = $this->getDefaultConnection();
294+
$sb = $conn->getSchemaBuilder();
295+
$table = $this->generateTableName(class_basename(__CLASS__));
296+
$sb->create($table, function (Blueprint $table) {
297+
$table->uuid('id')->primary();
298+
$table->uuid('something');
299+
$table->index('something');
300+
});
301+
302+
$this->assertSame([
303+
strtolower($table) . '_something_index',
304+
'PRIMARY_KEY',
305+
], $sb->getIndexListing($table));
267306
}
268307

269308
public function test_dropAllTables(): void

0 commit comments

Comments
 (0)