Skip to content

Reused existing relation name to produce opposite relation while expanding polymorphic relations#1572

Merged
ymc9 merged 39 commits intozenstackhq:devfrom
irvinzz:reuse-existing-relation-name-in-polimorphic
Jul 12, 2024
Merged

Reused existing relation name to produce opposite relation while expanding polymorphic relations#1572
ymc9 merged 39 commits intozenstackhq:devfrom
irvinzz:reuse-existing-relation-name-in-polimorphic

Conversation

@irvinzz
Copy link
Contributor

@irvinzz irvinzz commented Jul 10, 2024

Issue

expandPolymorphicRelations function always use generated relation name even it already exists.

Description

Named foreign key relation breaks model generation when it points to delegate type

Reproduce

Opposite relations delegate_aux_UserAssets_videoStream_Movie <=> userVideo and delegate_aux_UserAssets_subtitlesAsset_Movie <=> userSubtitles has different relation names.

zmodel source

generator client {
  provider = "prisma-client-js"
}

datasource db {
  provider = "cockroachdb"
  url      = env("URL")
}

model UserAssets {
  id       String @id @default(cuid())

  videoId         String
  videoStream     Asset @relation("userVideo", fields: [videoId], references: [id])
  subtitleId      String
  subtitlesAsset  Asset @relation("userSubtitles", fields: [subtitleId], references: [id])
}

model Asset {
  id              String @id @default(cuid())
  type            String

  userVideo       UserAssets[] @relation("userVideo")
  userSubtitles   UserAssets[] @relation("userSubtitles")

  @@delegate(type)
}

model Movie extends Asset {
  duration    Int
}

Logical prisma schema

//////////////////////////////////////////////////////////////////////////////////////////////
// DO NOT MODIFY THIS FILE                                                                  //
// This file is automatically generated by ZenStack CLI and should not be manually updated. //
//////////////////////////////////////////////////////////////////////////////////////////////

datasource db {
    provider = "cockroachdb"
    url = env("URL")
}

generator client {
    provider = "prisma-client-js"
    output = "../node_modules/.zenstack/.logical-prisma-client"
}

model UserAssets {
    id String @id() @default(cuid())
    videoId String
    videoStream Asset @relation("userVideo", fields: [videoId], references: [id])
    subtitleId String
    subtitlesAsset Asset @relation("userSubtitles", fields: [subtitleId], references: [id])
    delegate_aux_UserAssets_videoStream_Movie Movie @relation("delegate_aux_UserAssets_videoStream_Movie", fields: [delegate_aux_UserAssets_videoId_Movie], references: [id], map: "delegate_aux_UserAssets_videoStream_Movie_fk")
    delegate_aux_UserAssets_videoId_Movie String
    delegate_aux_UserAssets_subtitlesAsset_Movie Movie @relation("delegate_aux_UserAssets_subtitlesAsset_Movie", fields: [delegate_aux_UserAssets_subtitleId_Movie], references: [id], map: "delegate_aux_UserAssets_subtitlesAsset_Movie_fk")
    delegate_aux_UserAssets_subtitleId_Movie String
}

/// @@delegate(type)
model Asset {
    id String @id() @default(cuid())
    type String
    userVideo UserAssets[] @relation("userVideo")
    userSubtitles UserAssets[] @relation("userSubtitles")
}

model Movie {
    id String @id() @default(cuid())
    type String
    userVideo UserAssets[] @relation("userVideo")
    userSubtitles UserAssets[] @relation("userSubtitles")
    duration Int
}

ymc9 added 30 commits January 31, 2024 13:35
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jul 10, 2024

Walkthrough

Walkthrough

The recent changes introduce a new nameArg to the PrismaSchemaGenerator class, allowing the extraction of the 'name' attribute from a relation for setting relation names in PrismaFieldAttribute. Additionally, a new test file issue-1575.test.ts was added to verify the functionality and ensure the proper handling of specific schema definitions and relationships.

Changes

Files/Paths Change Summary
packages/.../prisma/schema-generator.ts Added extraction of the 'name' attribute in PrismaSchemaGenerator class for setting relation names.
tests/regression/tests/issue-1575.test.ts Introduced a test case for issue 1575 with schema models and relationships to validate the changes.

Sequence Diagram(s)

sequenceDiagram
    participant Developer
    participant PrismaSchemaGenerator
    participant Codebase

    Developer->>PrismaSchemaGenerator: Add new `nameArg` to extract 'name'
    PrismaSchemaGenerator->>Codebase: Use `nameArg` to set relation names
    Note right of Codebase: Relations have correct names

    Developer->>PrismaSchemaGenerator: Run tests in `issue-1575.test.ts`
    PrismaSchemaGenerator->>Codebase: Validate schema models and relations
    Codebase-->>Developer: Test results (success/failure)
Loading

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: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between b06cee6 and 611a432.

Files selected for processing (1)
  • packages/schema/src/plugins/prisma/schema-generator.ts (2 hunks)
Additional comments not posted (2)
packages/schema/src/plugins/prisma/schema-generator.ts (2)

413-413: Introduce nameArg to extract the 'name' attribute from relAttr.

The introduction of nameArg is to extract the 'name' attribute from a relation attribute (relAttr). This is a good practice to ensure that existing relation names are reused if available. This change is consistent with the PR objective to maintain consistency and avoid breaking model generation.


443-443: Ensure nameArg is used correctly in the relation attribute.

The usage of nameArg?.value || auxRelationField.name ensures that the existing relation name is used if available, otherwise, it falls back to the auxiliary relation field name. This aligns with the goal of reusing existing relation names and maintaining consistency.

@ymc9 ymc9 changed the base branch from main to dev July 12, 2024 00:35
@gitguardian
Copy link

gitguardian bot commented Jul 12, 2024

⚠️ GitGuardian has uncovered 5 secrets following the scan of your pull request.

Please consider investigating the findings and remediating the incidents. Failure to do so may lead to compromising the associated services or software components.

Since your pull request originates from a forked repository, GitGuardian is not able to associate the secrets uncovered with secret incidents on your GitGuardian dashboard.
Skipping this check run and merging your pull request will create secret incidents on your GitGuardian dashboard.

🔎 Detected hardcoded secrets in your pull request
GitGuardian id GitGuardian status Secret Commit Filename
5032426 Triggered Generic Password d0eaf25 tests/integration/tests/enhancements/with-policy/field-validation.test.ts View secret
5032426 Triggered Generic Password d0eaf25 tests/integration/tests/enhancements/with-policy/field-validation.test.ts View secret
5032426 Triggered Generic Password 0dfc81f tests/integration/tests/enhancements/with-policy/field-validation.test.ts View secret
5032426 Triggered Generic Password 0dfc81f tests/integration/tests/enhancements/with-policy/field-validation.test.ts View secret
8777145 Triggered Username Password b9c7572 packages/testtools/src/db.ts View secret
🛠 Guidelines to remediate hardcoded secrets
  1. Understand the implications of revoking this secret by investigating where it is used in your code.
  2. Replace and store your secrets safely. Learn here the best practices.
  3. Revoke and rotate these secrets.
  4. If possible, rewrite git history. Rewriting git history is not a trivial act. You might completely break other contributing developers' workflow and you risk accidentally deleting legitimate data.

To avoid such incidents in the future consider


🦉 GitGuardian detects secrets in your source code to help developers and security teams secure the modern development process. You are seeing this because you or someone else with access to this repository has authorized GitGuardian to scan your pull request.

@ymc9
Copy link
Member

ymc9 commented Jul 12, 2024

Hi @irvinzz , thanks for making this fix! I've added a test case and merged with parent branch "dev". Will merge this PR once CI passes.

@ymc9
Copy link
Member

ymc9 commented Jul 12, 2024

Fixes #1575

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: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 611a432 and bdcc027.

Files selected for processing (2)
  • packages/schema/src/plugins/prisma/schema-generator.ts (2 hunks)
  • tests/regression/tests/issue-1575.test.ts (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • packages/schema/src/plugins/prisma/schema-generator.ts
Additional comments not posted (2)
tests/regression/tests/issue-1575.test.ts (2)

1-1: Import statement looks good.

The loadSchema function is correctly imported from @zenstackhq/testtools.


2-29: Test case structure and content look good.

The describe block and it block are correctly structured, and the schema definitions align with the PR description for issue 1575.

@ymc9 ymc9 merged commit 4c6cde6 into zenstackhq:dev Jul 12, 2024
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