Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix DB name passing in SqliteSchemaManager::listTableForeignKeys() #6338

Merged
merged 1 commit into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Schema/SqliteSchemaManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ public function listTableForeignKeys($table, $database = null)
{
$table = $this->normalizeName($table);

$columns = $this->selectForeignKeyColumns('', $table)
$columns = $this->selectForeignKeyColumns($database ?? 'main', $table)
->fetchAllAssociative();

if (count($columns) > 0) {
Expand Down
30 changes: 30 additions & 0 deletions tests/Schema/SqliteSchemaManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Platforms\SqlitePlatform;
use Doctrine\DBAL\Result;
use Doctrine\DBAL\Schema\SqliteSchemaManager;
use PHPUnit\Framework\TestCase;
use ReflectionMethod;
Expand Down Expand Up @@ -340,4 +341,33 @@ public static function getDataColumnComment(): iterable
],
];
}

/**
* TODO move to functional test once SqliteSchemaManager::selectForeignKeyColumns can honor database/schema name
* https://github.com/doctrine/dbal/blob/3.8.3/src/Schema/SqliteSchemaManager.php#L740
*/
public function testListTableForeignKeysDefaultDatabasePassing(): void
{
$conn = $this->createMock(Connection::class);

$manager = new class ($conn, new SqlitePlatform()) extends SqliteSchemaManager {
public static string $passedDatabaseName;

protected function selectForeignKeyColumns(string $databaseName, ?string $tableName = null): Result
{
self::$passedDatabaseName = $databaseName;

return parent::selectForeignKeyColumns($databaseName, $tableName);
}
};

$manager->listTableForeignKeys('t');
self::assertSame('main', $manager::$passedDatabaseName);

$manager->listTableForeignKeys('t', 'd');
self::assertSame('d', $manager::$passedDatabaseName);

$manager->listTableForeignKeys('t');
self::assertSame('main', $manager::$passedDatabaseName);
}
}