Skip to content

fix: polymorphic logical Prisma schema has identifiers with too long names #1482

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

Merged
merged 1 commit into from
Jun 3, 2024

Conversation

ymc9
Copy link
Member

@ymc9 ymc9 commented Jun 3, 2024

Fixes #1466

  • Shorten long names with a threshold
  • Fixed issues with missing @unique attribute in generated aux fk fields

…names

- Shorten long names with a threshold
- Fixed issues with missing `@unique` attribute in generated aux fk fields
Copy link
Contributor

coderabbitai bot commented Jun 3, 2024

Walkthrough

Walkthrough

The recent changes address the issue of long relation map arguments in Prisma schemas by truncating field names and refactoring relation and constraint logic. Additionally, new tests validate schema loading and data manipulation for models with long names using PostgreSQL databases.

Changes

File Path Change Summary
packages/schema/src/plugins/prisma/schema-generator.ts Added imports for AbstractDeclaration and ReferenceExpr, removed upperCaseFirst import, introduced IDENTIFIER_NAME_MAX_LENGTH constant and shortNameMap property, and refactored logic for field name truncation, foreign key replication, unique constraint replication, and relation name generation.
tests/regression/tests/issue-1466.test.ts Introduced tests for issue 1466, added imports for createPostgresDb, dropPostgresDb, and loadSchema, included various model configurations and tests for schema validation, data operations, and assertions.

Assessment against linked issues

Objective Addressed Explanation
Handle long relation map arguments in Prisma schemas (Issue #1466)
Ensure schema validation error due to long map argument is resolved (Issue #1466)
Validate schema loading and data manipulation for models with long names (Issue #1466)

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

‼️ IMPORTANT
Auto-reply has been disabled for this repository in the CodeRabbit settings. The CodeRabbit bot will not respond to your replies unless it is explicitly tagged.

  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

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.

Actionable comments posted: 1

Outside diff range and nitpick comments (4)
packages/schema/src/plugins/prisma/schema-generator.ts (4)

Line range hint 51-51: Import Node.js built-in modules using the node: protocol to ensure compatibility and clarity.

- import fs from 'fs';
- import { writeFile } from 'fs/promises';
- import path from 'path';
+ import fs from 'node:fs';
+ import { writeFile } from 'node:fs/promises';
+ import path from 'node:path';

Also applies to: 52-52, 54-54


Line range hint 283-285: Consider using for...of loops instead of forEach for better performance and readability in scenarios where asynchronous operations are not involved.

- dataModel.fields.forEach((field) => {
+ for (const field of dataModel.fields) {

Also applies to: 288-288, 318-321, 336-373, 385-470, 401-469, 581-629


Line range hint 158-158: Use template literals for string concatenation to improve readability and maintain consistency.

- const auxName = DELEGATE_AUX_RELATION_PREFIX + '_' + this.truncate(lowerCaseFirst(concrete.name));
+ const auxName = `${DELEGATE_AUX_RELATION_PREFIX}_${this.truncate(lowerCaseFirst(concrete.name))}`;

Also applies to: 186-201, 195-195, 285-285


Line range hint 176-181: Remove unnecessary else clauses to simplify control flow and improve code readability.

- if (condition) {
-     // code
- } else {
-     return;
- }
+ if (condition) {
+     // code
+ }

Also applies to: 179-181, 562-571, 566-570, 680-682

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between a34e9e7 and 9150b28.

Files selected for processing (2)
  • packages/schema/src/plugins/prisma/schema-generator.ts (13 hunks)
  • tests/regression/tests/issue-1466.test.ts (1 hunks)
Additional context used
Biome
tests/regression/tests/issue-1466.test.ts

[error] 6-6: Unexpected any. Specify a different type.


[error] 69-69: Unexpected any. Specify a different type.

packages/schema/src/plugins/prisma/schema-generator.ts

[error] 51-51: A Node.js builtin module should be imported with the node: protocol.


[error] 52-52: A Node.js builtin module should be imported with the node: protocol.


[error] 54-54: A Node.js builtin module should be imported with the node: protocol.


[error] 158-158: Do not use template literals if interpolation and special-character handling are not needed.


[error] 176-181: This else clause can be omitted because previous branches break early.


[error] 179-181: This else clause can be omitted because previous branches break early.


[error] 186-201: Template literals are preferred over string concatenation.


[error] 191-198: This else clause can be omitted because previous branches break early.


[error] 195-195: Template literals are preferred over string concatenation.


[error] 283-285: Prefer for...of instead of forEach.


[error] 285-285: Template literals are preferred over string concatenation.


[error] 288-288: Prefer for...of instead of forEach.


[error] 318-321: Prefer for...of instead of forEach.


[error] 336-373: Prefer for...of instead of forEach.


[error] 385-470: Prefer for...of instead of forEach.


[error] 401-469: Prefer for...of instead of forEach.


[error] 562-571: This else clause can be omitted because previous branches break early.


[error] 566-570: This else clause can be omitted because previous branches break early.


[error] 581-629: Prefer for...of instead of forEach.


[error] 680-682: This else clause can be omitted because previous branches break early.

Additional comments not posted (4)
tests/regression/tests/issue-1466.test.ts (2)

1-1: Import statements for createPostgresDb, dropPostgresDb, and loadSchema are correctly added to support the new test cases.


3-236: The test suite for issue #1466 is well-structured and covers various scenarios involving models with long names. It tests the schema loading and data manipulation using Prisma with PostgreSQL databases, which aligns with the PR objectives.

Tools
Biome

[error] 6-6: Unexpected any. Specify a different type.


[error] 69-69: Unexpected any. Specify a different type.

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

86-86: The constant IDENTIFIER_NAME_MAX_LENGTH is correctly set to accommodate the maximum identifier length for databases like PostgreSQL. This change addresses the issue of relation map arguments being too long.


104-104: The shortNameMap property is a good addition for managing truncated names, enhancing the maintainability of the schema generation logic.

@ymc9 ymc9 merged commit 65443f8 into dev Jun 3, 2024
13 checks passed
@ymc9 ymc9 deleted the fix/1466 branch June 3, 2024 03:37
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.

1 participant