Skip to content

Commit ebdc51c

Browse files
authored
Merge pull request #6337 from mvorisek/fix_6314_sqlite_schema_emulation
Allow to disable SQLite schema emulation in SqliteSchemaManager
2 parents 9894055 + 7d710c9 commit ebdc51c

File tree

3 files changed

+88
-4
lines changed

3 files changed

+88
-4
lines changed

src/Platforms/SqlitePlatform.php

Lines changed: 1 addition & 1 deletion
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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -715,7 +715,9 @@ protected function selectTableColumns(string $databaseName, ?string $tableName =
715715

716716
if ($tableName !== null) {
717717
$conditions[] = 't.name = ?';
718-
$params[] = str_replace('.', '__', $tableName);
718+
$params[] = $this->_platform->canEmulateSchemas()
719+
? str_replace('.', '__', $tableName)
720+
: $tableName;
719721
}
720722

721723
$sql .= ' WHERE ' . implode(' AND ', $conditions) . ' ORDER BY t.name, c.cid';
@@ -740,7 +742,9 @@ protected function selectIndexColumns(string $databaseName, ?string $tableName =
740742

741743
if ($tableName !== null) {
742744
$conditions[] = 't.name = ?';
743-
$params[] = str_replace('.', '__', $tableName);
745+
$params[] = $this->_platform->canEmulateSchemas()
746+
? str_replace('.', '__', $tableName)
747+
: $tableName;
744748
}
745749

746750
$sql .= ' WHERE ' . implode(' AND ', $conditions) . ' ORDER BY t.name, i.seq';
@@ -766,7 +770,9 @@ protected function selectForeignKeyColumns(string $databaseName, ?string $tableN
766770

767771
if ($tableName !== null) {
768772
$conditions[] = 't.name = ?';
769-
$params[] = str_replace('.', '__', $tableName);
773+
$params[] = $this->_platform->canEmulateSchemas()
774+
? str_replace('.', '__', $tableName)
775+
: $tableName;
770776
}
771777

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

tests/Functional/Schema/SqliteSchemaManagerTest.php

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@
77
use Doctrine\DBAL\Platforms\SqlitePlatform;
88
use Doctrine\DBAL\Schema\Column;
99
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
10+
use Doctrine\DBAL\Schema\SqliteSchemaManager;
1011
use Doctrine\DBAL\Schema\Table;
1112
use Doctrine\DBAL\Schema\TableDiff;
1213
use Doctrine\DBAL\Types\BlobType;
1314
use Doctrine\DBAL\Types\Type;
1415
use Doctrine\DBAL\Types\Types;
1516

1617
use function array_keys;
18+
use function array_map;
1719
use function array_shift;
1820
use function assert;
1921
use function dirname;
@@ -398,6 +400,82 @@ public function testShorthandInForeignKeyReferenceWithMultipleColumns(): void
398400
);
399401
}
400402

403+
public function testListTableNoSchemaEmulation(): void
404+
{
405+
$databasePlatform = $this->connection->getDatabasePlatform();
406+
assert($databasePlatform instanceof SqlitePlatform);
407+
$databasePlatform->disableSchemaEmulation();
408+
409+
$this->dropTableIfExists('`list_table_no_schema_emulation.test`');
410+
411+
$this->connection->executeStatement(<<<'DDL'
412+
CREATE TABLE `list_table_no_schema_emulation.test` (
413+
id INTEGER,
414+
parent_id INTEGER,
415+
PRIMARY KEY (id),
416+
FOREIGN KEY (parent_id) REFERENCES `list_table_no_schema_emulation.test` (id)
417+
);
418+
DDL);
419+
420+
$this->connection->executeStatement(<<<'DDL'
421+
CREATE INDEX i ON `list_table_no_schema_emulation.test` (parent_id);
422+
DDL);
423+
424+
$customSqliteSchemaManager = new class ($this->connection, $databasePlatform) extends SqliteSchemaManager {
425+
/** @return list<array<string, mixed>> */
426+
public function selectTableColumnsWithSchema(): array
427+
{
428+
return $this->selectTableColumns('main', 'list_table_no_schema_emulation.test')
429+
->fetchAllAssociative();
430+
}
431+
432+
/** @return list<array<string, mixed>> */
433+
public function selectIndexColumnsWithSchema(): array
434+
{
435+
return $this->selectIndexColumns('main', 'list_table_no_schema_emulation.test')
436+
->fetchAllAssociative();
437+
}
438+
439+
/** @return list<array<string, mixed>> */
440+
public function selectForeignKeyColumnsWithSchema(): array
441+
{
442+
return $this->selectForeignKeyColumns('main', 'list_table_no_schema_emulation.test')
443+
->fetchAllAssociative();
444+
}
445+
};
446+
447+
self::assertSame(
448+
[
449+
['list_table_no_schema_emulation.test', 'id'],
450+
['list_table_no_schema_emulation.test', 'parent_id'],
451+
],
452+
array_map(
453+
static fn (array $row) => [$row['table_name'], $row['name']],
454+
$customSqliteSchemaManager->selectTableColumnsWithSchema(),
455+
),
456+
);
457+
458+
self::assertSame(
459+
[
460+
['list_table_no_schema_emulation.test', 'i'],
461+
],
462+
array_map(
463+
static fn (array $row) => [$row['table_name'], $row['name']],
464+
$customSqliteSchemaManager->selectIndexColumnsWithSchema(),
465+
),
466+
);
467+
468+
self::assertSame(
469+
[
470+
['list_table_no_schema_emulation.test', 'parent_id', 'id'],
471+
],
472+
array_map(
473+
static fn (array $row) => [$row['table_name'], $row['from'], $row['to']],
474+
$customSqliteSchemaManager->selectForeignKeyColumnsWithSchema(),
475+
),
476+
);
477+
}
478+
401479
/**
402480
* This test duplicates {@see parent::testCommentInTable()} with the only difference that the name of the table
403481
* being created is quoted. It is only meant to cover the logic of parsing the SQLite CREATE TABLE statement

0 commit comments

Comments
 (0)