WIP: add support for partial indexes based on the Prisma syntax#2622
WIP: add support for partial indexes based on the Prisma syntax#2622lsmith77 wants to merge 1 commit intozenstackhq:devfrom
Conversation
|
Note Currently processing new changes in this PR. This may take a few minutes, please wait... ⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (7)
📝 WalkthroughWalkthroughAdds optional Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related issues
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment Tip You can disable poems in the walkthrough.Disable the |
There was a problem hiding this comment.
Actionable comments posted: 4
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
packages/sdk/src/prisma/prisma-schema-generator.ts (1)
351-380:⚠️ Potential issue | 🟠 MajorString literals passed as
wherewill produce invalid Prisma output.A user writing
@@index([id], where: "id > 0")reaches theisLiteralExprbranch (line 352) and is emitted aswhere: "id > 0". Prisma'swhereonly acceptsraw("…")or{ field: value }object literals, so a bare quoted string is a syntax error in the generated Prisma schema — even though the regression test intests/regression/test/prisma-schema-generator.test.ts(lines 6–22, 43–60) currently asserts this output is produced.Two reasonable options:
- Reject
String/non-object/non-raw()values onwhereat validation time, so the user is steered toraw("…").- Auto-wrap a string literal
wherevalue withraw(...)in the generator (e.g. producewhere: raw("id > 0")) so the emitted Prisma is valid.If string-shaped
whereis intentional for ZModel ergonomics, option 2 is the least surprising; otherwise option 1 keeps things explicit.🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@packages/sdk/src/prisma/prisma-schema-generator.ts` around lines 351 - 380, The generator currently emits raw string literals for attribute args (in makeAttributeArgValue) which produces invalid Prisma for a `where` arg; change makeAttributeArgValue to accept an optional argName (e.g. add parameter argName?: string) and when isLiteralExpr(node) && node.$type === StringLiteral && argName === 'where' return a PrismaAttributeArgValue('Raw', this.exprToText(node)) or construct a raw(...) FunctionCall equivalent instead of emitting a plain String; update all callers that create attribute args (where attribute parsing site(s)) to pass the arg name so the function can auto-wrap `where` string literals into Raw (or alternatively throw a validation error there if you prefer rejection).
🧹 Nitpick comments (1)
packages/language/res/stdlib.zmodel (1)
253-253: Document the newwhereparameter and broadenraw()expression context to include@@unique.Two small follow-ups for the new
where: Any?parameter on@@unique(line 253) and@@index(line 356):
- The JSDoc above each attribute (lines 244–252 and 345–355) doesn't describe the new
whereparameter. Adding a one-liner clarifying it's used for partial indexes (PrismapartialIndexespreview) and accepts eitherraw("SQL")or an object literal would help schema authors discover it.- The
raw()helper is declared with@@@expressionContext([Index])(line 634). Now thatwhereis also valid on@@unique,raw(...)used inside@@unique(..., where: raw("..."))is technically outside the declared expression context. Consider widening that context (e.g. introduce a newUniquecontext or reuseIndex) so language-level validation stays consistent.🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@packages/language/res/stdlib.zmodel` at line 253, Update the JSDoc for the @@unique and @@index attributes to document the new where: Any? parameter with a one-line description stating it is for partial indexes (Prisma partialIndexes preview) and accepts either raw("SQL") or an object literal; then broaden the raw() expression context declaration (the raw symbol/@@@expressionContext) to include Unique (or both Index and Unique) so raw(...) used inside @@unique(..., where: raw("...")) is recognized by the language-level validation.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@packages/sdk/src/prisma/prisma-schema-generator.ts`:
- Around line 372-373: The generator currently neither adds previewFeatures =
["partialIndexes"] when index/constraint args use a where predicate nor properly
emits string literal predicates, so update generateDefaultGenerator to scan
index/unique definitions (where the code constructs @@index/@@unique and `@unique`
attributes) and if any PrismaAttributeArg with name "where" is present mark the
generator previewFeatures to include "partialIndexes"; additionally, change the
handling in exprToText/isLiteralExpr (the branch that returns
PrismaAttributeArgValue('Raw', this.exprToText(node)) and the branch emitting
bare quoted strings for literals) so that string literal `where` values are
either rejected with a clear validation error or automatically wrapped as
raw("...") (prefer wrapping to preserve compatibility), ensuring object
expressions remain emitted as before; reference the affected symbols:
generateDefaultGenerator, PrismaAttributeArgValue('Raw'), isLiteralExpr,
isObjectExpr, and exprToText.
In `@tests/e2e/orm/partial-index.e2e.test.ts`:
- Around line 1-21: This e2e test is brittle and redundant; remove this test
file (tests/e2e/orm/partial-index.e2e.test.ts) or rewrite it to use the
programmatic API: replace the execSync-based runGenerate()/npx invocation with a
call to loadSchema from `@zenstackhq/testtools` and invoke PrismaSchemaGenerator
directly (or the existing regression helper used in
tests/regression/test/prisma-schema-generator.test.ts), assert on the generated
schema string instead of reading/writing SCHEMA_FILE/GENERATED_PRISMA, and if
you must write files use a tmpdir or add cleanup in afterAll to avoid polluting
the source tree.
In `@tests/e2e/orm/schemas/partial-index/schema.zmodel`:
- Around line 1-13: The fixture is missing the generator preview feature for
partial indexes, so update the schema to include an explicit generator block
that sets previewFeatures = ["partialIndexes"] (so Prisma accepts the
model-level @@index and @@unique with where clauses); add this generator (using
the same generator name pattern produced by
PrismaSchemaGenerator.generateDefaultGenerator if you want parity) near the top
of the schema (above or below the datasource block) so the Post model's
@@index([title], where: { published: true }) and @@unique([title], where: {
published: true }) validate correctly.
In `@tests/regression/test/prisma-schema-generator.test.ts`:
- Around line 6-22: Tests currently expect the generator to emit bare quoted
string values for index `where`, which is invalid Prisma syntax; update the
PrismaSchemaGenerator so that when it encounters a string literal for an index
`where` it emits it wrapped in raw(...), e.g. `where: raw("...")`, and then
update the failing tests in
tests/regression/test/prisma-schema-generator.test.ts to assert for
`@@index([id], where: raw("id > 0"))` and `@@index([email], where: raw("email IS
NOT NULL"))` instead of the bare quoted strings; locate logic inside
PrismaSchemaGenerator.generate() that serializes index attributes and alter the
serializer for Index/@@index where-value handling to detect string nodes and
emit raw("...") accordingly.
---
Outside diff comments:
In `@packages/sdk/src/prisma/prisma-schema-generator.ts`:
- Around line 351-380: The generator currently emits raw string literals for
attribute args (in makeAttributeArgValue) which produces invalid Prisma for a
`where` arg; change makeAttributeArgValue to accept an optional argName (e.g.
add parameter argName?: string) and when isLiteralExpr(node) && node.$type ===
StringLiteral && argName === 'where' return a PrismaAttributeArgValue('Raw',
this.exprToText(node)) or construct a raw(...) FunctionCall equivalent instead
of emitting a plain String; update all callers that create attribute args (where
attribute parsing site(s)) to pass the arg name so the function can auto-wrap
`where` string literals into Raw (or alternatively throw a validation error
there if you prefer rejection).
---
Nitpick comments:
In `@packages/language/res/stdlib.zmodel`:
- Line 253: Update the JSDoc for the @@unique and @@index attributes to document
the new where: Any? parameter with a one-line description stating it is for
partial indexes (Prisma partialIndexes preview) and accepts either raw("SQL") or
an object literal; then broaden the raw() expression context declaration (the
raw symbol/@@@expressionContext) to include Unique (or both Index and Unique) so
raw(...) used inside @@unique(..., where: raw("...")) is recognized by the
language-level validation.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: 008b5d0b-0d7c-4e01-bf01-577bd8d2229a
📒 Files selected for processing (6)
packages/language/res/stdlib.zmodelpackages/sdk/src/prisma/prisma-builder.tspackages/sdk/src/prisma/prisma-schema-generator.tstests/e2e/orm/partial-index.e2e.test.tstests/e2e/orm/schemas/partial-index/schema.zmodeltests/regression/test/prisma-schema-generator.test.ts
c641029 to
57d82e2
Compare
|
@ymc9 |
| * @param where: Filters rows included in the partial unique constraint; accepts a Prisma filter object or raw("SQL"). | ||
| */ | ||
| attribute @@unique(_ fields: FieldReference[], name: String?, map: String?, length: Int?, sort: SortOrder?, clustered: Boolean?) @@@prisma | ||
| attribute @@unique(_ fields: FieldReference[], name: String?, map: String?, length: Int?, sort: SortOrder?, clustered: Boolean?, where: Any?) @@@prisma |
There was a problem hiding this comment.
I guess there is no support for union types to be able to be more strict here.
57d82e2 to
87ea867
Compare
https://www.prisma.io/docs/orm/prisma-schema/data-model/indexes#configuring-partial-indexes-with-where
fix #2621
I wasn't quite sure where to place the test.
Summary by CodeRabbit
New Features
Behavior / Validation
Tests