Skip to content

fix: auto-add "views" preview feature to generated Prisma schema (#2376)#2402

Merged
ymc9 merged 1 commit intodevfrom
fix/issue-2376-views-preview-feature
Feb 24, 2026
Merged

fix: auto-add "views" preview feature to generated Prisma schema (#2376)#2402
ymc9 merged 1 commit intodevfrom
fix/issue-2376-views-preview-feature

Conversation

@ymc9
Copy link
Member

@ymc9 ymc9 commented Feb 24, 2026

When a ZModel schema contains view declarations without an explicit
generator block, the generated Prisma schema now automatically includes
previewFeatures = ["views"] in the default generator, preventing the
P1012 validation error during db push and migrate.

Co-Authored-By: Claude Opus 4.6 noreply@anthropic.com

fixes #2376

Summary by CodeRabbit

  • Bug Fixes

    • Fixed Prisma schema generation to dynamically compute preview features based on actual schema requirements (extensions and views) instead of statically enabling features.
  • Tests

    • Added regression test to validate preview features are correctly included when schemas contain views.

When a ZModel schema contains `view` declarations without an explicit
generator block, the generated Prisma schema now automatically includes
`previewFeatures = ["views"]` in the default generator, preventing the
P1012 validation error during `db push` and `migrate`.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@claude
Copy link
Contributor

claude bot commented Feb 24, 2026

Claude finished @ymc9's task —— View job


PR Review Complete ✅

Reviewing PR #2376: Auto-add "views" preview feature to generated Prisma schema

Review Checklist:

  • Read and analyze the main implementation (prisma-schema-generator.ts)
  • Review the test coverage and test quality
  • Check for code quality and best practices
  • Identify potential bugs or edge cases
  • Assess performance considerations
  • Review security implications
  • Provide overall feedback and recommendations

✅ Code Quality & Best Practices

Excellent implementation overall! The code follows established patterns in the codebase:

  • Clean integration: The fix is properly integrated into the existing generateDefaultGenerator method at line 187-189 in packages/sdk/src/prisma/prisma-schema-generator.ts:177
  • Consistent style: Follows the same pattern as the existing postgresqlExtensions logic (lines 182-185)
  • Proper typing: Uses existing TypeScript types and maintains type safety

✅ Performance Considerations

No performance concerns identified:

  • The check this.zmodel.declarations.some((d) => isDataModel(d) && d.isView) is efficient and only runs once during schema generation
  • Minimal overhead - just adds a string to an array when views are detected
  • No unnecessary iterations or complex computations

✅ Security Assessment

No security implications:

  • The change only affects generated Prisma schema content
  • No user input processing or external dependencies
  • Safe string literals used for preview feature names

⚠️ Minor Issues & Suggestions

1. Test Coverage Gap
The test at tests/regression/test/issue-2376.test.ts:7 only covers the positive case. Consider adding:

it('should not include views preview feature when schema has no views', async () => {
  // Test that previewFeatures doesn't include "views" when no views exist
});

it('should handle mixed scenarios with existing generator', async () => {
  // Test interaction with explicit generators
});

2. Edge Case: Multiple Preview Features
The implementation correctly handles multiple preview features by using an array, but it would be worth verifying the order doesn't matter for Prisma.

3. Documentation Enhancement
Consider adding a brief comment explaining why this is needed:

// Auto-add "views" preview feature to prevent P1012 validation errors
if (this.zmodel.declarations.some((d) => isDataModel(d) && d.isView)) {
    previewFeatures.push('views');
}

✅ Potential Bugs Assessment

No bugs identified. The logic is sound:

  • Correctly identifies view declarations using isDataModel(d) && d.isView
  • Only adds preview feature when views are actually present
  • Properly integrates with existing preview feature logic
  • Maintains backward compatibility

🎯 Overall Assessment

This is a high-quality fix that:

  • ✅ Solves the P1012 validation error effectively
  • ✅ Maintains clean code architecture
  • ✅ Includes appropriate test coverage
  • ✅ Has no breaking changes
  • ✅ Follows established patterns

Recommendation: APPROVE 👍

The implementation is solid and ready for merge. The minor suggestions above are nice-to-haves but not blockers.


@coderabbitai
Copy link
Contributor

coderabbitai bot commented Feb 24, 2026

📝 Walkthrough

Walkthrough

The Prisma schema generator's preview features computation logic is refactored to dynamically collect features based on data source extensions and model views, replacing the previous static approach. A regression test validates the "views" preview feature is correctly added when schema views exist.

Changes

Cohort / File(s) Summary
Prisma Schema Generator
packages/sdk/src/prisma/prisma-schema-generator.ts
Modified preview features logic to dynamically aggregate features in an array: adds "postgresqlExtensions" when extensions field exists, adds "views" when any data model is marked as a view, and only sets the previewFeatures field if features were collected.
Regression Test
tests/regression/test/issue-2376.test.ts
New test file validating that PrismaSchemaGenerator includes "views" in previewFeatures when the input schema contains a view, using sqlite datasource with User, Post models and UserPostCount view.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related issues

Poem

🐰 Hop along with features bright,
Views and extensions, dynamic delight!
No more fixed paths, just rules that grow,
Where schemas contain what needs to show. 🌱

🚥 Pre-merge checks | ✅ 3
✅ 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 and specifically describes the main change: automatically adding the 'views' preview feature to generated Prisma schemas when views are present in the ZModel schema.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix/issue-2376-views-preview-feature

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.

Copy link
Contributor

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

🧹 Nitpick comments (1)
tests/regression/test/issue-2376.test.ts (1)

38-40: Tighten the assertion to target the previewFeatures array.

toContain('views') could pass if “views” appears elsewhere; a regex scoped to the previewFeatures assignment avoids false positives.

Suggested tweak
-        expect(prismaSchema).toContain('previewFeatures');
-        expect(prismaSchema).toContain('views');
+        expect(prismaSchema).toContain('previewFeatures');
+        expect(prismaSchema).toMatch(/previewFeatures\s*=\s*\[[^\]]*"views"/);
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@tests/regression/test/issue-2376.test.ts` around lines 38 - 40, The
assertions on prismaSchema are too loose—keep the check scoped to the
previewFeatures array so 'views' elsewhere won't produce false positives: update
the two expect calls around prismaSchema to use a single regex (or toMatch) that
targets the previewFeatures assignment and asserts that the array contains
'views' (match "previewFeatures" followed by an array literal that includes
'views'); reference the existing prismaSchema variable and the
previewFeatures/'views' tokens when applying this change.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@tests/regression/test/issue-2376.test.ts`:
- Around line 38-40: The assertions on prismaSchema are too loose—keep the check
scoped to the previewFeatures array so 'views' elsewhere won't produce false
positives: update the two expect calls around prismaSchema to use a single regex
(or toMatch) that targets the previewFeatures assignment and asserts that the
array contains 'views' (match "previewFeatures" followed by an array literal
that includes 'views'); reference the existing prismaSchema variable and the
previewFeatures/'views' tokens when applying this change.

ℹ️ Review info

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between f3a9850 and 3890ebf.

📒 Files selected for processing (2)
  • packages/sdk/src/prisma/prisma-schema-generator.ts
  • tests/regression/test/issue-2376.test.ts

@ymc9 ymc9 merged commit 89e3acb into dev Feb 24, 2026
9 checks passed
@ymc9 ymc9 deleted the fix/issue-2376-views-preview-feature branch February 24, 2026 03:52
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.

[bug] migrations fail when views are defined unless generator with previewFeatures = ['views'] is present in the schema (v3)

1 participant