Skip to content

Commit ae1fb56

Browse files
committed
drivers: getForeignKeys() works with multi-column foreign keys
1 parent 03000ee commit ae1fb56

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
@@ -201,12 +201,12 @@ public function getForeignKeys(string $table): array
201201
tab1.name = ?
202202
X, [$table_name]);
203203

204-
$id = 0;
205204
while ($row = $rows->fetch()) {
206-
$keys[$id]['name'] = $row['fk_name'];
207-
$keys[$id]['local'] = $row['column'];
205+
$id = $row['fk_name'];
206+
$keys[$id]['name'] = $id;
207+
$keys[$id]['local'][] = $row['column'];
208208
$keys[$id]['table'] = $table_schema . '.' . $row['referenced_table'];
209-
$keys[$id++]['foreign'] = $row['referenced_column'];
209+
$keys[$id]['foreign'][] = $row['referenced_column'];
210210
}
211211

212212
return array_values($keys);

src/Database/Drivers/Engines/MySQLEngine.php

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

158-
$id = 0;
159158
while ($row = $rows->fetch()) {
160-
$keys[$id]['name'] = $row['CONSTRAINT_NAME'];
161-
$keys[$id]['local'] = $row['COLUMN_NAME'];
159+
$id = $row['CONSTRAINT_NAME'];
160+
$keys[$id]['name'] = $id;
161+
$keys[$id]['local'][] = $row['COLUMN_NAME'];
162162
$keys[$id]['table'] = $row['REFERENCED_TABLE_NAME'];
163-
$keys[$id++]['foreign'] = $row['REFERENCED_COLUMN_NAME'];
163+
$keys[$id]['foreign'][] = $row['REFERENCED_COLUMN_NAME'];
164164
}
165165

166166
return array_values($keys);

src/Database/Drivers/Engines/PostgreSQLEngine.php

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

221221
while ($row = $rows->fetch()) {
222-
$keys[] = $row;
222+
$id = $row['name'];
223+
$keys[$id]['name'] = $id;
224+
$keys[$id]['local'][] = $row['local'];
225+
$keys[$id]['table'] = $row['table'];
226+
$keys[$id]['foreign'][] = $row['foreign'];
223227
}
224-
return $keys;
228+
229+
return array_values($keys);
225230
}
226231

227232

src/Database/Drivers/Engines/SQLServerEngine.php

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

211211
while ($row = $rows->fetch()) {
212-
$keys[$row['name']] = $row;
212+
$id = $row['name'];
213+
$keys[$id]['name'] = $id;
214+
$keys[$id]['local'][] = $row['local'];
215+
$keys[$id]['table'] = $row['table'];
216+
$keys[$id]['foreign'][] = $row['foreign'];
213217
}
214218

215219
return array_values($keys);

src/Database/Drivers/Engines/SQLiteEngine.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,9 +218,12 @@ public function getForeignKeys(string $table): array
218218
while ($row = $rows->fetch()) {
219219
$id = $row['id'];
220220
$keys[$id]['name'] = $id;
221-
$keys[$id]['local'] = $row['from'];
221+
$keys[$id]['local'][] = $row['from'];
222222
$keys[$id]['table'] = $row['table'];
223-
$keys[$id]['foreign'] = $row['to'];
223+
$keys[$id]['foreign'][] = $row['to'];
224+
if ($keys[$id]['foreign'][0] == null) {
225+
$keys[$id]['foreign'] = [];
226+
}
224227
}
225228

226229
return array_values($keys);

src/Database/Reflection/Table.php

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

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

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

230224
foreach ($foreignKeys as $row) {
231-
$structure['belongsTo'][$lowerTable][$row['local']] = $row['table'];
232-
$structure['hasMany'][strtolower($row['table'])][$table][] = $row['local'];
225+
$structure['belongsTo'][$lowerTable][$row['local'][0]] = $row['table'];
226+
$structure['hasMany'][strtolower($row['table'])][$table][] = $row['local'][0];
233227
}
234228

235229
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)