-
-
Notifications
You must be signed in to change notification settings - Fork 12
feat: postgres custom schema #385
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
Conversation
|
Note Other AI code review bot(s) detectedCodeRabbit has detected other AI code review bot(s) in this pull request and will avoid duplicating their findings in the review comments. This may lead to a less comprehensive review. WalkthroughAdds PostgreSQL custom schema support: activates @@Schema docs, validates schema names and datasource schema fields, propagates schema names through ORM name mapping and executor initialization, extends schema/provider types and generators, centralizes test schema placeholder handling, and adds end-to-end PostgreSQL schema tests. Changes
Estimated code review effort🎯 4 (Complex) | ⏱️ ~60 minutes
Possibly related PRs
Poem
Pre-merge checks and finishing touches✅ Passed checks (3 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
📜 Recent review detailsConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (3)
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 |
|
Claude finished @ymc9's task —— View job PostgreSQL Custom Schema Support - PR ReviewI've completed a comprehensive review of this PR, which adds PostgreSQL custom schema support to ZenStack. Here's my detailed analysis: Summary of ChangesThis PR introduces comprehensive PostgreSQL custom schema support through:
✅ Strengths1. Comprehensive Implementation
2. Robust Validation Logic (
|
|
fixes #346 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 2
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
packages/testtools/src/client.ts (1)
82-88: Preserve provider metadata for object schemas.When callers pass a pre-built
Schema, this branch now overwritesschema.providerwith{ type }, discarding new fields likedefaultSchema. That breaks PostgreSQL schema tests using the object form.Keep the existing provider fields:
_schema = { ...schema, provider: { - type: provider, + ...schema.provider, + type: provider, }, };
📜 Review details
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (10)
packages/language/res/stdlib.zmodel(1 hunks)packages/language/src/validators/attribute-application-validator.ts(3 hunks)packages/language/src/validators/datasource-validator.ts(2 hunks)packages/orm/src/client/executor/name-mapper.ts(5 hunks)packages/orm/src/client/executor/zenstack-query-executor.ts(1 hunks)packages/schema/src/schema.ts(1 hunks)packages/sdk/src/ts-schema-generator.ts(2 hunks)packages/testtools/src/client.ts(2 hunks)packages/testtools/src/schema.ts(2 hunks)tests/e2e/orm/client-api/pg-custom-schema.test.ts(1 hunks)
🧰 Additional context used
🧠 Learnings (1)
📚 Learning: 2025-10-21T16:09:31.218Z
Learnt from: ymc9
Repo: zenstackhq/zenstack-v3 PR: 319
File: packages/runtime/src/client/executor/zenstack-query-executor.ts:63-72
Timestamp: 2025-10-21T16:09:31.218Z
Learning: In ZenStack, TypeDefs can be inherited by models. When a TypeDef contains fields with `map` attributes, those mapped field names need to be processed by the QueryNameMapper since they become part of the inheriting model's schema. Therefore, when checking if a schema has mapped names (e.g., in `schemaHasMappedNames`), both `schema.models` and `schema.typeDefs` must be inspected for `@map` and `map` attributes.
Applied to files:
packages/orm/src/client/executor/zenstack-query-executor.tspackages/orm/src/client/executor/name-mapper.ts
🧬 Code graph analysis (6)
packages/sdk/src/ts-schema-generator.ts (2)
packages/common-helpers/src/tiny-invariant.ts (1)
invariant(4-14)packages/language/src/generated/ast.ts (4)
isLiteralExpr(133-135)Model(559-563)Model(565-565)isDataSource(417-419)
tests/e2e/orm/client-api/pg-custom-schema.test.ts (1)
packages/testtools/src/client.ts (1)
createTestClient(53-182)
packages/orm/src/client/executor/zenstack-query-executor.ts (1)
packages/schema/src/schema.ts (1)
SchemaDef(11-19)
packages/language/src/validators/datasource-validator.ts (3)
packages/language/src/utils.ts (1)
getStringLiteral(75-77)packages/language/src/constants.ts (1)
SUPPORTED_PROVIDERS(4-11)packages/language/src/generated/ast.ts (2)
isConfigArrayExpr(290-292)isLiteralExpr(133-135)
packages/testtools/src/client.ts (2)
packages/testtools/src/schema.ts (1)
generateTsSchema(40-74)tests/e2e/orm/schemas/basic/schema.ts (1)
schema(9-284)
packages/language/src/validators/attribute-application-validator.ts (4)
packages/schema/src/schema.ts (1)
AttributeApplication(40-43)packages/language/src/utils.ts (1)
getStringLiteral(75-77)packages/common-helpers/src/tiny-invariant.ts (1)
invariant(4-14)packages/language/src/generated/ast.ts (4)
isModel(567-569)isDataSource(417-419)isConfigArrayExpr(290-292)isLiteralExpr(133-135)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (4)
- GitHub Check: Agent
- GitHub Check: build-test (20.x, sqlite)
- GitHub Check: claude-review
- GitHub Check: build-test (20.x, postgresql)
🔇 Additional comments (12)
packages/testtools/src/schema.ts (1)
35-54: Helper centralizes placeholder handling cleanly.Thanks for extracting the placeholder replacement into
replacePlaceholdersand reusing it ingenerateTsSchema; the schema setup logic is clearer and easier to reuse.packages/schema/src/schema.ts (1)
6-9: Provider type is ready for default schemas.Adding
defaultSchema?: stringon the provider surfaces the new capability to the rest of the stack without breaking existing callers. 👍packages/language/res/stdlib.zmodel (1)
457-463: Docstring clarifies PostgreSQL-only usage.Appreciate the explicit documentation of
@@schema; it matches the new validation semantics.packages/sdk/src/ts-schema-generator.ts (3)
237-255: LGTM! Clean implementation of defaultSchema support.The conditional inclusion of
defaultSchemain the provider object is handled correctly using the spread operator pattern. The implementation properly extracts the value and only includes it when present.
631-641: LGTM! Enhanced validation for provider field.The stricter validation using
invariantensures the provider is always a string literal, which aligns with the validation rules in datasource-validator.ts. The change to return a string directly (rather than an object) simplifies the API and is consistent with how the value is used increateProviderObject.
643-656: LGTM! Consistent implementation following established patterns.The new
getDataSourceDefaultSchemamethod mirrors the structure ofgetDataSourceProvider, maintaining consistency in the codebase. The validation ensuresdefaultSchemais a string literal when present, and correctly returnsundefinedwhen the field is absent.packages/language/src/validators/datasource-validator.ts (2)
26-39: LGTM! Enhanced provider validation with better error messages.The validation now enforces that the provider is both a string literal and within the supported providers list. The error message helpfully lists all supported providers, improving the developer experience.
41-64: LGTM! Comprehensive validation for PostgreSQL-specific schema features.The validation correctly enforces that
defaultSchemaandschemasfields are only allowed for PostgreSQL providers. Theschemasfield validation properly checks both the array structure and that all items are string literals.tests/e2e/orm/client-api/pg-custom-schema.test.ts (4)
6-35: LGTM! Comprehensive test of default public schema behavior.The test effectively verifies that ORM queries default to the public schema by inspecting the generated SQL. The flag-based verification pattern ensures all CRUD operations are covered.
70-112: LGTM! Good test of default schema behavior and error handling.The test verifies both the error case (non-existent schema) and success case (valid schema). The error message check on Line 89 depends on PostgreSQL's error format, which is acceptable for E2E tests but could be fragile across PostgreSQL versions.
114-158: LGTM! Excellent test of multi-schema functionality.This test comprehensively validates per-model schema annotations with multiple schemas defined in the datasource. The use of
usePrismaPush: trueis appropriate here to ensure the schemas are actually created in PostgreSQL before testing queries against them.
160-195: LGTM! Comprehensive validation error coverage.These tests properly verify that the validation rules are enforced:
- Schema features are rejected for non-PostgreSQL providers
- Model schema annotations must reference schemas defined in the datasource
The error message assertions using
toThrowwith substrings are appropriate for validating that the right validation errors are raised.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR adds support for PostgreSQL custom schemas in ZenStack, allowing users to specify which database schema models should be created in and set default schemas at the datasource level.
Key changes:
- Added
@@schemaattribute for models anddefaultSchema/schemasfields for datasources - Implemented schema name mapping in query execution layer
- Added validation for schema-related fields to ensure they're only used with PostgreSQL
Reviewed Changes
Copilot reviewed 10 out of 10 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| tests/e2e/orm/client-api/pg-custom-schema.test.ts | Comprehensive test coverage for custom schema functionality including defaults, QB queries, and validation |
| packages/schema/src/schema.ts | Extended DataSourceProvider type to include optional defaultSchema field |
| packages/sdk/src/ts-schema-generator.ts | Modified provider object generation to include defaultSchema in TypeScript schema output |
| packages/orm/src/client/executor/zenstack-query-executor.ts | Added check for custom PostgreSQL schemas to enable name mapping |
| packages/orm/src/client/executor/name-mapper.ts | Implemented schema name resolution and injection into SQL queries via TableNode.createWithSchema |
| packages/language/src/validators/datasource-validator.ts | Added validation for defaultSchema and schemas fields to ensure PostgreSQL-only usage |
| packages/language/src/validators/attribute-application-validator.ts | Added validation for @@schema attribute to verify schema names are declared in datasource |
| packages/testtools/src/schema.ts | Added placeholder replacement function for test schemas |
| packages/testtools/src/client.ts | Updated to preserve provider object properties when replacing provider type |
| packages/language/res/stdlib.zmodel | Uncommented and updated documentation for @@schema attribute |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
Claude finished @ymc9's task —— View job PostgreSQL Custom Schema Support - Code Review
|

Summary by CodeRabbit
New Features
Improvements
Tests
Documentation