Skip to content

Conversation

@ymc9
Copy link
Member

@ymc9 ymc9 commented Dec 14, 2025

Summary by CodeRabbit

  • New Features

    • Schema validation refined: validates default schema inclusion when set and requires the "public" schema when models/enums implicitly target it but it's not listed.
  • Tests

    • Added end-to-end test covering the implicit-public schema requirement and default-schema validation.

✏️ Tip: You can customize this high-level summary in your review settings.

Copilot AI review requested due to automatic review settings December 14, 2025 06:22
@coderabbitai
Copy link

coderabbitai bot commented Dec 14, 2025

Warning

Rate limit exceeded

@ymc9 has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 2 minutes and 14 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

📥 Commits

Reviewing files that changed from the base of the PR and between 94b69b7 and 5667acd.

📒 Files selected for processing (1)
  • tests/e2e/orm/client-api/pg-custom-schema.test.ts (1 hunks)

Note

Other AI code review bot(s) detected

CodeRabbit 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.

Walkthrough

Refactors datasource schema validation to compute schemas once and enforce: if a default schema is set it must be in the schemas array; if no default is set and any data model or enum lacks an explicit @@schema, then "public" must be included in the schemas array. Adds a test covering the implicit-public requirement.

Changes

Cohort / File(s) Summary
Schema validation refactor
packages/language/src/validators/datasource-validator.ts
Added isDataModel and isEnum imports; compute schemasArray once; two-branch validation: (1) if defaultSchemaValue exists — require it be in schemasArray; (2) if no defaultSchemaValue — scan ds.$container.declarations for data models/enums without @@schema and, if any, require "public" in schemasArray. Other provider/url validation paths unchanged.
Test addition
tests/e2e/orm/client-api/pg-custom-schema.test.ts
Added test "requires implicit public schema to be included in schemas" that asserts client creation fails when models/enums implicitly using the public schema exist but "public" is not present in the datasource schemas array.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

  • Review focus areas:
    • Correctness of AST checks using isDataModel / isEnum and detection of missing @@schema.
    • Edge cases: declarations in different containers, shadowed/overloaded attributes, and handling of mixed explicit/implicit schema usage.
    • Test coverage adequacy and message/assertion alignment with validator errors.

Possibly related PRs

Poem

🐇 I nibble through schema lines tonight,
If models hide their schema light,
Then public must be on the list—
Don’t let implicit paths be missed.
Hooray for checks that keep things right! ✨

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: requiring implicit 'public' schema to be declared in the 'schemas' configuration. It is specific, clear, and directly related to the primary objective of the PR.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@claude
Copy link

claude bot commented Dec 14, 2025

Claude finished @ymc9's task —— View job


PR Review: PostgreSQL Multi-Schema "public" Schema Declaration Fix

Review Tasks:

  • Read and analyze datasource-validator.ts changes
  • Review pg-custom-schema.test.ts additions
  • Check code quality and best practices
  • Identify potential bugs or issues
  • Assess performance considerations
  • Review security concerns
  • Evaluate test coverage

📋 Review Summary

This PR addresses an important edge case in PostgreSQL multi-schema support by requiring the implicit "public" schema to be declared when using the schemas configuration. The implementation is well-thought-out and properly tested.

Strengths

Code Quality & Implementation

  • Clean validation logic: The new validation in datasource-validator.ts:86-94 correctly identifies when models/enums lack explicit @@schema attributes and validates that "public" is included in the schemas array
  • Proper error messaging: Clear, actionable error messages that help developers understand the requirement
  • Good separation of concerns: The validation logic is appropriately placed in the datasource validator

Test Coverage

  • Comprehensive test case: New test 'requires implicit public schema to be included in schemas' (lines 250-279) properly validates the edge case
  • Real-world scenario: Tests use both models with and without explicit schema declarations, reflecting actual usage patterns
  • Integration testing: Tests verify the actual error message content, ensuring end-to-end validation

🎯 Technical Analysis

Implementation Details

The core fix is in datasource-validator.ts:86-88:

const hasImplicitPublicSchema = ds.$container.declarations.some(
    (d) => (isDataModel(d) || isEnum(d)) && !d.attributes.some((a) => a.decl.$refText === 'schema'),
);

This correctly identifies when:

  1. There are models or enums without explicit @@schema attributes
  2. These would implicitly use the "public" schema
  3. The "public" schema must therefore be declared in the schemas array

Edge Case Handling

The logic properly handles the distinction between:

  • Explicit default schema (lines 76-83): When defaultSchema is set, validates it's in the schemas array
  • Implicit public schema (lines 84-94): When no explicit default is set, validates "public" is included if needed

Performance Considerations

  • Efficient validation: Uses Array.some() for early termination when checking for implicit schema usage
  • Single pass validation: All schema-related validations happen in one method call
  • Minimal overhead: Validation only runs during compilation, not at runtime

🔒 Security Assessment

  • No security concerns: This is purely validation logic that improves schema configuration correctness
  • Input validation: Proper string literal validation for schema names prevents injection issues

📝 Minor Suggestions

  1. Code Comments: Consider adding a comment above line 86 explaining the implicit "public" schema behavior:

    // If no explicit defaultSchema is set, models/enums without @@schema 
    // implicitly use "public", so it must be declared in schemas array
  2. Test Readability: The test on line 272 could benefit from a comment explaining why Bar triggers the implicit public schema requirement while Foo doesn't.

🧪 Test Coverage Analysis

The test additions are thorough:

  • ✅ Tests the specific bug scenario (implicit public schema validation)
  • ✅ Verifies error message content
  • ✅ Uses realistic model configurations
  • ✅ Maintains existing test patterns and structure

📊 Overall Assessment

This is a high-quality fix that:

  • Addresses a real edge case in PostgreSQL multi-schema support
  • Implements clean, understandable validation logic
  • Includes comprehensive test coverage
  • Follows established code patterns and conventions
  • Has no apparent bugs or security issues

Recommendation: ✅ Approve - This PR is ready to merge.


Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
packages/language/src/validators/datasource-validator.ts (1)

73-94: Consider making the @@schema detection a bit more robust than $refText.

Using a.decl.$refText === 'schema' will usually work, but checking the resolved symbol name (when available) is less brittle and clearer intent. E.g. a.decl.ref?.name === 'schema' || a.decl.$refText === 'schema'.

📜 Review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 081e434 and 25d11e0.

📒 Files selected for processing (2)
  • packages/language/src/validators/datasource-validator.ts (2 hunks)
  • tests/e2e/orm/client-api/pg-custom-schema.test.ts (1 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
tests/e2e/**/*.{ts,tsx}

📄 CodeRabbit inference engine (CLAUDE.md)

E2E tests should validate real-world schema compatibility with established projects

Files:

  • tests/e2e/orm/client-api/pg-custom-schema.test.ts
🧠 Learnings (3)
📓 Common learnings
Learnt from: CR
Repo: zenstackhq/zenstack-v3 PR: 0
File: CLAUDE.md:0-0
Timestamp: 2025-11-26T01:55:04.540Z
Learning: Applies to **/*.zmodel : ZModel schema files should define database structure and policies that compile to TypeScript via `zenstack generate`
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.
📚 Learning: 2025-11-26T01:55:04.540Z
Learnt from: CR
Repo: zenstackhq/zenstack-v3 PR: 0
File: CLAUDE.md:0-0
Timestamp: 2025-11-26T01:55:04.540Z
Learning: Applies to tests/e2e/**/*.{ts,tsx} : E2E tests should validate real-world schema compatibility with established projects

Applied to files:

  • tests/e2e/orm/client-api/pg-custom-schema.test.ts
  • packages/language/src/validators/datasource-validator.ts
📚 Learning: 2025-11-26T01:55:04.540Z
Learnt from: CR
Repo: zenstackhq/zenstack-v3 PR: 0
File: CLAUDE.md:0-0
Timestamp: 2025-11-26T01:55:04.540Z
Learning: Applies to packages/zenstackhq/orm/**/*.test.{ts,tsx} : ORM package tests should include comprehensive client API tests and policy tests

Applied to files:

  • tests/e2e/orm/client-api/pg-custom-schema.test.ts
🧬 Code graph analysis (1)
packages/language/src/validators/datasource-validator.ts (2)
packages/language/src/utils.ts (1)
  • getStringLiteral (75-77)
packages/language/src/generated/ast.ts (2)
  • isDataModel (391-393)
  • isEnum (432-434)
⏰ 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: CodeQL analysis (javascript-typescript)
  • GitHub Check: build-test (20.x, sqlite)
  • GitHub Check: build-test (20.x, postgresql)
🔇 Additional comments (1)
packages/language/src/validators/datasource-validator.ts (1)

3-3: New AST type-guards import looks appropriate.

Copy link
Contributor

Copilot AI left a 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 validation to ensure that the implicitly-used "public" schema is declared in the "schemas" configuration for PostgreSQL datasources. When no explicit defaultSchema is specified and models/enums lack a @@schema attribute, they implicitly use the "public" schema, which must be included in the schemas array.

Key Changes:

  • Added validation logic to check for implicit "public" schema usage when defaultSchema is not specified
  • Added test case to verify the validation triggers when "public" is missing from the schemas array despite being implicitly used

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
packages/language/src/validators/datasource-validator.ts Added conditional logic to validate implicit "public" schema usage; imports isDataModel and isEnum for declaration filtering
tests/e2e/orm/client-api/pg-custom-schema.test.ts Added test case covering scenario where enum and model without @@Schema require "public" in schemas array

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@claude
Copy link

claude bot commented Dec 14, 2025

Claude finished @ymc9's task —— View job


PR Review: PostgreSQL Multi-Schema "public" Schema Declaration Fix

Review Tasks:

  • Read and analyze datasource-validator.ts changes
  • Review pg-custom-schema.test.ts additions
  • Check code quality and best practices
  • Identify potential bugs or issues
  • Assess performance considerations
  • Review security concerns
  • Evaluate test coverage

📋 Review Summary

This PR addresses an important edge case in PostgreSQL multi-schema support by requiring the implicit "public" schema to be declared when using the schemas configuration. The implementation is well-thought-out and properly tested.

Strengths

Code Quality & Implementation

  • Clean validation logic: The new validation in datasource-validator.ts:86-94 correctly identifies when models/enums lack explicit @@schema attributes and validates that "public" is included in the schemas array
  • Proper error messaging: Clear, actionable error messages that help developers understand the requirement
  • Good separation of concerns: The validation logic is appropriately placed in the datasource validator

Test Coverage

  • Comprehensive test case: New test 'requires implicit public schema to be included in schemas' (lines 250-279) properly validates the edge case
  • Real-world scenario: Tests use both models with and without explicit schema declarations, reflecting actual usage patterns
  • Integration testing: Tests verify the actual error message content, ensuring end-to-end validation

🎯 Technical Analysis

Implementation Details

The core fix is in datasource-validator.ts:86-88:

const hasImplicitPublicSchema = ds.$container.declarations.some(
    (d) => (isDataModel(d) || isEnum(d)) && !d.attributes.some((a) => a.decl.$refText === '@@schema'),
);

This correctly identifies when:

  1. There are models or enums without explicit @@schema attributes
  2. These would implicitly use the "public" schema
  3. The "public" schema must therefore be declared in the schemas array

Edge Case Handling

The logic properly handles the distinction between:

  • Explicit default schema (lines 76-83): When defaultSchema is set, validates it's in the schemas array
  • Implicit public schema (lines 84-94): When no explicit default is set, validates "public" is included if needed

Performance Considerations

  • Efficient validation: Uses Array.some() for early termination when checking for implicit schema usage
  • Single pass validation: All schema-related validations happen in one method call
  • Minimal overhead: Validation only runs during compilation, not at runtime

🔒 Security Assessment

  • No security concerns: This is purely validation logic that improves schema configuration correctness
  • Input validation: Proper string literal validation for schema names prevents injection issues

📝 Minor Suggestions

  1. Code Comments: Consider adding a comment above line 86 explaining the implicit "public" schema behavior:

    // If no explicit defaultSchema is set, models/enums without @@schema 
    // implicitly use "public", so it must be declared in schemas array
  2. Test Readability: The test on line 272 could benefit from a comment explaining why Bar triggers the implicit public schema requirement while Foo doesn't.

🧪 Test Coverage Analysis

The test additions are thorough:

  • ✅ Tests the specific bug scenario (implicit public schema validation)
  • ✅ Verifies error message content
  • ✅ Uses realistic model configurations
  • ✅ Maintains existing test patterns and structure

📊 Overall Assessment

This is a high-quality fix that:

  • Addresses a real edge case in PostgreSQL multi-schema support
  • Implements clean, understandable validation logic
  • Includes comprehensive test coverage
  • Follows established code patterns and conventions
  • Has no apparent bugs or security issues

Recommendation: ✅ Approve - This PR is ready to merge.


Copy link
Contributor

Copilot AI commented Dec 14, 2025

@ymc9 I've opened a new pull request, #496, to work on those changes. Once the pull request is ready, I'll request review from you.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (1)
packages/language/src/validators/datasource-validator.ts (1)

83-94: LGTM! Implicit public schema detection is implemented correctly.

The logic properly detects when models or enums lack an explicit @@schema attribute and correctly requires "public" to be in the schemas array, since it will be implicitly used. The attribute check on line 87 correctly uses '@@schema' with the @@ prefix for model-level attributes.

Consider making the error message more descriptive to help users understand why:

-                        accept('error', `"public" must be included in the "schemas" array`, {
+                        accept('error', `"public" must be included in the "schemas" array because it is implicitly used by models or enums without an explicit @@schema attribute`, {
📜 Review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 25d11e0 and 94b69b7.

📒 Files selected for processing (1)
  • packages/language/src/validators/datasource-validator.ts (2 hunks)
🧰 Additional context used
🧠 Learnings (4)
📓 Common learnings
Learnt from: CR
Repo: zenstackhq/zenstack-v3 PR: 0
File: CLAUDE.md:0-0
Timestamp: 2025-11-26T01:55:04.540Z
Learning: Applies to **/*.zmodel : ZModel schema files should define database structure and policies that compile to TypeScript via `zenstack generate`
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.
Learnt from: CR
Repo: zenstackhq/zenstack-v3 PR: 0
File: CLAUDE.md:0-0
Timestamp: 2025-11-26T01:55:04.540Z
Learning: Applies to tests/e2e/**/*.{ts,tsx} : E2E tests should validate real-world schema compatibility with established projects
📚 Learning: 2025-11-26T01:55:04.540Z
Learnt from: CR
Repo: zenstackhq/zenstack-v3 PR: 0
File: CLAUDE.md:0-0
Timestamp: 2025-11-26T01:55:04.540Z
Learning: Applies to tests/e2e/**/*.{ts,tsx} : E2E tests should validate real-world schema compatibility with established projects

Applied to files:

  • packages/language/src/validators/datasource-validator.ts
📚 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/language/src/validators/datasource-validator.ts
📚 Learning: 2025-11-26T01:55:04.540Z
Learnt from: CR
Repo: zenstackhq/zenstack-v3 PR: 0
File: CLAUDE.md:0-0
Timestamp: 2025-11-26T01:55:04.540Z
Learning: Applies to **/*.zmodel : ZModel schema files should define database structure and policies that compile to TypeScript via `zenstack generate`

Applied to files:

  • packages/language/src/validators/datasource-validator.ts
🧬 Code graph analysis (1)
packages/language/src/validators/datasource-validator.ts (2)
packages/language/src/utils.ts (1)
  • getStringLiteral (75-77)
packages/language/src/generated/ast.ts (2)
  • isDataModel (391-393)
  • isEnum (432-434)
⏰ 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). (2)
  • GitHub Check: build-test (20.x, postgresql)
  • GitHub Check: build-test (20.x, sqlite)
🔇 Additional comments (3)
packages/language/src/validators/datasource-validator.ts (3)

3-3: LGTM! Import additions support the new validation.

The isDataModel and isEnum type guards are correctly imported from the generated AST and are necessary for the implicit public schema detection logic.


73-75: LGTM! Efficient extraction of schemas array.

Extracting schemasArray once and reusing it in both validation branches is good for performance and maintainability. The non-null assertion is safe given the prior validation at line 68.


76-82: LGTM! DefaultSchema validation is correct.

The logic properly validates that when defaultSchema is explicitly set, it must be included in the schemas array. The error message is clear and actionable.

…it @@Schema (#496)

* Initial plan

* Add test for explicit schema usage without public

Co-authored-by: ymc9 <[email protected]>

---------

Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Co-authored-by: ymc9 <[email protected]>
@claude
Copy link

claude bot commented Dec 14, 2025

Claude finished @ymc9's task —— View job


PR Review: PostgreSQL Multi-Schema "public" Schema Declaration Fix

Review Tasks:

  • Read and analyze datasource-validator.ts changes
  • Review pg-custom-schema.test.ts additions
  • Check code quality and best practices
  • Identify potential bugs or issues
  • Assess performance considerations
  • Review security concerns
  • Evaluate test coverage

📋 Review Summary

This PR addresses an important edge case in PostgreSQL multi-schema support by requiring the implicit "public" schema to be declared when using the schemas configuration. The implementation is well-thought-out and properly tested.

Strengths

Code Quality & Implementation

  • Clean validation logic: The new validation in datasource-validator.ts:86-94 correctly identifies when models/enums lack explicit @@schema attributes and validates that "public" is included in the schemas array
  • Proper error messaging: Clear, actionable error messages that help developers understand the requirement
  • Good separation of concerns: The validation logic is appropriately placed in the datasource validator

Test Coverage

  • Comprehensive test case: New test 'requires implicit public schema to be included in schemas' (lines 250-279) properly validates the edge case
  • Real-world scenario: Tests use both models with and without explicit schema declarations, reflecting actual usage patterns
  • Integration testing: Tests verify the actual error message content, ensuring end-to-end validation

🎯 Technical Analysis

Implementation Details

The core fix is in datasource-validator.ts:86-88:

const hasImplicitPublicSchema = ds.$container.declarations.some(
    (d) => (isDataModel(d) || isEnum(d)) && !d.attributes.some((a) => a.decl.$refText === '@@schema'),
);

This correctly identifies when:

  1. There are models or enums without explicit @@schema attributes
  2. These would implicitly use the "public" schema
  3. The "public" schema must therefore be declared in the schemas array

Edge Case Handling

The logic properly handles the distinction between:

  • Explicit default schema (lines 76-83): When defaultSchema is set, validates it's in the schemas array
  • Implicit public schema (lines 84-94): When no explicit default is set, validates "public" is included if needed

Performance Considerations

  • Efficient validation: Uses Array.some() for early termination when checking for implicit schema usage
  • Single pass validation: All schema-related validations happen in one method call
  • Minimal overhead: Validation only runs during compilation, not at runtime

🔒 Security Assessment

  • No security concerns: This is purely validation logic that improves schema configuration correctness
  • Input validation: Proper string literal validation for schema names prevents injection issues

📝 Minor Suggestions

  1. Code Comments: Consider adding a comment above line 86 explaining the implicit "public" schema behavior:

    // If no explicit defaultSchema is set, models/enums without @@schema 
    // implicitly use "public", so it must be declared in schemas array
  2. Test Readability: The test on line 272 could benefit from a comment explaining why Bar triggers the implicit public schema requirement while Foo doesn't.

🧪 Test Coverage Analysis

The test additions are thorough:

  • ✅ Tests the specific bug scenario (implicit public schema validation)
  • ✅ Verifies error message content
  • ✅ Uses realistic model configurations
  • ✅ Maintains existing test patterns and structure

📊 Overall Assessment

This is a high-quality fix that:

  • Addresses a real edge case in PostgreSQL multi-schema support
  • Implements clean, understandable validation logic
  • Includes comprehensive test coverage
  • Follows established code patterns and conventions
  • Has no apparent bugs or security issues

Recommendation: ✅ Approve - This PR is ready to merge.


@ymc9 ymc9 merged commit 0d74a02 into dev Dec 14, 2025
5 checks passed
@ymc9 ymc9 deleted the fix/pg-multi-schema-implicit-public branch December 14, 2025 07:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants