Skip to content

Commit

Permalink
Move schema split for SQLite CREATE INDEX only
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek committed Apr 7, 2024
1 parent e5db00e commit 20fbd75
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/Platforms/SqlitePlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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);

Expand Down
36 changes: 36 additions & 0 deletions tests/Platforms/SqlitePlatformTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<SqlitePlatform> */
class SqlitePlatformTest extends AbstractPlatformTestCase
{
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 20fbd75

Please sign in to comment.