Skip to content

Commit 49f86b9

Browse files
committed
Fix unwanted SQLite schema emulation in SqliteSchemaManager
1 parent e5db00e commit 49f86b9

File tree

4 files changed

+69
-4
lines changed

4 files changed

+69
-4
lines changed

psalm.xml.dist

+3
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,9 @@
519519

520520
<!-- TODO: remove in 4.0.0 -->
521521
<referencedMethod name="Doctrine\DBAL\Platforms\DB2Platform::getForUpdateSQL"/>
522+
523+
<!-- TODO: remove in 4.0.0 -->
524+
<referencedMethod name="Doctrine\DBAL\Platforms\SqlitePlatform::canEmulateSchemas"/>
522525
</errorLevel>
523526
</DeprecatedMethod>
524527
<DeprecatedProperty>

src/Platforms/SqlitePlatform.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -908,7 +908,7 @@ public function getTemporaryTableName($tableName)
908908
*/
909909
public function canEmulateSchemas()
910910
{
911-
Deprecation::trigger(
911+
Deprecation::triggerIfCalledFromOutside(
912912
'doctrine/dbal',
913913
'https://github.com/doctrine/dbal/pull/4805',
914914
'SqlitePlatform::canEmulateSchemas() is deprecated.',

src/Schema/SqliteSchemaManager.php

+9-3
Original file line numberDiff line numberDiff line change
@@ -704,7 +704,9 @@ protected function selectTableColumns(string $databaseName, ?string $tableName =
704704

705705
if ($tableName !== null) {
706706
$conditions[] = 't.name = ?';
707-
$params[] = str_replace('.', '__', $tableName);
707+
$params[] = $this->_platform->canEmulateSchemas()
708+
? str_replace('.', '__', $tableName)
709+
: $tableName;
708710
}
709711

710712
$sql .= ' WHERE ' . implode(' AND ', $conditions) . ' ORDER BY t.name, c.cid';
@@ -729,7 +731,9 @@ protected function selectIndexColumns(string $databaseName, ?string $tableName =
729731

730732
if ($tableName !== null) {
731733
$conditions[] = 't.name = ?';
732-
$params[] = str_replace('.', '__', $tableName);
734+
$params[] = $this->_platform->canEmulateSchemas()
735+
? str_replace('.', '__', $tableName)
736+
: $tableName;
733737
}
734738

735739
$sql .= ' WHERE ' . implode(' AND ', $conditions) . ' ORDER BY t.name, i.seq';
@@ -755,7 +759,9 @@ protected function selectForeignKeyColumns(string $databaseName, ?string $tableN
755759

756760
if ($tableName !== null) {
757761
$conditions[] = 't.name = ?';
758-
$params[] = str_replace('.', '__', $tableName);
762+
$params[] = $this->_platform->canEmulateSchemas()
763+
? str_replace('.', '__', $tableName)
764+
: $tableName;
759765
}
760766

761767
$sql .= ' WHERE ' . implode(' AND ', $conditions) . ' ORDER BY t.name, p.id DESC, p.seq';

tests/Functional/Schema/SqliteSchemaManagerTest.php

+56
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,11 @@
1010
use Doctrine\DBAL\Types\BlobType;
1111
use Doctrine\DBAL\Types\Type;
1212
use Doctrine\DBAL\Types\Types;
13+
use ReflectionMethod;
1314

15+
use function array_map;
1416
use function array_shift;
17+
use function assert;
1518
use function dirname;
1619

1720
class SqliteSchemaManagerTest extends SchemaManagerFunctionalTestCase
@@ -371,4 +374,57 @@ public function testShorthandInForeignKeyReferenceWithMultipleColumns(): void
371374
$createTableTrackSql,
372375
);
373376
}
377+
378+
public function testListTableNoSchemaEmulation(): void
379+
{
380+
$this->dropTableIfExists('`list_table_no_schema_emulation.test`');
381+
382+
$ddl = <<<'DDL'
383+
CREATE TABLE `list_table_no_schema_emulation.test` (
384+
id INTEGER,
385+
parent_id INTEGER,
386+
PRIMARY KEY (id),
387+
FOREIGN KEY (parent_id) REFERENCES `list_table_no_schema_emulation.test` (id)
388+
);
389+
DDL;
390+
$dd2 = <<<'DDL'
391+
CREATE INDEX i ON `list_table_no_schema_emulation.test` (parent_id);
392+
DDL;
393+
394+
$this->connection->executeStatement($ddl);
395+
$this->connection->executeStatement($dd2);
396+
397+
$databasePlatform = $this->connection->getDatabasePlatform();
398+
assert($databasePlatform instanceof SqlitePlatform);
399+
$databasePlatform->disableSchemaEmulation();
400+
401+
$schemaManager = $this->schemaManager;
402+
$refl = new ReflectionMethod($schemaManager, 'selectTableColumns');
403+
$refl->setAccessible(true);
404+
$res = $refl->invoke($schemaManager, 'main', 'list_table_no_schema_emulation.test')
405+
->fetchAllAssociative();
406+
407+
self::assertSame([
408+
['list_table_no_schema_emulation.test', 'id'],
409+
['list_table_no_schema_emulation.test', 'parent_id'],
410+
], array_map(static fn (array $row) => [$row['table_name'], $row['name']], $res));
411+
412+
$refl = new ReflectionMethod($schemaManager, 'selectIndexColumns');
413+
$refl->setAccessible(true);
414+
$res = $refl->invoke($schemaManager, 'main', 'list_table_no_schema_emulation.test')
415+
->fetchAllAssociative();
416+
417+
self::assertSame([
418+
['list_table_no_schema_emulation.test', 'i'],
419+
], array_map(static fn (array $row) => [$row['table_name'], $row['name']], $res));
420+
421+
$refl = new ReflectionMethod($schemaManager, 'selectForeignKeyColumns');
422+
$refl->setAccessible(true);
423+
$res = $refl->invoke($schemaManager, 'main', 'list_table_no_schema_emulation.test')
424+
->fetchAllAssociative();
425+
426+
self::assertSame([
427+
['list_table_no_schema_emulation.test', 'parent_id', 'id'],
428+
], array_map(static fn (array $row) => [$row['table_name'], $row['from'], $row['to']], $res));
429+
}
374430
}

0 commit comments

Comments
 (0)