Skip to content

Commit ffaa60b

Browse files
authored
fix!: SchemaBuilder::getForeignKeys was returning incorrect values (#275)
1 parent e17aa0a commit ffaa60b

File tree

4 files changed

+62
-8
lines changed

4 files changed

+62
-8
lines changed

src/Query/Processor.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,13 +123,23 @@ public function processIndexes($results)
123123

124124
/**
125125
* {@inheritDoc}
126-
* @param array{key_name: string}&array<string, mixed> $results
127-
* @return array<array-key, string>
126+
* @param list<array<string, mixed>> $results
127+
* @return list<array{name: string, columns: list<string>, foreign_schema: string, foreign_table: string, foreign_columns: list<string>, on_update: string, on_delete: string}>
128128
*/
129129
public function processForeignKeys($results)
130130
{
131131
return array_map(function ($result) {
132-
return ((object) $result)->key_name;
132+
$result = (object) $result;
133+
134+
return [
135+
'name' => $result->name,
136+
'columns' => explode(',', $result->columns),
137+
'foreign_schema' => $result->foreign_schema,
138+
'foreign_table' => $result->foreign_table,
139+
'foreign_columns' => explode(',', $result->foreign_columns),
140+
'on_update' => strtolower($result->on_update),
141+
'on_delete' => strtolower($result->on_delete),
142+
];
133143
}, $results);
134144
}
135145
}

src/Schema/Builder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ public function dropAllTables()
133133
$foreigns = $this->getForeignKeys($tableName);
134134
$blueprint = $this->createBlueprint($tableName);
135135
foreach ($foreigns as $foreign) {
136-
$blueprint->dropForeign($foreign);
136+
$blueprint->dropForeign($foreign['name']);
137137
}
138138
array_push($queries, ...$blueprint->toSql($connection, $this->grammar));
139139
}

src/Schema/Grammar.php

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,24 @@ public function compileIndexes($table)
8181
*/
8282
public function compileForeignKeys($table)
8383
{
84-
return sprintf(
85-
'select constraint_name as `key_name` from information_schema.table_constraints where constraint_type = "FOREIGN KEY" and table_schema = \'\' and table_name = %s',
86-
$this->quoteString($table),
87-
);
84+
return implode(' ', [
85+
'select',
86+
implode(', ', [
87+
'kc.constraint_name as `name`',
88+
'string_agg(kc.column_name) as `columns`',
89+
'cc.table_schema as `foreign_schema`',
90+
'cc.table_name as `foreign_table`',
91+
'string_agg(cc.column_name) as `foreign_columns`',
92+
'rc.update_rule as `on_update`',
93+
'rc.delete_rule as `on_delete`',
94+
]),
95+
'from information_schema.key_column_usage kc',
96+
'join information_schema.referential_constraints rc on kc.constraint_name = rc.constraint_name',
97+
'join information_schema.constraint_column_usage cc on kc.constraint_name = cc.constraint_name',
98+
'where kc.table_schema = ""',
99+
'and kc.table_name = ' . $this->quoteString($table),
100+
'group by kc.constraint_name, cc.table_schema, cc.table_name, rc.update_rule, rc.delete_rule'
101+
]);
88102
}
89103

90104
/**

tests/Schema/BuilderTestLast.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,36 @@ public function test_getIndexListing(): void
305305
], $sb->getIndexListing($table));
306306
}
307307

308+
public function test_getForeignKeys(): void
309+
{
310+
$conn = $this->getDefaultConnection();
311+
$sb = $conn->getSchemaBuilder();
312+
$table1 = $this->generateTableName(class_basename(__CLASS__). '_1');
313+
$sb->create($table1, function (Blueprint $table) {
314+
$table->uuid('id')->primary();
315+
$table->uuid('something');
316+
$table->index('something');
317+
});
318+
319+
$table2 = $this->generateTableName(class_basename(__CLASS__). '_2');
320+
$sb->create($table2, function (Blueprint $table) use ($table1) {
321+
$table->uuid('table2_id')->primary();
322+
$table->uuid('other_id');
323+
$table->index('other_id');
324+
$table->foreign('other_id')->references('id')->on($table1);
325+
});
326+
327+
$this->assertSame([[
328+
'name' => strtolower($table2) . '_other_id_foreign',
329+
'columns' => ['other_id'],
330+
'foreign_schema' => '',
331+
'foreign_table' => $table1,
332+
'foreign_columns' => ['id'],
333+
'on_update' => "no action",
334+
'on_delete' => "no action",
335+
]], $sb->getForeignKeys($table2));
336+
}
337+
308338
public function test_dropAllTables(): void
309339
{
310340
$conn = $this->getDefaultConnection();

0 commit comments

Comments
 (0)