fix(sqlite-kit): don't crash introspection on a unique index mixing an expression with a column - #6171
Open
webdevsamran wants to merge 1 commit into
Open
Conversation
…n expression with a column A multi-column UNIQUE index whose keys mix an expression with a plain column (e.g. CREATE UNIQUE INDEX idx ON users (type, lower(email))) made fromDatabase throw 'unexpected unique index ... with expression value', which takes down the whole drizzle-kit pull/Studio session for the database. Such an index cannot be promoted to a unique constraint (constraints reference plain columns only), but it is still correctly introspected as an index. Skip the constraint promotion instead of throwing. Fixes drizzle-team#6159
|
You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard. |
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.
Summary
drizzle-kit pull(and therefore Drizzle Studio) currently crashes withwhen the database contains a multi-column UNIQUE index whose keys mix an expression with a plain column — for example the case-insensitive-unique-email pattern from the official guide with an extra column:
With such an index present,
drizzle-kit pulland Studio fail for the whole database — no table is browsable. A non-unique index with identical keys is handled fine, which makes the crash easy to hit by accident.Root cause
In
fromDatabase, SQLite reports expression index keys frompragma_index_infoascid = -2. The unique-constraint promotion loop treats any multi-column unique index containing such a key as unrecoverable and throws, aborting the entire introspection.A unique index mixing expressions with columns can never be represented as a drizzle unique constraint (constraints reference plain columns only), but it is already fully introspected as an index (
entityType: 'indexes',isUnique: true). So this change skips the constraint promotion instead of throwing.Changes
drizzle-kit/src/dialects/sqlite/introspect.ts: replace thethrowwith a skip (continue) so introspection completes; the index remains available in the introspected schema.drizzle-kit/tests/sqlite/pull.test.ts: regression test that creates the exact schema above and asserts the index survives withisUnique: true, no constraint is emitted, and nothing throws.Testing evidence
Reproduced against current
rc5HEAD before the fix using an in-memory SQLite database driven through the same query/run/batch interface the test harness uses:After the fix, the same script completes successfully:
Note: I could not execute the vitest sqlite suite locally on this Windows machine because
better-sqlite3@11.9.1has no prebuilt binding for Node 24 and no MSVC toolchain is available to compile it (a local environment limitation unrelated to this change). The added regression test follows the existingpull.test.tspattern exactly and is expected to run green in CI.Fixes #6159