Skip to content

fix(policy): properly handle array-form of upsert payload #1101

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
Mar 8, 2024
Merged

Conversation

ymc9
Copy link
Member

@ymc9 ymc9 commented Mar 8, 2024

Fixes #1080

Summary by CodeRabbit

  • Refactor

    • Improved data iteration logic for various database actions.
    • Enhanced value handling in policy management for better context integration.
  • Tests

    • Added and refined integration tests to ensure reliability in database operations and schema handling.

Copy link
Contributor

coderabbitai bot commented Mar 8, 2024

Walkthrough

Walkthrough

The recent updates primarily target enhancements and bug fixes in nested data operations within a database management context, focusing on the reverse iteration of data for various actions and refining logic in policy handling for more efficient data manipulation. Furthermore, integration tests have been adjusted to better reflect these changes, removing unnecessary configurations and improving test reliability for database operations.

Changes

Files Change Summary
.../nested-write-visitor.ts Replaced enumerate with enumerateReverse for reverse data iteration in various actions.
.../policy/handler.ts Refactored logic and introduced mergeToParent and removeFromParent methods.
.../tests/regression/issue-1078.test.ts Adjusted database operation handling and removed unnecessary loadSchema configurations.
.../tests/regression/issue-1080.test.ts Added regression test for verifying database operation correctness on nested relations.

Assessment against linked issues

Objective Addressed Explanation
Upsert operation on multiple nested relations should update all specified fields (#1080)
Ensure upsert operation's consistency and reliability on nested relations using enhanced client (#1080)
Address discrepancies in upsert operation between pure PrismaClient and enhanced Prisma client (#1080) The changes do not explicitly mention adjustments to align the enhanced client's behavior with the pure PrismaClient.
Improve handling of nested relations during data updates (#1080)

These assessments validate that the majority of the objectives related to issue #1080 have been directly addressed through the implemented changes, with the exception of explicitly aligning the behavior of the enhanced Prisma client with the pure PrismaClient. The changes made in the codebase indicate a focus on improving the logic and functionality around nested relation operations, which is in line with the objectives of the linked issue.

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:

Note: Auto-reply has been disabled for this repository by the repository owner. 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 tests 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 tests.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

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 a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @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.
  • The JSON schema for the configuration file is available here.
  • 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/coderabbit-overrides.v2.json

CodeRabbit Discord Community

Join our Discord Community to get help, request features, and share feedback.

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.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 4dd7aa0 and 9daff09.
Files selected for processing (4)
  • packages/runtime/src/cross/nested-write-visitor.ts (11 hunks)
  • packages/runtime/src/enhancements/policy/handler.ts (4 hunks)
  • tests/integration/tests/regression/issue-1078.test.ts (3 hunks)
  • tests/integration/tests/regression/issue-1080.test.ts (1 hunks)
Additional comments: 26
tests/integration/tests/regression/issue-1078.test.ts (4)
  • 2-8: > 📝 NOTE

This review was outside the diff hunks and was mapped to the diff hunk with the greatest overlap. Original lines [5-15]

The schema definition for the Counter model within the loadSchema call is well-defined and includes model-specific directives like @@validate and @@allow. This setup is crucial for testing the behavior of the Counter model under various conditions, ensuring that the model's constraints and permissions are correctly enforced.

  • 21-25: The use of expect().toResolveTruthy() for asserting the successful creation of a counter instance is a good practice. It ensures that the promise resolves successfully, indicating that the operation completed without errors. This approach enhances the readability and maintainability of the test by clearly stating the expected outcome.
  • 28-33: Similarly, the use of expect().toResolveTruthy() for asserting the successful update of a counter instance is consistent with best practices for writing clear and maintainable tests. It confirms that the update operation completes successfully, which is essential for verifying the correct behavior of the Counter model under update scenarios.
  • 44-44: The schema definition for the Post model within the loadSchema call is concise and includes model-specific directives like @allow. This setup is important for testing the behavior of the Post model, especially in terms of access control and permissions. It ensures that the model's constraints are correctly enforced and tested.
tests/integration/tests/regression/issue-1080.test.ts (6)
  • 1-23: The import of loadSchema from @zenstackhq/testtools and the setup of the Project and Field models within the loadSchema call are correctly implemented. Including the logPrismaQuery: true option is useful for debugging purposes by logging the queries made by Prisma. However, it's important to ensure that such logging is disabled or removed in production environments to avoid potential performance impacts and to keep the logs clean.
  • 28-35: Creating a project instance with nested Fields using the create operation is a critical part of the test, ensuring that nested relations can be created successfully. This operation tests the ability to handle nested creation within the enhanced Prisma client, which is central to the issue being addressed.
  • 37-62: The test case for updating a project instance with nested Fields using the upsert operation is well-structured. It verifies that the upsert operation can correctly handle multiple nested relations, addressing the core issue of the pull request. This test is essential for ensuring that the fix works as intended.
  • 64-82: The subsequent test case further validates the upsert operation on a single nested relation. It's important for ensuring that the fix not only works for multiple relations but also behaves correctly when applied to a single relation. This test complements the previous one by covering a different scenario.
  • 84-106: This test case introduces a mix of upsert and update operations on nested relations, providing a more complex scenario to verify the fix. It's crucial for testing the fix's robustness and ensuring that it handles mixed operation types correctly.
  • 108-131: The final test case in this file tests the upsert operation with a non-existent ID, combined with an update operation on an existing relation. This scenario is important for ensuring that the fix can handle edge cases, such as attempting to upsert a non-existent relation, while still correctly updating existing relations.
packages/runtime/src/cross/nested-write-visitor.ts (10)
  • 158-158: Replacing enumerate with enumerateReverse in the create action is a significant change that aims to ensure the correct order of processing nested relations. This change is likely to address the core issue of the pull request by ensuring that nested relations are processed in reverse order, which can be crucial for operations like upsert that depend on the order in which relations are processed.
  • 186-186: Similarly, the use of enumerateReverse in the connectOrCreate action aligns with the goal of processing nested relations in reverse order. This adjustment is consistent with the overall objective of the pull request and is likely to contribute to the correct handling of nested relations during upsert operations.
  • 201-201: The application of enumerateReverse to the connect action further emphasizes the importance of processing order for nested relations. This change ensures consistency across different actions, reinforcing the approach taken to address the issue at hand.
  • 213-213: The use of enumerateReverse for the disconnect action is another example of the consistent application of the reverse order processing strategy. This change is part of the broader effort to ensure that all nested relations are correctly processed, regardless of the specific action being performed.
  • 228-228: Applying enumerateReverse to the update action is crucial for ensuring that updates to nested relations are processed in the correct order. This change is directly related to the core issue being addressed and is essential for the proper handling of nested relations during upsert operations.
  • 247-247: The use of enumerateReverse in the updateMany action is consistent with the changes made to other actions. It ensures that updates to multiple nested relations are processed in reverse order, which is important for maintaining the integrity of nested relations during complex operations.
  • 261-261: Finally, the application of enumerateReverse to the upsert action is central to the pull request's objective. This change ensures that the upsert operation correctly processes all nested relations in reverse order, addressing the issue that prompted the pull request.
  • 281-281: The use of enumerateReverse for the delete action further extends the reverse order processing strategy to deletion operations. This consistency across actions is important for ensuring that all nested relations are processed correctly, regardless of the operation being performed.
  • 291-291: Applying enumerateReverse to the deleteMany action is in line with the changes made to other actions. It ensures that deletions of multiple nested relations are processed in reverse order, which can be crucial for maintaining the integrity of nested relations during complex deletion operations.
  • 339-350: The implementation of the enumerateReverse method is crucial for enabling the reverse order processing of nested relations. This method allows for iterating over data in reverse order, which is essential for the changes made to various actions in the NestedWriteVisitor class. The method is well-implemented and directly supports the pull request's objective.
packages/runtime/src/enhancements/policy/handler.ts (6)
  • 1384-1394: The implementation of mergeToParent method is well-designed to handle merging values into a parent object, supporting both single and multiple relations under the same key. Ensure that this method is consistently used across all relevant parts of the class to manage nested relations effectively.
  • 1396-1408: The removeFromParent method is correctly implemented to handle the removal of values from a parent object, supporting both single values and arrays. It's essential for maintaining the integrity of nested relations. Verify its usage across the class to ensure it's applied correctly in all scenarios involving modification of nested relations.
  • 346-354: The usage of mergeToParent in the connectOrCreate method for handling the 'connect' and 'create' cases is appropriate and ensures that nested relations are correctly managed. This approach simplifies the logic and improves maintainability.
  • 358-358: The application of removeFromParent in the connectOrCreate method to remove the connectOrCreate clause after processing is a good practice. It ensures that the parent object remains clean and only contains the necessary data.
  • 888-888: Using removeFromParent in the update method to remove the 'create' clause after processing is consistent with the intended purpose of managing nested relations effectively. This ensures that the update payload is correctly structured before proceeding with the operation.
  • 921-937: The use of mergeToParent and removeFromParent in the upsert method to handle the conversion of an 'upsert' operation into an 'update' or 'create' operation is well thought out. It demonstrates a clear understanding of how to manage complex nested relations dynamically.

@ymc9 ymc9 merged commit e7e1873 into dev Mar 8, 2024
@ymc9 ymc9 deleted the fix/issue-1080 branch March 8, 2024 06:11
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