diff --git a/src/Schema/SQLiteSchemaManager.php b/src/Schema/SQLiteSchemaManager.php index 5c2d7934d0d..c001c2504eb 100644 --- a/src/Schema/SQLiteSchemaManager.php +++ b/src/Schema/SQLiteSchemaManager.php @@ -79,7 +79,7 @@ public function listTableForeignKeys(string $table): array { $table = $this->normalizeName($table); - $columns = $this->selectForeignKeyColumns($database ?? 'main', $table) + $columns = $this->selectForeignKeyColumns('main', $table) ->fetchAllAssociative(); if (count($columns) > 0) { diff --git a/tests/Functional/Schema/SqliteSchemaManagerTest.php b/tests/Functional/Schema/SqliteSchemaManagerTest.php index 8babc2ad277..b8ed3193e91 100644 --- a/tests/Functional/Schema/SqliteSchemaManagerTest.php +++ b/tests/Functional/Schema/SqliteSchemaManagerTest.php @@ -7,12 +7,15 @@ use Doctrine\DBAL\Exception; use Doctrine\DBAL\Platforms\AbstractPlatform; use Doctrine\DBAL\Platforms\SQLitePlatform; +use Doctrine\DBAL\Schema\Column; use Doctrine\DBAL\Schema\ForeignKeyConstraint; use Doctrine\DBAL\Schema\Table; +use Doctrine\DBAL\Schema\TableDiff; use Doctrine\DBAL\Types\BlobType; use Doctrine\DBAL\Types\Type; use Doctrine\DBAL\Types\Types; +use function array_keys; use function array_shift; class SqliteSchemaManagerTest extends SchemaManagerFunctionalTestCase @@ -207,24 +210,22 @@ public function testNonSimpleAlterTableCreatedFromDDL(): void self::assertSame(['name'], $index->getColumns()); } - public function testGeneratesAlterTableRenameColumnSQLWithSchema(): void + public function testAlterTableWithSchema(): void { - $this->platform->disableSchemaEmulation(); + $this->dropTableIfExists('t'); $table = new Table('main.t'); $table->addColumn('a', Types::INTEGER); + $this->schemaManager->createTable($table); - $tableDiff = new TableDiff('t'); - $tableDiff->fromTable = $table; - $tableDiff->renamedColumns['a'] = new Column('b', Type::getType(Types::INTEGER)); - - self::assertSame([ - 'CREATE TEMPORARY TABLE __temp__t AS SELECT a FROM main.t', - 'DROP TABLE main.t', - 'CREATE TABLE main.t (b INTEGER NOT NULL)', - 'INSERT INTO main.t (b) SELECT a FROM __temp__t', - 'DROP TABLE __temp__t', - ], $this->platform->getAlterTableSQL($tableDiff)); + self::assertSame(['a'], array_keys($this->schemaManager->listTableColumns('t'))); + + $tableDiff = new TableDiff($table, [], [], [], [ + 'a' => new Column('b', Type::getType(Types::INTEGER)), + ], [], [], [], [], [], [], []); + $this->schemaManager->alterTable($tableDiff); + + self::assertSame(['b'], array_keys($this->schemaManager->listTableColumns('t'))); } public function testIntrospectMultipleAnonymousForeignKeyConstraints(): void diff --git a/tests/Platforms/SQLitePlatformTest.php b/tests/Platforms/SQLitePlatformTest.php index 5084369ea20..d9d421c47fa 100644 --- a/tests/Platforms/SQLitePlatformTest.php +++ b/tests/Platforms/SQLitePlatformTest.php @@ -450,9 +450,9 @@ public function testGeneratesAlterTableRenameColumnSQLWithSchema(): void $table = new Table('main.t'); $table->addColumn('a', Types::INTEGER); - $tableDiff = new TableDiff('t'); - $tableDiff->fromTable = $table; - $tableDiff->renamedColumns['a'] = new Column('b', Type::getType(Types::INTEGER)); + $tableDiff = new TableDiff($table, [], [], [], [ + 'a' => new Column('b', Type::getType(Types::INTEGER)), + ], [], [], [], [], [], [], []); self::assertSame([ 'CREATE TEMPORARY TABLE __temp__t AS SELECT a FROM main.t', diff --git a/tests/Schema/SQLiteSchemaManagerTest.php b/tests/Schema/SQLiteSchemaManagerTest.php index ad322a6706c..f7ad776cd32 100644 --- a/tests/Schema/SQLiteSchemaManagerTest.php +++ b/tests/Schema/SQLiteSchemaManagerTest.php @@ -6,6 +6,7 @@ use Doctrine\DBAL\Connection; use Doctrine\DBAL\Platforms\SQLitePlatform; +use Doctrine\DBAL\Result; use Doctrine\DBAL\Schema\SQLiteSchemaManager; use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; @@ -202,4 +203,27 @@ 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); + } }