Skip to content

Commit 1c6cc29

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

File tree

4 files changed

+67
-4
lines changed

4 files changed

+67
-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

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

0 commit comments

Comments
 (0)