Skip to content

Commit a0c9416

Browse files
authored
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.
1 parent 032d902 commit a0c9416

File tree

2 files changed

+41
-5
lines changed

2 files changed

+41
-5
lines changed

src/Platforms/SqlitePlatform.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -950,11 +950,6 @@ public function getCreateIndexSQL(Index $index, $table)
950950
$name = $index->getQuotedName($this);
951951
$columns = $index->getColumns();
952952

953-
if (strpos($table, '.') !== false) {
954-
[$schema, $table] = explode('.', $table);
955-
$name = $schema . '.' . $name;
956-
}
957-
958953
if (count($columns) === 0) {
959954
throw new InvalidArgumentException(sprintf(
960955
'Incomplete or invalid index definition %s on table %s',
@@ -967,6 +962,11 @@ public function getCreateIndexSQL(Index $index, $table)
967962
return $this->getCreatePrimaryKeySQL($index, $table);
968963
}
969964

965+
if (strpos($table, '.') !== false) {
966+
[$schema, $table] = explode('.', $table, 2);
967+
$name = $schema . '.' . $name;
968+
}
969+
970970
$query = 'CREATE ' . $this->getCreateIndexSQLFlags($index) . 'INDEX ' . $name . ' ON ' . $table;
971971
$query .= ' (' . $this->getIndexFieldDeclarationListSQL($index) . ')' . $this->getPartialIndexSQL($index);
972972

tests/Platforms/SqlitePlatformTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,17 @@
66
use Doctrine\DBAL\Platforms\AbstractPlatform;
77
use Doctrine\DBAL\Platforms\SqlitePlatform;
88
use Doctrine\DBAL\Schema\Column;
9+
use Doctrine\DBAL\Schema\Index;
910
use Doctrine\DBAL\Schema\Table;
1011
use Doctrine\DBAL\Schema\TableDiff;
1112
use Doctrine\DBAL\TransactionIsolationLevel;
1213
use Doctrine\DBAL\Types\Type;
1314
use Doctrine\DBAL\Types\Types;
1415

16+
use function assert;
17+
use function implode;
18+
use function is_string;
19+
1520
/** @extends AbstractPlatformTestCase<SqlitePlatform> */
1621
class SqlitePlatformTest extends AbstractPlatformTestCase
1722
{
@@ -250,6 +255,37 @@ public function getGenerateUniqueIndexSql(): string
250255
return 'CREATE UNIQUE INDEX index_name ON test (test, test2)';
251256
}
252257

258+
public function testGeneratesIndexCreationSqlWithSchema(): void
259+
{
260+
$indexDef = new Index('i', ['a', 'b']);
261+
262+
self::assertSame(
263+
'CREATE INDEX main.i ON mytable (a, b)',
264+
$this->platform->getCreateIndexSQL($indexDef, 'main.mytable'),
265+
);
266+
}
267+
268+
public function testGeneratesPrimaryIndexCreationSqlWithSchema(): void
269+
{
270+
$primaryIndexDef = new Index('i2', ['a', 'b'], false, true);
271+
272+
self::assertSame(
273+
'TEST: main.mytable, i2 - a, b',
274+
(new class () extends SqlitePlatform {
275+
/**
276+
* {@inheritDoc}
277+
*/
278+
public function getCreatePrimaryKeySQL(Index $index, $table)
279+
{
280+
assert(is_string($table));
281+
282+
return 'TEST: ' . $table . ', ' . $index->getName()
283+
. ' - ' . implode(', ', $index->getColumns());
284+
}
285+
})->getCreateIndexSQL($primaryIndexDef, 'main.mytable'),
286+
);
287+
}
288+
253289
public function testGeneratesForeignKeyCreationSql(): void
254290
{
255291
$this->expectException(Exception::class);

0 commit comments

Comments
 (0)