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

SQLite partial indexes support #6643

Open
wants to merge 5 commits into
base: 4.3.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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Platforms/SQLitePlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -987,4 +987,9 @@ public function normalizeUnquotedIdentifier(string $identifier): string
{
return $identifier;
}

public function supportsPartialIndexes(): bool
{
return true;
}
}
10 changes: 10 additions & 0 deletions src/Schema/SQLiteSchemaManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,16 @@ static function (array $a, array $b): int {
$idx['primary'] = false;
$idx['non_unique'] = ! $tableIndex['unique'];

if ($tableIndex['partial'] === 1) {
$idx['where'] = $this->connection->fetchOne(
<<<'SQL'
SELECT SUBSTR(sql, INSTR(lower(sql), 'where ') + LENGTH('where '))
FROM sqlite_master WHERE type = 'index' AND name = (?)
SQL,
[$keyName],
);
}

$indexArray = $this->connection->fetchAllAssociative('SELECT * FROM PRAGMA_INDEX_INFO (?)', [$keyName]);

foreach ($indexArray as $indexColumnRow) {
Expand Down
72 changes: 72 additions & 0 deletions tests/Functional/Schema/SQLiteSchemaManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -413,4 +413,76 @@ public function testCommentInQuotedTable(): void
$table = $this->schemaManager->introspectTable('table_with_comment');
self::assertSame('This is a comment', $table->getComment());
}

public function testPartialIndexes(): void
{
$offlineTable = new Table('person');
$offlineTable->addColumn('id', Types::INTEGER);
$offlineTable->addColumn('name', Types::STRING);
$offlineTable->addColumn('email', Types::STRING);
$offlineTable->addUniqueIndex(['id', 'name'], 'simple_partial_index', ['where' => '(id IS NULL)']);

$this->dropAndCreateTable($offlineTable);

$onlineTable = $this->schemaManager->introspectTable('person');

self::assertTrue(
$this->schemaManager->createComparator()
->compareTables($offlineTable, $onlineTable)
->isEmpty(),
);
self::assertTrue($onlineTable->hasIndex('simple_partial_index'));
self::assertTrue($onlineTable->getIndex('simple_partial_index')->hasOption('where'));
self::assertSame('(id IS NULL)', $onlineTable->getIndex('simple_partial_index')->getOption('where'));
}

public function testPartialIndexWhenIndexCreatedLowercase(): void
{
$offlineTable = new Table('person');
$offlineTable->addColumn('id', Types::INTEGER);
$offlineTable->addColumn('name', Types::STRING);
$offlineTable->addColumn('email', Types::STRING);

$this->dropAndCreateTable($offlineTable);
$this->connection->executeStatement(
<<<'SQL'
CREATE UNIQUE INDEX simple_partial_index ON person (id, name) where (id IS NULL)
SQL,
);

$onlineTable = $this->schemaManager->introspectTable('person');

self::assertSame(
'simple_partial_index',
$this->schemaManager->createComparator()
->compareTables($offlineTable, $onlineTable)
->getAddedIndexes()['simple_partial_index']->getName(),
);
self::assertTrue($onlineTable->hasIndex('simple_partial_index'));
self::assertTrue($onlineTable->getIndex('simple_partial_index')->hasOption('where'));
self::assertSame('(id IS NULL)', $onlineTable->getIndex('simple_partial_index')->getOption('where'));
}

public function testPartialIndexWhenTableColumnsContainTheKeywordWhere(): void
{
$offlineTable = new Table('person');
$offlineTable->addColumn('id', Types::INTEGER);
$offlineTable->addColumn('somewhere', Types::STRING);
$offlineTable->addColumn('name', Types::STRING);
$offlineTable->addColumn('email', Types::STRING);
$offlineTable->addUniqueIndex(['id', 'somewhere'], 'simple_partial_index', ['where' => '(id IS NULL)']);

$this->dropAndCreateTable($offlineTable);

$onlineTable = $this->schemaManager->introspectTable('person');

self::assertTrue(
$this->schemaManager->createComparator()
->compareTables($offlineTable, $onlineTable)
->isEmpty(),
);
self::assertTrue($onlineTable->hasIndex('simple_partial_index'));
self::assertTrue($onlineTable->getIndex('simple_partial_index')->hasOption('where'));
self::assertSame('(id IS NULL)', $onlineTable->getIndex('simple_partial_index')->getOption('where'));
}
}