Skip to content
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

fix(api): Project hard sync existing entities deleted #660

Merged
merged 3 commits into from
Feb 4, 2025

Conversation

csehatt741
Copy link
Contributor

@csehatt741 csehatt741 commented Jan 25, 2025

User description

Description

When hard syncing all the existing items are deleted in the fork

Fixes #454

Screenshots of relevant screens

image

Developer's checklist

  • My PR follows the style guidelines of this project
  • I have performed a self-check on my work

If changes are made in the code:

  • I have followed the coding guidelines
  • My changes in code generate no new warnings
  • My changes are breaking another fix/feature of the project
  • I have added test cases to show that my feature works
  • I have added relevant screenshots in my PR
  • There are no UI/UX issues

Documentation Update

  • This PR requires an update to the documentation at docs.keyshade.xyz
  • I have made the necessary updates to the documentation, or no documentation changes are required.

PR Type

Bug fix, Documentation


Description

  • Fixed unique constraint error during project hard sync.

  • Added deletion of existing entities in target project for hard sync.

  • Updated CLI documentation for --hard-sync option.


Changes walkthrough 📝

Relevant files
Bug fix
project.service.ts
Add deletion logic for hard sync in projects                         

apps/api/src/project/service/project.service.ts

  • Added logic to delete existing entities during hard sync.
  • Introduced deleteOps array to handle deletion operations.
  • Ensured proper sequencing of deletion and creation operations.
  • +35/-1   
    Documentation
    project.md
    Update CLI documentation for hard sync                                     

    docs/cli/project.md

  • Updated description for --hard-sync option.
  • Clarified behavior of hard sync in CLI documentation.
  • +1/-1     

    Need help?
  • Type /help how to ... in the comments thread for any questions about Qodo Merge usage.
  • Check out the documentation for more information.
  • Copy link
    Contributor

    PR Reviewer Guide 🔍

    Here are some key observations to aid the review process:

    🎫 Ticket compliance analysis ✅

    454 - PR Code Verified

    Compliant requirements:

    • Fix unique constraint error when syncing a project with hardSync=true
    • Project sync should work without errors when hardSync is enabled

    Requires further human verification:

    • Verify that sync operation works correctly in Postman with hardSync=true
    ⏱️ Estimated effort to review: 2 🔵🔵⚪⚪⚪
    🧪 No relevant tests
    🔒 No security concerns identified
    ⚡ Recommended focus areas for review

    Data Loss Risk

    Hard sync deletes all existing entities without backup or confirmation. Consider adding a warning or backup mechanism.

    deleteOps.push(
      this.prisma.environment.deleteMany({
        where: {
          projectId: toProject.id
        }
      })
    )
    
    deleteOps.push(
      this.prisma.secret.deleteMany({
        where: {
          projectId: toProject.id
        }
      })
    )
    
    deleteOps.push(
      this.prisma.variable.deleteMany({
        where: {
          projectId: toProject.id
        }
      })
    )
    Transaction Safety

    Multiple delete and create operations are executed separately. Consider wrapping them in a transaction to ensure atomicity.

    return [
      ...deleteOps,
      ...createEnvironmentOps,
      ...createSecretOps,
      ...createVariableOps
    ]

    Copy link
    Contributor

    PR Code Suggestions ✨

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Score
    Possible issue
    Add transaction for data consistency

    Add error handling and transaction wrapping around the deletion operations to ensure
    data consistency and prevent partial deletions if an operation fails.

    apps/api/src/project/service/project.service.ts [1149-1154]

    -return [
    +return await this.prisma.$transaction([
       ...deleteOps,
       ...createEnvironmentOps,
       ...createSecretOps,
       ...createVariableOps
    -]
    +])
    Suggestion importance[1-10]: 9

    Why: Adding transaction support is critical for maintaining data consistency during complex operations involving multiple deletions and creations. Without it, failed operations could leave the database in an inconsistent state.

    9
    Security
    Prevent accidental same-project deletion

    Validate that the project IDs exist and are different before proceeding with
    deletion operations to prevent accidental data loss.

    apps/api/src/project/service/project.service.ts [973-979]

    -deleteOps.push(
    -  this.prisma.environment.deleteMany({
    -    where: {
    -      projectId: toProject.id
    -    }
    -  })
    -)
    +if (toProject.id !== fromProject.id) {
    +  deleteOps.push(
    +    this.prisma.environment.deleteMany({
    +      where: {
    +        projectId: toProject.id
    +      }
    +    })
    +  )
    +}
    • Apply this suggestion
    Suggestion importance[1-10]: 8

    Why: This safety check is important to prevent accidental data loss by ensuring we don't delete data from the source project, which could have severe consequences in production.

    8

    @@ -1117,7 +1146,12 @@ export class ProjectService {
    )
    }

    return [...createEnvironmentOps, ...createSecretOps, ...createVariableOps]
    return [
    ...deleteOps,
    Copy link
    Member

    Choose a reason for hiding this comment

    The reason will be displayed to describe this comment to others. Learn more.

    I feel that this wont work. This is because, we are not actually deleting the variables, secrets or environments. So the subsequent block wont work.

    We need to refactor the missingEnvironments section aswell to make sure that we don't do that comparison when we are doing a hard sync. Hope it makes sense

    Copy link
    Contributor Author

    Choose a reason for hiding this comment

    The reason will be displayed to describe this comment to others. Learn more.

    I`ve tested it and it does delete all existing items from forked project then copies everything from the parent.

    To my understanding this is what hard sync is about.

    Copy link
    Member

    Choose a reason for hiding this comment

    The reason will be displayed to describe this comment to others. Learn more.

    Yes your understanding is correct. I was talking about the next block where in we are checking for existing environments. In case we are hard syncing a project, we would be deleting the environments. So there shouldn't be any environments left to check for. So, it would be the best to use that block optionally.

    Copy link
    Member

    Choose a reason for hiding this comment

    The reason will be displayed to describe this comment to others. Learn more.

    I guess i pinned this in the wrong place. It's line 974 that im talking about.

    @rajdip-b rajdip-b merged commit 3632217 into keyshade-xyz:develop Feb 4, 2025
    2 of 4 checks passed
    rajdip-b pushed a commit that referenced this pull request Feb 4, 2025
    ## [2.11.0-stage.11](v2.11.0-stage.10...v2.11.0-stage.11) (2025-02-04)
    
    ### 🐛 Bug Fixes
    
    * **api:** Project hard sync existing entities deleted ([#660](#660)) ([3632217](3632217))
    @rajdip-b
    Copy link
    Member

    rajdip-b commented Feb 4, 2025

    🎉 This PR is included in version 2.11.0-stage.11 🎉

    The release is available on GitHub release

    Your semantic-release bot 📦🚀

    @csehatt741 csehatt741 deleted the project-hard-sync branch February 4, 2025 07:50
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Projects
    None yet
    Development

    Successfully merging this pull request may close these issues.

    API: Error while hard syncing a project
    2 participants