Skip to content

Commit 008a802

Browse files
authored
Fix SQLite temp table name must not contain dot (#6315)
| Q | A |------------- | ----------- | Type | bug | Fixed issues | #### Summary Fix invalid SQL generated when table with schema is given. As it was invalid, this PR is fully BC in sense of the exact generated SQL.
1 parent 7342bad commit 008a802

File tree

3 files changed

+52
-1
lines changed

3 files changed

+52
-1
lines changed

src/Platforms/SqlitePlatform.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1150,7 +1150,12 @@ public function getAlterTableSQL(TableDiff $diff)
11501150
$sql = [];
11511151
$tableSql = [];
11521152
if (! $this->onSchemaAlterTable($diff, $tableSql)) {
1153-
$dataTable = new Table('__temp__' . $table->getName());
1153+
$tableName = $table->getName();
1154+
if (strpos($tableName, '.') !== false) {
1155+
[, $tableName] = explode('.', $tableName, 2);
1156+
}
1157+
1158+
$dataTable = new Table('__temp__' . $tableName);
11541159

11551160
$newTable = new Table(
11561161
$table->getQuotedName($this),

tests/Functional/Schema/SqliteSchemaManagerTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,17 @@
55
use Doctrine\DBAL\Exception;
66
use Doctrine\DBAL\Platforms\AbstractPlatform;
77
use Doctrine\DBAL\Platforms\SqlitePlatform;
8+
use Doctrine\DBAL\Schema\Column;
89
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
910
use Doctrine\DBAL\Schema\Table;
11+
use Doctrine\DBAL\Schema\TableDiff;
1012
use Doctrine\DBAL\Types\BlobType;
1113
use Doctrine\DBAL\Types\Type;
1214
use Doctrine\DBAL\Types\Types;
1315

16+
use function array_keys;
1417
use function array_shift;
18+
use function assert;
1519
use function dirname;
1620

1721
class SqliteSchemaManagerTest extends SchemaManagerFunctionalTestCase
@@ -216,6 +220,28 @@ public function testNonSimpleAlterTableCreatedFromDDL(): void
216220
self::assertSame(['name'], $index->getColumns());
217221
}
218222

223+
public function testAlterTableWithSchema(): void
224+
{
225+
$databasePlatform = $this->connection->getDatabasePlatform();
226+
assert($databasePlatform instanceof SqlitePlatform);
227+
$databasePlatform->disableSchemaEmulation();
228+
229+
$this->dropTableIfExists('t');
230+
231+
$table = new Table('main.t');
232+
$table->addColumn('a', Types::INTEGER);
233+
$this->schemaManager->createTable($table);
234+
235+
self::assertSame(['a'], array_keys($this->schemaManager->listTableColumns('t')));
236+
237+
$tableDiff = new TableDiff('t');
238+
$tableDiff->fromTable = $table;
239+
$tableDiff->renamedColumns['a'] = new Column('b', Type::getType(Types::INTEGER));
240+
$this->schemaManager->alterTable($tableDiff);
241+
242+
self::assertSame(['b'], array_keys($this->schemaManager->listTableColumns('t')));
243+
}
244+
219245
public function testIntrospectMultipleAnonymousForeignKeyConstraints(): void
220246
{
221247
$this->dropTableIfExists('album');

tests/Platforms/SqlitePlatformTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -615,6 +615,26 @@ public function getAlterTableRenameColumnSQL(): array
615615
];
616616
}
617617

618+
public function testGeneratesAlterTableRenameColumnSQLWithSchema(): void
619+
{
620+
$this->platform->disableSchemaEmulation();
621+
622+
$table = new Table('main.t');
623+
$table->addColumn('a', Types::INTEGER);
624+
625+
$tableDiff = new TableDiff('t');
626+
$tableDiff->fromTable = $table;
627+
$tableDiff->renamedColumns['a'] = new Column('b', Type::getType(Types::INTEGER));
628+
629+
self::assertSame([
630+
'CREATE TEMPORARY TABLE __temp__t AS SELECT a FROM main.t',
631+
'DROP TABLE main.t',
632+
'CREATE TABLE main.t (b INTEGER NOT NULL)',
633+
'INSERT INTO main.t (b) SELECT a FROM __temp__t',
634+
'DROP TABLE __temp__t',
635+
], $this->platform->getAlterTableSQL($tableDiff));
636+
}
637+
618638
/**
619639
* {@inheritDoc}
620640
*/

0 commit comments

Comments
 (0)