[13.x] Add support for partial indexes - #61098
Closed
ibrasho wants to merge 1 commit into
Closed
Conversation
Adds a "where" modifier to index definitions so indexes can be limited to
the rows matching a predicate:
$table->unique(['workspace_id', 'sku'])->where('sku is not null');
$table->string('sku')->unique()->where(fn ($q) => $q->whereNotNull('sku'));
The predicate accepts a raw string, an expression, or a closure over a query
builder, in which case the constraints are compiled by the query grammar and
their bindings inlined, since DDL statements may not carry any bindings.
Partial indexes are supported on PostgreSQL, SQLite and SQL Server. MySQL and
MariaDB have no equivalent, so a predicate raises an exception there rather
than being silently dropped.
A unique constraint may not be backed by a partial index on PostgreSQL, so a
predicate compiles the index directly instead of through an "add constraint"
statement. SQLite exposes no pragma for the predicate of a partial index, so
it is read back from "sqlite_master" and is now carried through the table
rebuild that emulates the unsupported "alter table" operations, which would
otherwise regenerate the index without its predicate.
Member
|
Thanks for your pull request to Laravel! Unfortunately, I'm going to delay merging this code for now. To preserve our ability to adequately maintain the framework, we need to be very careful regarding the amount of code we include. If applicable, please consider releasing your code as a package so that the community can still take advantage of your contributions! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds a
wheremodifier to index definitions so an index can be limited to the rows matching a predicate:The predicate accepts a raw string, an expression, or a closure over a query builder, in which case the constraints are compiled by the query grammar and their bindings inlined, since DDL statements may not carry any bindings.
Today this requires dropping to
DB::statement('create unique index ... where ...'), which works until the migration touches something SQLite cannot alter in place — adding a foreign key, changing a column — at which point the table is rebuilt, its indexes are regenerated from introspection, and the predicate is silently lost along with the uniqueness guarantee it was enforcing.Driver support
Partial indexes are supported on PostgreSQL, SQLite and SQL Server. MySQL and MariaDB have no equivalent, so a predicate throws a
RuntimeExceptionthere rather than being silently dropped.A unique constraint may not be backed by a partial index on PostgreSQL, so a predicate compiles the index directly instead of through an
add constraintstatement. Constraint-only options have no equivalent on a bare index, so combiningwherewithdeferrablethrows rather than dropping the deferrability.SQLite exposes no pragma for the predicate of a partial index, so it is read back from
sqlite_masterand is now carried through the table rebuild that emulates the unsupportedalter tableoperations, which would otherwise regenerate the index without its predicate.Introspection
Schema::getIndexes()now returns awherekey alongside the existing ones, holding the predicate as reported by the database (pg_get_expr(indpred, indrelid)on PostgreSQL,sys.indexes.filter_definitionon SQL Server, parsed from the index definition on SQLite) ornull. Anything implementing a custom schema processor will want to add the key.Notes for review
ColumnDefinitionthe predicate is a single attribute, so a column declared with both->unique()and->index()applies the same predicate to both indexes. Splitting it per index method would mean tracking predicates per fluent index rather than per column — happy to change this if the shared predicate is the wrong default.