Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move schema split for SQLite CREATE INDEX only #6352

Open
wants to merge 1 commit into
base: 3.8.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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