Skip to content

Conversation

@ymc9
Copy link
Member

@ymc9 ymc9 commented Nov 14, 2025

Summary by CodeRabbit

Release Notes

  • Improvements
    • Enums now automatically receive proper schema attributes when defined in custom database schemas, enabling seamless enum usage without manual schema configuration.
    • Enhanced validation of enum fields across custom and default database schemas.

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

coderabbitai bot commented Nov 14, 2025

Walkthrough

The Prisma schema generator is updated to optimize enum attribute iteration by computing attributes once before filtering. Additionally, when a datasource declares schemas without an explicit @@Schema attribute on an enum, a default @@Schema attribute is automatically added using the Postgres default schema name. Comprehensive test coverage validates enum behavior across custom schema scenarios.

Changes

Cohort / File(s) Summary
Enum schema generation optimization
packages/sdk/src/prisma/prisma-schema-generator.ts
Refactored attribute iteration for enums to compute all attributes once upfront and iterate over that collection. Implemented automatic injection of default @@Schema attributes on enums when datasources declare schemas, using the Postgres default schema name from container configuration.
Custom schema enum test coverage
tests/e2e/orm/client-api/pg-custom-schema.test.ts
Added comprehensive test scenarios with a new Role enum (ADMIN, USER) integrated into Foo and Bar models. Updated test payloads to include role field assignments and validated query/schema functionality across custom schema and default schema configurations.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

  • The core schema generator logic change involves attribute computation and default schema injection—review the enum attribute filtering pattern and verify @@Schema injection logic handles edge cases correctly
  • Test file contains multiple synchronized updates across test blocks that follow a consistent pattern, but verify enum field additions and assertions align with the schema generation changes
  • Ensure the automatic @@Schema addition doesn't conflict with existing explicit @@Schema attributes on enums

Possibly related PRs

Poem

🐰 Enums leap through schemas bright,
Attributes gathered, looping right,
Defaults injected, none left behind,
Custom schemas of every kind!
Postgres whispers its secret name,
And auto-schemas stake their claim.

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 clearly identifies the main fix: enums not properly using the default Postgres schema, which aligns with the PR's primary change of automatically adding @@Schema attributes to enums when datasources declare schemas.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix/enum-schema

📜 Recent 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 aa34372 and f43d54a.

📒 Files selected for processing (2)
  • packages/sdk/src/prisma/prisma-schema-generator.ts (1 hunks)
  • tests/e2e/orm/client-api/pg-custom-schema.test.ts (8 hunks)
⏰ 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)
  • GitHub Check: Agent
  • GitHub Check: build-test (20.x, postgresql)
  • GitHub Check: build-test (20.x, sqlite)
🔇 Additional comments (6)
tests/e2e/orm/client-api/pg-custom-schema.test.ts (4)

41-49: LGTM! Enum addition validates default public schema behavior.

The Role enum without explicit @@schema correctly tests the automatic default schema assignment. The role field integration and usage with valid enum value 'ADMIN' properly validates that QB queries work with enums in the default public schema context.

Also applies to: 66-68


86-94: LGTM! Validates defaultSchema inheritance for enums.

The test effectively validates that enums without explicit @@schema correctly inherit the defaultSchema setting. The first scenario properly expects failure when the custom schema doesn't exist, while the second scenario confirms success with the public schema. The role field usage is consistent and correct.

Also applies to: 102-102, 115-123, 131-131


146-155: LGTM! Explicit enum schema assignments work correctly.

The explicit @@schema attributes on FooRole and BarRole properly validate multi-schema scenarios. Each enum is correctly placed in the same schema as its consuming model, and the SQL verification confirms schema-qualified queries are generated correctly.

Also applies to: 159-168, 187-188


263-271: LGTM! Critical test for automatic default schema assignment.

This test is particularly valuable as it validates the core fix: the Role enum without explicit @@schema in a multi-schema context correctly receives the default schema ('somedefault'), even when used by a model (Foo) in a different schema ('mySchema'). This confirms that the automatic schema assignment works correctly and that cross-schema enum references are properly supported.

Also applies to: 295-295

packages/sdk/src/prisma/prisma-schema-generator.ts (2)

405-408: LGTM! Good optimization.

Computing attributes once before iteration is more efficient and improves readability. This also enables the subsequent check at line 412 to reference the same filtered collection, ensuring consistency.


410-421: Implementation correctly mirrors model schema handling with proper test coverage.

This fix consistently applies default Postgres schema to enums just as it does for models. The implementation correctly checks for datasourceHasSchemasSetting and applies the default schema from defaultSchema (or 'public') only when no explicit @@schema attribute exists. The test case at lines 250-300 in pg-custom-schema.test.ts exercises this exact scenario and validates correct schema assignment through SQL query execution.


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 Nov 14, 2025

Claude finished @ymc9's task —— View job


Code Review: Fix enum schema handling for PostgreSQL

Review Checklist

  • Analyze the main fix in prisma-schema-generator.ts
  • Review test coverage and changes
  • Check for code quality and best practices
  • Identify potential bugs or issues
  • Evaluate performance considerations
  • Assess security concerns

Summary

This PR fixes a bug where enum declarations weren't properly inheriting the default PostgreSQL schema when no explicit @@schema attribute was defined. The fix ensures consistency between model and enum schema handling.

Code Quality & Best Practices ✅

Main Fix (prisma-schema-generator.ts:410-421)

  • Good: The fix mirrors the existing logic for models (lines 193-204), maintaining consistency
  • Good: Proper null-safe attribute checking with attr.decl.ref?.name
  • Good: Reuses existing helper methods datasourceHasSchemasSetting() and getDefaultPostgresSchemaName()
  • Good: Clean separation of concerns - schema logic is isolated

Bug Analysis ✅

Root Cause: Enums were not getting default schema attributes applied automatically, unlike models, causing schema mismatches in multi-schema PostgreSQL setups.

Fix Validation: The solution correctly:

  1. Checks if datasource has schemas setting
  2. Verifies no explicit @@schema attribute exists
  3. Adds default schema using the same pattern as models

Test Coverage ✅

Comprehensive Test Updates:

  • Positive: All test cases now include enum usage to verify the fix
  • Positive: Tests cover both explicit schema assignment and default schema inheritance
  • Positive: Edge cases like mixed schemas (FooRole in 'mySchema', BarRole in 'public') are tested
  • Good: Tests verify actual SQL generation includes correct schema prefixes

Performance Considerations ✅

  • Low Impact: The fix adds minimal overhead - only executes during schema generation
  • Efficient: Reuses existing attribute filtering and schema name resolution
  • No Runtime Cost: This is compile-time schema generation, no runtime performance impact

Security Concerns ✅

  • No Issues: The fix doesn't introduce any security vulnerabilities
  • Safe: Uses the same schema validation and sanitization as existing model logic
  • Consistent: Maintains the same security posture as the existing codebase

Minor Observations

  1. Code Duplication: The schema default logic is now duplicated between models and enums. Consider extracting this into a shared helper method for future maintainability.

  2. Test Consistency: The test updates appropriately exercise the enum schema behavior, but the enum additions feel somewhat artificial (adding role fields just to test enums). This is acceptable for testing purposes.

Recommendation: APPROVE

This is a solid bug fix that:

  • Addresses a real consistency issue in PostgreSQL schema handling
  • Follows existing patterns and maintains code consistency
  • Includes comprehensive test coverage
  • Has minimal performance impact and no security concerns

The implementation is clean, well-tested, and follows the established codebase patterns.

Copilot finished reviewing on behalf of ymc9 November 14, 2025 04:15
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 fixes a bug where enum types weren't properly receiving the default PostgreSQL schema when the datasource declares a schemas setting. The fix ensures that enums without an explicit @@schema attribute automatically receive the default schema (either from defaultSchema or 'public'), matching the existing behavior for models.

Key changes:

  • Added logic to automatically apply @@schema attribute to enums when schemas is defined in the datasource
  • Enhanced tests to include enum types with role fields, ensuring enum schema behavior is properly validated

Reviewed Changes

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

File Description
packages/sdk/src/prisma/prisma-schema-generator.ts Added logic in generateEnum to automatically add @@schema attribute with default schema name when datasource has schemas setting and enum lacks explicit @@schema, mirroring existing model behavior
tests/e2e/orm/client-api/pg-custom-schema.test.ts Added enum types (Role, FooRole, BarRole) and corresponding role fields to models across multiple test cases to validate that enums properly receive schema assignments

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

@ymc9 ymc9 merged commit 982a36c into dev Nov 14, 2025
11 checks passed
@ymc9 ymc9 deleted the fix/enum-schema branch November 14, 2025 04:19
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