Skip to content

Commit 6898979

Browse files
committed
drivers: getForeignKeys() works with multi-column foreign keys
1 parent bfd5100 commit 6898979

File tree

11 files changed

+39
-33
lines changed

11 files changed

+39
-33
lines changed

src/Database/Drivers/Engine.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ function getIndexes(string $table): array;
7373

7474
/**
7575
* Returns metadata for all foreign keys in a table.
76-
* @return list<array{name: string, local: string, table: string, foreign: string}>
76+
* @return list<array{name: string, local: list<string>, table: string, foreign: list<string>}>
7777
*/
7878
function getForeignKeys(string $table): array;
7979
}

src/Database/Drivers/Engines/MSSQLEngine.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -222,12 +222,12 @@ public function getForeignKeys(string $table): array
222222
tab1.name = ?
223223
X, [$table_name]);
224224

225-
$id = 0;
226225
while ($row = $rows->fetch()) {
227-
$keys[$id]['name'] = $row['fk_name'];
228-
$keys[$id]['local'] = $row['column'];
226+
$id = $row['fk_name'];
227+
$keys[$id]['name'] = $id;
228+
$keys[$id]['local'][] = $row['column'];
229229
$keys[$id]['table'] = $table_schema . '.' . $row['referenced_table'];
230-
$keys[$id++]['foreign'] = $row['referenced_column'];
230+
$keys[$id]['foreign'][] = $row['referenced_column'];
231231
}
232232

233233
return array_values($keys);

src/Database/Drivers/Engines/MySQLEngine.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -163,12 +163,12 @@ public function getForeignKeys(string $table): array
163163
AND TABLE_NAME = ?
164164
X, [$table]);
165165

166-
$id = 0;
167166
while ($row = $rows->fetch()) {
168-
$keys[$id]['name'] = $row['CONSTRAINT_NAME'];
169-
$keys[$id]['local'] = $row['COLUMN_NAME'];
167+
$id = $row['CONSTRAINT_NAME'];
168+
$keys[$id]['name'] = $id;
169+
$keys[$id]['local'][] = $row['COLUMN_NAME'];
170170
$keys[$id]['table'] = $row['REFERENCED_TABLE_NAME'];
171-
$keys[$id++]['foreign'] = $row['REFERENCED_COLUMN_NAME'];
171+
$keys[$id]['foreign'][] = $row['REFERENCED_COLUMN_NAME'];
172172
}
173173

174174
return array_values($keys);

src/Database/Drivers/Engines/PostgreSQLEngine.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,9 +223,14 @@ public function getForeignKeys(string $table): array
223223
X, [$this->delimiteFQN($table)]);
224224

225225
while ($row = $rows->fetch()) {
226-
$keys[] = $row;
226+
$id = $row['name'];
227+
$keys[$id]['name'] = $id;
228+
$keys[$id]['local'][] = $row['local'];
229+
$keys[$id]['table'] = $row['table'];
230+
$keys[$id]['foreign'][] = $row['foreign'];
227231
}
228-
return $keys;
232+
233+
return array_values($keys);
229234
}
230235

231236

src/Database/Drivers/Engines/SQLServerEngine.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,11 @@ public function getForeignKeys(string $table): array
222222
X, [$table]);
223223

224224
while ($row = $rows->fetch()) {
225-
$keys[$row['name']] = $row;
225+
$id = $row['name'];
226+
$keys[$id]['name'] = $id;
227+
$keys[$id]['local'][] = $row['local'];
228+
$keys[$id]['table'] = $row['table'];
229+
$keys[$id]['foreign'][] = $row['foreign'];
226230
}
227231

228232
return array_values($keys);

src/Database/Drivers/Engines/SQLiteEngine.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,9 +221,12 @@ public function getForeignKeys(string $table): array
221221
while ($row = $rows->fetch()) {
222222
$id = $row['id'];
223223
$keys[$id]['name'] = $id;
224-
$keys[$id]['local'] = $row['from'];
224+
$keys[$id]['local'][] = $row['from'];
225225
$keys[$id]['table'] = $row['table'];
226-
$keys[$id]['foreign'] = $row['to'];
226+
$keys[$id]['foreign'][] = $row['to'];
227+
if ($keys[$id]['foreign'][0] == null) {
228+
$keys[$id]['foreign'] = [];
229+
}
227230
}
228231

229232
return array_values($keys);

src/Database/Reflection/Table.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@ private function initForeignKeys(): void
9393
$id = $row['name'];
9494
$foreignTable = $this->reflection->getTable($row['table']);
9595
$tmp[$id][0] = $foreignTable;
96-
$tmp[$id][1][] = $this->getColumn($row['local']);
97-
$tmp[$id][2][] = $foreignTable->getColumn($row['foreign']);
96+
$tmp[$id][1] = array_map(fn($name) => $this->getColumn($name), $row['local']);
97+
$tmp[$id][2] = array_map(fn($name) => $foreignTable->getColumn($name), $row['foreign']);
9898
$tmp[$id][3] = is_string($id) ? $id : null;
9999
}
100100
$this->foreignKeys = array_map(fn($row) => new ForeignKey(...$row), array_values($tmp));

src/Database/Structure.php

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -220,17 +220,11 @@ protected function analyzeForeignKeys(array &$structure, string $table): void
220220

221221
$foreignKeys = $this->connection->getDatabaseEngine()->getForeignKeys($table);
222222

223-
$fksColumnsCounts = [];
224-
foreach ($foreignKeys as $foreignKey) {
225-
$tmp = &$fksColumnsCounts[$foreignKey['name']];
226-
$tmp++;
227-
}
228-
229-
usort($foreignKeys, fn($a, $b): int => $fksColumnsCounts[$b['name']] <=> $fksColumnsCounts[$a['name']]);
223+
usort($foreignKeys, fn($a, $b): int => count($b['local']) <=> count($a['local']));
230224

231225
foreach ($foreignKeys as $row) {
232-
$structure['belongsTo'][$lowerTable][$row['local']] = $row['table'];
233-
$structure['hasMany'][strtolower($row['table'])][$table][] = $row['local'];
226+
$structure['belongsTo'][$lowerTable][$row['local'][0]] = $row['table'];
227+
$structure['hasMany'][strtolower($row['table'])][$table][] = $row['local'][0];
234228
}
235229

236230
if (isset($structure['belongsTo'][$lowerTable])) {

tests/Database/Engine.postgre.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,9 @@ test('Tables in schema', function () use ($connection) {
6565
$foreign = $engine->getForeignKeys('one.slave');
6666
Assert::same([
6767
'name' => 'one_slave_fk',
68-
'local' => 'one_id',
68+
'local' => ['one_id'],
6969
'table' => 'one.master',
70-
'foreign' => 'one_id',
70+
'foreign' => ['one_id'],
7171
], (array) $foreign[0]);
7272

7373

tests/Database/Structure.phpt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,13 @@ class StructureTestCase extends TestCase
7474
$this->connection->shouldReceive('getDatabaseEngine')->times(4)->andReturn($this->engine);
7575
$this->engine->shouldReceive('getForeignKeys')->with('authors')->once()->andReturn([]);
7676
$this->engine->shouldReceive('getForeignKeys')->with('Books')->once()->andReturn([
77-
['local' => 'author_id', 'table' => 'authors', 'foreign' => 'id', 'name' => 'authors_fk1'],
78-
['local' => 'translator_id', 'table' => 'authors', 'foreign' => 'id', 'name' => 'authors_fk2'],
77+
['local' => ['author_id'], 'table' => 'authors', 'foreign' => ['id'], 'name' => 'authors_fk1'],
78+
['local' => ['translator_id'], 'table' => 'authors', 'foreign' => ['id'], 'name' => 'authors_fk2'],
7979
]);
8080
$this->engine->shouldReceive('getForeignKeys')->with('tags')->once()->andReturn([]);
8181
$this->engine->shouldReceive('getForeignKeys')->with('books_x_tags')->once()->andReturn([
82-
['local' => 'book_id', 'table' => 'Books', 'foreign' => 'id', 'name' => 'books_x_tags_fk1'],
83-
['local' => 'tag_id', 'table' => 'tags', 'foreign' => 'id', 'name' => 'books_x_tags_fk2'],
82+
['local' => ['book_id'], 'table' => 'Books', 'foreign' => ['id'], 'name' => 'books_x_tags_fk1'],
83+
['local' => ['tag_id'], 'table' => 'tags', 'foreign' => ['id'], 'name' => 'books_x_tags_fk2'],
8484
]);
8585

8686
$this->structure = new StructureMock($this->connection, $this->storage);

0 commit comments

Comments
 (0)