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 unwanted SQLite schema emulation in SqliteSchemaManager #6337

Open
wants to merge 2 commits into
base: 3.9.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions psalm.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,9 @@

<!-- TODO: remove in 4.0.0 -->
<referencedMethod name="Doctrine\DBAL\Platforms\DB2Platform::getForUpdateSQL"/>

<!-- TODO: remove in 4.0.0 -->
<referencedMethod name="Doctrine\DBAL\Platforms\SqlitePlatform::canEmulateSchemas"/>
</errorLevel>
</DeprecatedMethod>
<DeprecatedProperty>
Expand Down
2 changes: 1 addition & 1 deletion src/Platforms/SqlitePlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -908,7 +908,7 @@ public function getTemporaryTableName($tableName)
*/
public function canEmulateSchemas()
{
Deprecation::trigger(
Deprecation::triggerIfCalledFromOutside(
'doctrine/dbal',
'https://github.com/doctrine/dbal/pull/4805',
'SqlitePlatform::canEmulateSchemas() is deprecated.',
Expand Down
12 changes: 9 additions & 3 deletions src/Schema/SqliteSchemaManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -715,7 +715,9 @@ protected function selectTableColumns(string $databaseName, ?string $tableName =

if ($tableName !== null) {
$conditions[] = 't.name = ?';
$params[] = str_replace('.', '__', $tableName);
$params[] = $this->_platform->canEmulateSchemas()
? str_replace('.', '__', $tableName)
: $tableName;
}

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

if ($tableName !== null) {
$conditions[] = 't.name = ?';
$params[] = str_replace('.', '__', $tableName);
$params[] = $this->_platform->canEmulateSchemas()
? str_replace('.', '__', $tableName)
: $tableName;
}

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

if ($tableName !== null) {
$conditions[] = 't.name = ?';
$params[] = str_replace('.', '__', $tableName);
$params[] = $this->_platform->canEmulateSchemas()
? str_replace('.', '__', $tableName)
: $tableName;
}

$sql .= ' WHERE ' . implode(' AND ', $conditions) . ' ORDER BY t.name, p.id DESC, p.seq';
Expand Down
78 changes: 78 additions & 0 deletions tests/Functional/Schema/SqliteSchemaManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@
use Doctrine\DBAL\Platforms\SqlitePlatform;
use Doctrine\DBAL\Schema\Column;
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
use Doctrine\DBAL\Schema\SqliteSchemaManager;
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_map;
use function array_shift;
use function assert;
use function dirname;
Expand Down Expand Up @@ -398,6 +400,82 @@ public function testShorthandInForeignKeyReferenceWithMultipleColumns(): void
);
}

public function testListTableNoSchemaEmulation(): void
{
$databasePlatform = $this->connection->getDatabasePlatform();
assert($databasePlatform instanceof SqlitePlatform);
$databasePlatform->disableSchemaEmulation();

$this->dropTableIfExists('`list_table_no_schema_emulation.test`');

$this->connection->executeStatement(<<<'DDL'
CREATE TABLE `list_table_no_schema_emulation.test` (
id INTEGER,
parent_id INTEGER,
PRIMARY KEY (id),
FOREIGN KEY (parent_id) REFERENCES `list_table_no_schema_emulation.test` (id)
);
DDL);

$this->connection->executeStatement(<<<'DDL'
CREATE INDEX i ON `list_table_no_schema_emulation.test` (parent_id);
DDL);

$customSqliteSchemaManager = new class ($this->connection, $databasePlatform) extends SqliteSchemaManager {
/** @return list<array<string, mixed>> */
public function selectTableColumnsWithSchema(): array
{
return $this->selectTableColumns('main', 'list_table_no_schema_emulation.test')
->fetchAllAssociative();
}

/** @return list<array<string, mixed>> */
public function selectIndexColumnsWithSchema(): array
{
return $this->selectIndexColumns('main', 'list_table_no_schema_emulation.test')
->fetchAllAssociative();
}

/** @return list<array<string, mixed>> */
public function selectForeignKeyColumnsWithSchema(): array
{
return $this->selectForeignKeyColumns('main', 'list_table_no_schema_emulation.test')
->fetchAllAssociative();
}
};

self::assertSame(
[
['list_table_no_schema_emulation.test', 'id'],
['list_table_no_schema_emulation.test', 'parent_id'],
],
array_map(
static fn (array $row) => [$row['table_name'], $row['name']],
$customSqliteSchemaManager->selectTableColumnsWithSchema(),
),
);

self::assertSame(
[
['list_table_no_schema_emulation.test', 'i'],
],
array_map(
static fn (array $row) => [$row['table_name'], $row['name']],
$customSqliteSchemaManager->selectIndexColumnsWithSchema(),
),
);

self::assertSame(
[
['list_table_no_schema_emulation.test', 'parent_id', 'id'],
],
array_map(
static fn (array $row) => [$row['table_name'], $row['from'], $row['to']],
$customSqliteSchemaManager->selectForeignKeyColumnsWithSchema(),
),
);
}

/**
* This test duplicates {@see parent::testCommentInTable()} with the only difference that the name of the table
* being created is quoted. It is only meant to cover the logic of parsing the SQLite CREATE TABLE statement
Expand Down