Skip to content

Commit

Permalink
drivers: getForeignKeys() works with multi-column foreign keys
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Apr 29, 2024
1 parent 2ef8ea2 commit 13dc813
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 30 deletions.
9 changes: 5 additions & 4 deletions src/Database/Drivers/MsSqlDriver.php
Expand Up @@ -181,11 +181,12 @@ public function getForeignKeys(string $table): array
tab1.name = {$this->pdo->quote($table_name)}
X;

foreach ($this->pdo->query($query) as $id => $row) {
$keys[$id]['name'] = $row['fk_name'];
$keys[$id]['local'] = $row['column'];
foreach ($this->pdo->query($query) as $row) {
$id = $row['fk_name'];
$keys[$id]['name'] = $id;
$keys[$id]['local'][] = $row['column'];
$keys[$id]['table'] = $table_schema . '.' . $row['referenced_table'];
$keys[$id]['foreign'] = $row['referenced_column'];
$keys[$id]['foreign'][] = $row['referenced_column'];
}

return array_values($keys);

Check failure on line 192 in src/Database/Drivers/MsSqlDriver.php

View workflow job for this annotation

GitHub Actions / PHPStan

Method Nette\Database\Drivers\MsSqlDriver::getForeignKeys() should return array<int, array{name: string, local: string, table: string, foreign: string}> but returns array<int, array{foreign: non-empty-array<int<0, max>, mixed>, local: non-empty-array<int<0, max>, mixed>, name: mixed, table: non-falsy-string}>.
Expand Down
9 changes: 5 additions & 4 deletions src/Database/Drivers/MySqlDriver.php
Expand Up @@ -181,11 +181,12 @@ public function getForeignKeys(string $table): array
WHERE TABLE_SCHEMA = DATABASE()
AND REFERENCED_TABLE_NAME IS NOT NULL
AND TABLE_NAME = {$this->pdo->quote($table)}
X) as $id => $row) {
$keys[$id]['name'] = $row['CONSTRAINT_NAME'];
$keys[$id]['local'] = $row['COLUMN_NAME'];
X) as $row) {
$id = $row['CONSTRAINT_NAME'];
$keys[$id]['name'] = $id;
$keys[$id]['local'][] = $row['COLUMN_NAME'];
$keys[$id]['table'] = $row['REFERENCED_TABLE_NAME'];
$keys[$id]['foreign'] = $row['REFERENCED_COLUMN_NAME'];
$keys[$id]['foreign'][] = $row['REFERENCED_COLUMN_NAME'];
}

return array_values($keys);
Expand Down
13 changes: 11 additions & 2 deletions src/Database/Drivers/PgSqlDriver.php
Expand Up @@ -182,7 +182,8 @@ public function getIndexes(string $table): array
public function getForeignKeys(string $table): array
{
/* Doesn't work with multi-column foreign keys */
return $this->pdo->query(<<<X
$keys = [];
foreach ($this->pdo->query(<<<X
SELECT
co.conname::varchar AS name,
al.attname::varchar AS local,
Expand All @@ -199,7 +200,15 @@ public function getForeignKeys(string $table): array
co.contype = 'f'
AND cl.oid = {$this->pdo->quote($this->delimiteFQN($table))}::regclass
AND nf.nspname = ANY (pg_catalog.current_schemas(FALSE))
X)->fetchAll(\PDO::FETCH_ASSOC);
X) as $row) {
$id = $row['name'];
$keys[$id]['name'] = $id;
$keys[$id]['local'][] = $row['local'];
$keys[$id]['table'] = $row['table'];
$keys[$id]['foreign'][] = $row['foreign'];
}

return array_values($keys);
}


Expand Down
7 changes: 5 additions & 2 deletions src/Database/Drivers/SqliteDriver.php
Expand Up @@ -216,9 +216,12 @@ public function getForeignKeys(string $table): array
foreach ($this->pdo->query("PRAGMA foreign_key_list({$this->delimite($table)})") as $row) {
$id = $row['id'];
$keys[$id]['name'] = $id;
$keys[$id]['local'] = $row['from'];
$keys[$id]['local'][] = $row['from'];
$keys[$id]['table'] = $row['table'];
$keys[$id]['foreign'] = $row['to'];
$keys[$id]['foreign'][] = $row['to'];
if ($keys[$id]['foreign'][0] == null) {
$keys[$id]['foreign'] = [];
}
}

return array_values($keys);
Expand Down
6 changes: 5 additions & 1 deletion src/Database/Drivers/SqlsrvDriver.php
Expand Up @@ -207,7 +207,11 @@ public function getForeignKeys(string $table): array
WHERE
tl.name = {$this->pdo->quote($table)}
X, \PDO::FETCH_ASSOC) as $row) {
$keys[$row['name']] = $row;
$id = $row['name'];
$keys[$id]['name'] = $id;
$keys[$id]['local'][] = $row['local'];
$keys[$id]['table'] = $row['table'];
$keys[$id]['foreign'][] = $row['column'];
}

return array_values($keys);
Expand Down
12 changes: 3 additions & 9 deletions src/Database/Structure.php
Expand Up @@ -235,17 +235,11 @@ protected function analyzeForeignKeys(array &$structure, string $table): void

$foreignKeys = $this->connection->getDriver()->getForeignKeys($table);

$fksColumnsCounts = [];
foreach ($foreignKeys as $foreignKey) {
$tmp = &$fksColumnsCounts[$foreignKey['name']];
$tmp++;
}

usort($foreignKeys, fn($a, $b): int => $fksColumnsCounts[$b['name']] <=> $fksColumnsCounts[$a['name']]);
usort($foreignKeys, fn($a, $b): int => count($b['local']) <=> count($a['local']));

foreach ($foreignKeys as $row) {
$structure['belongsTo'][$lowerTable][$row['local']] = $row['table'];
$structure['hasMany'][strtolower($row['table'])][$table][] = $row['local'];
$structure['belongsTo'][$lowerTable][$row['local'][0]] = $row['table'];
$structure['hasMany'][strtolower($row['table'])][$table][] = $row['local'][0];
}

if (isset($structure['belongsTo'][$lowerTable])) {
Expand Down
4 changes: 2 additions & 2 deletions tests/Database/Reflection.postgre.phpt
Expand Up @@ -63,9 +63,9 @@ test('Tables in schema', function () use ($connection) {
$foreign = $driver->getForeignKeys('one.slave');
Assert::same([
'name' => 'one_slave_fk',
'local' => 'one_id',
'local' => ['one_id'],
'table' => 'one.master',
'foreign' => 'one_id',
'foreign' => ['one_id'],
], (array) $foreign[0]);


Expand Down
8 changes: 4 additions & 4 deletions tests/Database/Structure.phpt
Expand Up @@ -74,13 +74,13 @@ class StructureTestCase extends TestCase
$this->connection->shouldReceive('getDriver')->times(4)->andReturn($this->driver);
$this->driver->shouldReceive('getForeignKeys')->with('authors')->once()->andReturn([]);
$this->driver->shouldReceive('getForeignKeys')->with('Books')->once()->andReturn([
['local' => 'author_id', 'table' => 'authors', 'foreign' => 'id', 'name' => 'authors_fk1'],
['local' => 'translator_id', 'table' => 'authors', 'foreign' => 'id', 'name' => 'authors_fk2'],
['local' => ['author_id'], 'table' => 'authors', 'foreign' => ['id'], 'name' => 'authors_fk1'],
['local' => ['translator_id'], 'table' => 'authors', 'foreign' => ['id'], 'name' => 'authors_fk2'],
]);
$this->driver->shouldReceive('getForeignKeys')->with('tags')->once()->andReturn([]);
$this->driver->shouldReceive('getForeignKeys')->with('books_x_tags')->once()->andReturn([
['local' => 'book_id', 'table' => 'Books', 'foreign' => 'id', 'name' => 'books_x_tags_fk1'],
['local' => 'tag_id', 'table' => 'tags', 'foreign' => 'id', 'name' => 'books_x_tags_fk2'],
['local' => ['book_id'], 'table' => 'Books', 'foreign' => ['id'], 'name' => 'books_x_tags_fk1'],
['local' => ['tag_id'], 'table' => 'tags', 'foreign' => ['id'], 'name' => 'books_x_tags_fk2'],
]);

$this->structure = new StructureMock($this->connection, $this->storage);
Expand Down
4 changes: 2 additions & 2 deletions tests/Database/Structure.schemas.phpt
Expand Up @@ -60,8 +60,8 @@ class StructureSchemasTestCase extends TestCase
$this->connection->shouldReceive('getDriver')->times(2)->andReturn($this->driver);
$this->driver->shouldReceive('getForeignKeys')->with('authors.authors')->once()->andReturn([]);
$this->driver->shouldReceive('getForeignKeys')->with('books.books')->once()->andReturn([
['local' => 'author_id', 'table' => 'authors.authors', 'foreign' => 'id', 'name' => 'authors_authors_fk1'],
['local' => 'translator_id', 'table' => 'authors.authors', 'foreign' => 'id', 'name' => 'authors_authors_fk2'],
['local' => ['author_id'], 'table' => 'authors.authors', 'foreign' => ['id'], 'name' => 'authors_authors_fk1'],
['local' => ['translator_id'], 'table' => 'authors.authors', 'foreign' => ['id'], 'name' => 'authors_authors_fk2'],
]);

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

0 comments on commit 13dc813

Please sign in to comment.