Skip to content

Commit f54433d

Browse files
committed
drivers: getForeignKeys() works with multi-column foreign keys
1 parent 384ab71 commit f54433d

11 files changed

Lines changed: 39 additions & 33 deletions

File tree

src/Database/Drivers/Engine.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ function getColumns(string $table): array;
6464
/** @return list<array{name: string, columns: list<string>, unique: bool, primary: bool}> */
6565
function getIndexes(string $table): array;
6666

67-
/** @return list<array{name: string, local: string, table: string, foreign: string}> */
67+
/** @return list<array{name: string, local: list<string>, table: string, foreign: list<string>}> */
6868
function getForeignKeys(string $table): array;
6969

7070
/**

src/Database/Drivers/Engines/MSSQLEngine.php

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

203-
$id = 0;
204203
while ($row = $rows->fetch()) {
205-
$keys[$id]['name'] = $row['fk_name'];
206-
$keys[$id]['local'] = $row['column'];
204+
$id = $row['fk_name'];
205+
$keys[$id]['name'] = $id;
206+
$keys[$id]['local'][] = $row['column'];
207207
$keys[$id]['table'] = $table_schema . '.' . $row['referenced_table'];
208-
$keys[$id++]['foreign'] = $row['referenced_column'];
208+
$keys[$id]['foreign'][] = $row['referenced_column'];
209209
}
210210

211211
return array_values($keys);

src/Database/Drivers/Engines/MySQLEngine.php

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

165-
$id = 0;
166165
while ($row = $rows->fetch()) {
167-
$keys[$id]['name'] = $row['CONSTRAINT_NAME'];
168-
$keys[$id]['local'] = $row['COLUMN_NAME'];
166+
$id = $row['CONSTRAINT_NAME'];
167+
$keys[$id]['name'] = $id;
168+
$keys[$id]['local'][] = $row['COLUMN_NAME'];
169169
$keys[$id]['table'] = $row['REFERENCED_TABLE_NAME'];
170-
$keys[$id++]['foreign'] = $row['REFERENCED_COLUMN_NAME'];
170+
$keys[$id]['foreign'][] = $row['REFERENCED_COLUMN_NAME'];
171171
}
172172

173173
return array_values($keys);

src/Database/Drivers/Engines/PostgreSQLEngine.php

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

223223
while ($row = $rows->fetch()) {
224-
$keys[] = $row;
224+
$id = $row['name'];
225+
$keys[$id]['name'] = $id;
226+
$keys[$id]['local'][] = $row['local'];
227+
$keys[$id]['table'] = $row['table'];
228+
$keys[$id]['foreign'][] = $row['foreign'];
225229
}
226-
return $keys;
230+
231+
return array_values($keys);
227232
}
228233

229234

src/Database/Drivers/Engines/SQLServerEngine.php

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

217217
while ($row = $rows->fetch()) {
218-
$keys[$row['name']] = $row;
218+
$id = $row['name'];
219+
$keys[$id]['name'] = $id;
220+
$keys[$id]['local'][] = $row['local'];
221+
$keys[$id]['table'] = $row['table'];
222+
$keys[$id]['foreign'][] = $row['foreign'];
219223
}
220224

221225
return array_values($keys);

src/Database/Drivers/Engines/SQLiteEngine.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,9 +225,12 @@ public function getForeignKeys(string $table): array
225225
while ($row = $rows->fetch()) {
226226
$id = $row['id'];
227227
$keys[$id]['name'] = $id;
228-
$keys[$id]['local'] = $row['from'];
228+
$keys[$id]['local'][] = $row['from'];
229229
$keys[$id]['table'] = $row['table'];
230-
$keys[$id]['foreign'] = $row['to'];
230+
$keys[$id]['foreign'][] = $row['to'];
231+
if ($keys[$id]['foreign'][0] == null) {
232+
$keys[$id]['foreign'] = [];
233+
}
231234
}
232235

233236
return array_values($keys);

src/Database/Reflection/Table.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ private function initForeignKeys(): void
8686
$id = $row['name'];
8787
$foreignTable = $this->reflection->getTable($row['table']);
8888
$tmp[$id][0] = $foreignTable;
89-
$tmp[$id][1][] = $this->getColumn($row['local']);
90-
$tmp[$id][2][] = $foreignTable->getColumn($row['foreign']);
89+
$tmp[$id][1] = array_map(fn($name) => $this->getColumn($name), $row['local']);
90+
$tmp[$id][2] = array_map(fn($name) => $foreignTable->getColumn($name), $row['foreign']);
9191
$tmp[$id][3] = is_string($id) ? $id : null;
9292
}
9393
$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
@@ -235,17 +235,11 @@ protected function analyzeForeignKeys(array &$structure, string $table): void
235235

236236
$foreignKeys = $this->connection->getEngine()->getForeignKeys($table);
237237

238-
$fksColumnsCounts = [];
239-
foreach ($foreignKeys as $foreignKey) {
240-
$tmp = &$fksColumnsCounts[$foreignKey['name']];
241-
$tmp++;
242-
}
243-
244-
usort($foreignKeys, fn($a, $b): int => $fksColumnsCounts[$b['name']] <=> $fksColumnsCounts[$a['name']]);
238+
usort($foreignKeys, fn($a, $b): int => count($b['local']) <=> count($a['local']));
245239

246240
foreach ($foreignKeys as $row) {
247-
$structure['belongsTo'][$lowerTable][$row['local']] = $row['table'];
248-
$structure['hasMany'][strtolower($row['table'])][$table][] = $row['local'];
241+
$structure['belongsTo'][$lowerTable][$row['local'][0]] = $row['table'];
242+
$structure['hasMany'][strtolower($row['table'])][$table][] = $row['local'][0];
249243
}
250244

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

tests/Database/Reflection.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('getEngine')->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)