From a0c9416b96d06032ac34e92275cee5bed3954df8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Vo=C5=99=C3=AD=C5=A1ek?= Date: Tue, 18 Jun 2024 14:12:44 +0200 Subject: [PATCH] Move schema split for SQLite CREATE INDEX only (#6352) | Q | A |------------- | ----------- | Type | bug | Fixed issues | n/a #### Summary `SqlitePlatform::getCreatePrimaryKeySQL` method must receive original table name, the split is intended for SQLite `CREATE INDEX` statement only. --- src/Platforms/SqlitePlatform.php | 10 +++---- tests/Platforms/SqlitePlatformTest.php | 36 ++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 5 deletions(-) diff --git a/src/Platforms/SqlitePlatform.php b/src/Platforms/SqlitePlatform.php index bbca4234db5..48c692fdb45 100644 --- a/src/Platforms/SqlitePlatform.php +++ b/src/Platforms/SqlitePlatform.php @@ -950,11 +950,6 @@ public function getCreateIndexSQL(Index $index, $table) $name = $index->getQuotedName($this); $columns = $index->getColumns(); - if (strpos($table, '.') !== false) { - [$schema, $table] = explode('.', $table); - $name = $schema . '.' . $name; - } - if (count($columns) === 0) { throw new InvalidArgumentException(sprintf( 'Incomplete or invalid index definition %s on table %s', @@ -967,6 +962,11 @@ public function getCreateIndexSQL(Index $index, $table) return $this->getCreatePrimaryKeySQL($index, $table); } + if (strpos($table, '.') !== false) { + [$schema, $table] = explode('.', $table, 2); + $name = $schema . '.' . $name; + } + $query = 'CREATE ' . $this->getCreateIndexSQLFlags($index) . 'INDEX ' . $name . ' ON ' . $table; $query .= ' (' . $this->getIndexFieldDeclarationListSQL($index) . ')' . $this->getPartialIndexSQL($index); diff --git a/tests/Platforms/SqlitePlatformTest.php b/tests/Platforms/SqlitePlatformTest.php index f8ae9f7f6c3..8f54cf98af0 100644 --- a/tests/Platforms/SqlitePlatformTest.php +++ b/tests/Platforms/SqlitePlatformTest.php @@ -6,12 +6,17 @@ use Doctrine\DBAL\Platforms\AbstractPlatform; use Doctrine\DBAL\Platforms\SqlitePlatform; use Doctrine\DBAL\Schema\Column; +use Doctrine\DBAL\Schema\Index; use Doctrine\DBAL\Schema\Table; use Doctrine\DBAL\Schema\TableDiff; use Doctrine\DBAL\TransactionIsolationLevel; use Doctrine\DBAL\Types\Type; use Doctrine\DBAL\Types\Types; +use function assert; +use function implode; +use function is_string; + /** @extends AbstractPlatformTestCase */ class SqlitePlatformTest extends AbstractPlatformTestCase { @@ -250,6 +255,37 @@ public function getGenerateUniqueIndexSql(): string return 'CREATE UNIQUE INDEX index_name ON test (test, test2)'; } + public function testGeneratesIndexCreationSqlWithSchema(): void + { + $indexDef = new Index('i', ['a', 'b']); + + self::assertSame( + 'CREATE INDEX main.i ON mytable (a, b)', + $this->platform->getCreateIndexSQL($indexDef, 'main.mytable'), + ); + } + + public function testGeneratesPrimaryIndexCreationSqlWithSchema(): void + { + $primaryIndexDef = new Index('i2', ['a', 'b'], false, true); + + self::assertSame( + 'TEST: main.mytable, i2 - a, b', + (new class () extends SqlitePlatform { + /** + * {@inheritDoc} + */ + public function getCreatePrimaryKeySQL(Index $index, $table) + { + assert(is_string($table)); + + return 'TEST: ' . $table . ', ' . $index->getName() + . ' - ' . implode(', ', $index->getColumns()); + } + })->getCreateIndexSQL($primaryIndexDef, 'main.mytable'), + ); + } + public function testGeneratesForeignKeyCreationSql(): void { $this->expectException(Exception::class);