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

verify remote collection before local collection deletion #2701

Merged
merged 1 commit into from
Nov 9, 2024

Conversation

imolorhe
Copy link
Collaborator

@imolorhe imolorhe commented Nov 9, 2024

Fixes

Checks

  • Ran yarn test-build
  • Updated relevant documentations
  • Updated matching config options in altair-static

Changes proposed in this pull request:

Summary by Sourcery

Verify the creation of remote collections before deleting local collections to prevent data loss and handle subcollections during remote collection creation.

Bug Fixes:

  • Ensure remote collection is verified before deleting the local collection to prevent data loss.

Enhancements:

  • Add handling for subcollections when creating remote collections to ensure all subcollections are also created remotely.

Copy link

sourcery-ai bot commented Nov 9, 2024

Reviewer's Guide by Sourcery

This PR enhances the query collection synchronization between local and remote storage by implementing verification steps before local collection deletion and adding support for subcollection creation. The changes also include error handling improvements and code formatting cleanup.

Sequence diagram for verifying remote collection before local deletion

sequenceDiagram
    participant User
    participant LocalStorage
    participant RemoteStorage
    User->>LocalStorage: Request to delete collection
    LocalStorage->>RemoteStorage: Create remote collection
    alt Remote collection creation failed
        RemoteStorage-->>LocalStorage: Error
        LocalStorage->>User: Notify failure
    else Remote collection created
        RemoteStorage-->>LocalStorage: Return collection ID
        LocalStorage->>RemoteStorage: Verify remote collection
        alt Verification failed
            RemoteStorage-->>LocalStorage: Error
            LocalStorage->>User: Notify failure
        else Verification successful
            LocalStorage->>LocalStorage: Delete local collection
            LocalStorage->>User: Notify success
        end
    end
Loading

File-Level Changes

Change Details Files
Added verification steps when moving collections from local to remote storage
  • Added verification of remote collection creation success
  • Added validation of query count matching between local and remote collections
  • Implemented checks to ensure remote collection exists before local deletion
packages/altair-app/src/app/modules/altair/services/query-collection/query-collection.service.ts
Implemented subcollection creation support for remote collections
  • Modified createRemoteCollection to handle IQueryCollection type
  • Added recursive creation of subcollections when creating remote collections
  • Added parent collection ID handling for subcollections
packages/altair-app/src/app/modules/altair/services/query-collection/query-collection.service.ts
Enhanced error handling in window service collection retrieval
  • Added error catching for collection retrieval
  • Implemented fallback behavior when collection is not found
packages/altair-app/src/app/modules/altair/services/window.service.ts

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time. You can also use
    this command to specify where the summary should be inserted.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@imolorhe imolorhe linked an issue Nov 9, 2024 that may be closed by this pull request
1 task
Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @imolorhe - I've reviewed your changes and they look great!

Here's what I looked at during the review
  • 🟢 General issues: all looks good
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟡 Complexity: 1 issue found
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@@ -122,7 +138,22 @@ export class QueryCollectionService {
// Create remote collection
// Delete local collection
const localCollection = await this.mustGetLocalCollection(collectionId);
await this.createRemoteCollection(localCollection);
const resId = await this.createRemoteCollection(localCollection);
Copy link

Choose a reason for hiding this comment

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

issue (complexity): Consider extracting verification and subcollection creation logic into dedicated methods to improve code organization

The code could be simplified while maintaining the same functionality:

  1. Extract verification logic into a dedicated method:
private async verifyRemoteCollection(remoteId: string, localCollection: IQueryCollection) {
  const remote = await this.api.getCollection(remoteId);
  if (!remote) {
    throw new Error('Failed to retrieve the remote collection');
  }
  if (remote.queries.length !== localCollection.queries.length) {
    throw new Error('Query count mismatch');
  }
  return remote;
}
  1. Extract subcollection creation into a focused method:
private async createSubcollections(
  parentId: string, 
  originalCollectionId: string,
  workspaceId?: WorkspaceId,
  teamId?: TeamId
) {
  const subcollections = await this.getSubcollections(originalCollectionId);
  await Promise.all(subcollections.map(sub => 
    sub?.id && this.api.createQueryCollection(sub, parentId, workspaceId, teamId)
  ));
}

This simplifies the main methods:

async createRemoteCollection(
  collection: CreateDTO<IQueryCollection> | IQueryCollection,
  workspaceId?: WorkspaceId,
  teamId?: TeamId
) {
  if (!(await this.canApplyRemote())) return;

  const res = await this.api.createQueryCollection(collection, undefined, workspaceId, teamId);
  if (!res) throw new Error('could not create the collection');

  if ('id' in collection) {
    await this.createSubcollections(res.id, collection.id, workspaceId, teamId);
  }
  return res.id;
}

async transformCollectionToRemoteCollection(collectionId: CollectionID) {
  if (!(await this.canApplyRemote())) return;

  const localCollection = await this.mustGetLocalCollection(collectionId);
  const remoteId = await this.createRemoteCollection(localCollection);
  await this.verifyRemoteCollection(remoteId, localCollection);
  await this.deleteLocalCollection(collectionId);
}

Copy link

github-actions bot commented Nov 9, 2024

Visit the preview URL for this PR (updated for commit 0031c88):

https://altair-gql--pr2701-imolorhe-verify-remo-5q80oimj.web.app

(expires Sat, 16 Nov 2024 03:09:20 GMT)

🔥 via Firebase Hosting GitHub Action 🌎

Sign: 02d6323d75a99e532a38922862e269d63351a6cf

@imolorhe imolorhe added this pull request to the merge queue Nov 9, 2024
Merged via the queue into master with commit 17e6831 Nov 9, 2024
14 checks passed
@imolorhe imolorhe deleted the imolorhe/verify-remote-before-local-deletion branch November 9, 2024 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] Sync to Remote feature deleted all my collections [URGENT]
1 participant