Skip to content

Commit

Permalink
Fix unwanted SQLite schema emulation in SqliteSchemaManager
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek committed Apr 5, 2024
1 parent e5db00e commit 6245c62
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 4 deletions.
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(

Check warning on line 911 in src/Platforms/SqlitePlatform.php

View check run for this annotation

Codecov / codecov/patch

src/Platforms/SqlitePlatform.php#L911

Added line #L911 was not covered by tests
'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 @@ -704,7 +704,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;

Check warning on line 709 in src/Schema/SqliteSchemaManager.php

View check run for this annotation

Codecov / codecov/patch

src/Schema/SqliteSchemaManager.php#L707-L709

Added lines #L707 - L709 were not covered by tests
}

$sql .= ' WHERE ' . implode(' AND ', $conditions) . ' ORDER BY t.name, c.cid';
Expand All @@ -729,7 +731,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;

Check warning on line 736 in src/Schema/SqliteSchemaManager.php

View check run for this annotation

Codecov / codecov/patch

src/Schema/SqliteSchemaManager.php#L734-L736

Added lines #L734 - L736 were not covered by tests
}

$sql .= ' WHERE ' . implode(' AND ', $conditions) . ' ORDER BY t.name, i.seq';
Expand All @@ -755,7 +759,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;

Check warning on line 764 in src/Schema/SqliteSchemaManager.php

View check run for this annotation

Codecov / codecov/patch

src/Schema/SqliteSchemaManager.php#L762-L764

Added lines #L762 - L764 were not covered by tests
}

$sql .= ' WHERE ' . implode(' AND ', $conditions) . ' ORDER BY t.name, p.id DESC, p.seq';
Expand Down
56 changes: 56 additions & 0 deletions tests/Functional/Schema/SqliteSchemaManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@
use Doctrine\DBAL\Types\BlobType;
use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\Types\Types;
use ReflectionMethod;

use function array_map;
use function array_shift;
use function assert;
use function dirname;

class SqliteSchemaManagerTest extends SchemaManagerFunctionalTestCase
Expand Down Expand Up @@ -371,4 +374,57 @@ public function testShorthandInForeignKeyReferenceWithMultipleColumns(): void
$createTableTrackSql,
);
}

public function testListTableNoSchemaEmulation(): void
{
$this->dropTableIfExists('`list_table_no_schema_emulation.test`');

$ddl = <<<'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;
$dd2 = <<<'DDL'
CREATE INDEX i ON `list_table_no_schema_emulation.test` (parent_id);
DDL;

$this->connection->executeStatement($ddl);
$this->connection->executeStatement($dd2);

$databasePlatform = $this->connection->getDatabasePlatform();
assert($databasePlatform instanceof SqlitePlatform);
$databasePlatform->disableSchemaEmulation();

$schemaManager = $this->schemaManager;
$refl = new ReflectionMethod($schemaManager, 'selectTableColumns');
$refl->setAccessible(true);
$res = $refl->invoke($schemaManager, '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']], $res));

$refl = new ReflectionMethod($schemaManager, 'selectIndexColumns');
$refl->setAccessible(true);
$res = $refl->invoke($schemaManager, 'main', 'list_table_no_schema_emulation.test')
->fetchAllAssociative();

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

$refl = new ReflectionMethod($schemaManager, 'selectForeignKeyColumns');
$refl->setAccessible(true);
$res = $refl->invoke($schemaManager, 'main', 'list_table_no_schema_emulation.test')
->fetchAllAssociative();

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

0 comments on commit 6245c62

Please sign in to comment.