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: Restore doesn't work when backup file is renamed #37666

Merged
merged 4 commits into from
Nov 25, 2024

Conversation

sharat87
Copy link
Member

@sharat87 sharat87 commented Nov 25, 2024

When backups are renamed from their original generated name, restore operation can't process that backup anymore. This PR should fix that.

Automation

/test sanity

🔍 Cypress test results

Tip

🟢 🟢 🟢 All cypress tests have passed! 🎉 🎉 🎉
Workflow run: https://github.com/appsmithorg/appsmith/actions/runs/12004342912
Commit: ef62c35
Cypress dashboard.
Tags: @tag.Sanity
Spec:


Mon, 25 Nov 2024 06:54:45 UTC

Communication

Should the DevRel and Marketing teams inform users about this change?

  • Yes
  • No

Summary by CodeRabbit

  • New Features

    • Introduced a new function to determine the contents path within backup archives, enhancing backup restoration capabilities.
  • Bug Fixes

    • Improved error handling for invalid backup file indices and password attempts during restoration.
  • Refactor

    • Streamlined output and error handling in the backup process, including cleanup and email notifications for backup errors.

Copy link
Contributor

coderabbitai bot commented Nov 25, 2024

Walkthrough

The pull request introduces modifications to the BackupState class in backup.ts and enhances the backup restoration process in restore.ts. Key changes include the removal of console logs, improved error handling for backup failures, and the addition of a new asynchronous function to determine backup contents. The cleanup process has been streamlined, and path handling has been made more consistent. Overall, these changes refine the output and error handling while maintaining the core functionality of the backup and restore processes.

Changes

File Path Change Summary
app/client/packages/rts/src/ctl/backup.ts - Removed console logs about free space and empty lines.
- Added error handling for --error-mail argument.
- Streamlined cleanup process in the finally block.
app/client/packages/rts/src/ctl/restore.ts - Added figureOutContentsPath function for determining backup contents path.
- Updated checkRestoreVersionCompatability for better path handling.
- Enhanced error handling in getBackupFileName.

Possibly related PRs

  • chore: Fix typing in ctl backup and restore #37663: This PR modifies the backup.ts and restore.ts files to enhance type safety by adding missing type annotations, which directly relates to the changes made in the run function and other functions in the backup.ts file in the main PR.

Suggested labels

Task

Suggested reviewers

  • nidhi-nair
  • pratapaprasanna
  • mohanarpit

🎶 In the realm of backups, we refine and mend,
With logs now silent, our paths we send.
Errors are caught, and contents we find,
A cleaner process, all well-defined.
So here’s to the code, both sturdy and bright,
In the world of restore, we’ve taken flight! 🎶


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:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • 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 gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @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 using 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.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

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.

@github-actions github-actions bot added the Bug Something isn't working label Nov 25, 2024
@sharat87
Copy link
Member Author

/build-deploy-preview skip-tests=true

Copy link

Deploying Your Preview: https://github.com/appsmithorg/appsmith/actions/runs/12003516564.
Workflow: On demand build Docker image and deploy preview.
skip-tests: true.
env: ``.
PR: 37666.
recreate: .

Copy link

Deploy-Preview-URL: https://ce-37666.dp.appsmith.com

@sharat87
Copy link
Member Author

@coderabbitai review

Copy link
Contributor

coderabbitai bot commented Nov 25, 2024

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

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

🧹 Outside diff range and nitpick comments (2)
app/client/packages/rts/src/ctl/restore.ts (2)

273-275: Consider using template literals for better readability

The message construction could be more readable using template literals.

-      "Please update your appsmith image to 'index.docker.io/appsmith/appsmith-ce:" +
-        restoreVersion +
-        "' in the 'docker-compose.yml' file\nand run the cmd: 'docker-compose restart' " +
-        "after the restore process is completed, to ensure the restored instance runs successfully.",
+      `Please update your appsmith image to 'index.docker.io/appsmith/appsmith-ce:${restoreVersion}' in the 'docker-compose.yml' file\nand run the cmd: 'docker-compose restart' after the restore process is completed, to ensure the restored instance runs successfully.`,

394-418: Consider early return pattern for cleaner code flow

The function handles the backup discovery well, but could be more concise using early returns.

 async function figureOutContentsPath(root: string): Promise<string> {
   const subfolders = await fsPromises.readdir(root, { withFileTypes: true });
 
-  for (const subfolder of subfolders) {
-    if (subfolder.isDirectory()) {
-      try {
-        // Try to find the `manifest.json` file.
-        await fsPromises.access(
-          path.join(root, subfolder.name, "manifest.json"),
-        );
-
-        return path.join(root, subfolder.name);
-      } catch (error) {
-        // If that fails, look for the MongoDB data archive, since some very old backups won't have `manifest.json`.
-        await fsPromises.access(
-          path.join(root, subfolder.name, "mongodb-data.gz"),
-        );
-
-        return path.join(root, subfolder.name);
-      }
-    }
-  }
+  const directories = subfolders.filter(subfolder => subfolder.isDirectory());
+  
+  for (const dir of directories) {
+    const dirPath = path.join(root, dir.name);
+    
+    // Try manifest.json first
+    try {
+      await fsPromises.access(path.join(dirPath, "manifest.json"));
+      return dirPath;
+    } catch {} // Intentionally empty - try mongodb-data.gz next
+    
+    // Try mongodb-data.gz for older backups
+    try {
+      await fsPromises.access(path.join(dirPath, "mongodb-data.gz"));
+      return dirPath;
+    } catch {} // Not found in this directory
+  }
 
   throw new Error("Could not find the contents of the backup archive.");
 }
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 392524e and 61c893a.

📒 Files selected for processing (2)
  • app/client/packages/rts/src/ctl/backup.ts (0 hunks)
  • app/client/packages/rts/src/ctl/restore.ts (4 hunks)
💤 Files with no reviewable changes (1)
  • app/client/packages/rts/src/ctl/backup.ts
🔇 Additional comments (2)
app/client/packages/rts/src/ctl/restore.ts (2)

252-253: LGTM! Improved cross-platform path handling

Using path.join instead of string concatenation ensures correct path separators across different operating systems.


355-355: LGTM! Good integration of the new backup discovery logic

The new figureOutContentsPath function is properly integrated into the restore flow, with errors properly propagated to the caller.

@sharat87 sharat87 added the ok-to-test Required label for CI label Nov 25, 2024
@sharat87 sharat87 marked this pull request as ready for review November 25, 2024 06:13
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

🧹 Outside diff range and nitpick comments (2)
app/client/packages/rts/src/ctl/restore.ts (2)

273-275: Consider using template literals for better readability

The string concatenation can be simplified using template literals.

-      "Please update your appsmith image to 'index.docker.io/appsmith/appsmith-ce:" +
-        restoreVersion +
-        "' in the 'docker-compose.yml' file\nand run the cmd: 'docker-compose restart' " +
-        "after the restore process is completed, to ensure the restored instance runs successfully.",
+      `Please update your appsmith image to 'index.docker.io/appsmith/appsmith-ce:${restoreVersion}' in the 'docker-compose.yml' file\nand run the cmd: 'docker-compose restart' after the restore process is completed, to ensure the restored instance runs successfully.`,

396-435: Good implementation with room for minor improvements

The function effectively handles both new and legacy backup formats. Consider these refinements:

 async function figureOutContentsPath(root: string): Promise<string> {
   const subfolders = await fsPromises.readdir(root, { withFileTypes: true });
 
+  // Check root first
   try {
-    // Check if the root itself contains the contents.
     await fsPromises.access(path.join(root, "manifest.json"));
     return root;
   } catch (error) {
-    // Ignore
+    // Root doesn't contain manifest.json, continue to check subfolders
   }
 
-  for (const subfolder of subfolders) {
+  for (const subfolder of subfolders as fsPromises.Dirent[]) {
     if (subfolder.isDirectory()) {
+      const subfolderPath = path.join(root, subfolder.name);
+
       try {
-        // Try to find the `manifest.json` file.
-        await fsPromises.access(
-          path.join(root, subfolder.name, "manifest.json"),
-        );
-
-        return path.join(root, subfolder.name);
+        await fsPromises.access(path.join(subfolderPath, "manifest.json"));
+        return subfolderPath;
       } catch (error) {
-        // Ignore
+        // Try legacy format
       }
 
       try {
-        // If that fails, look for the MongoDB data archive, since backups from v1.7.x and older won't have `manifest.json`.
-        await fsPromises.access(
-          path.join(root, subfolder.name, "mongodb-data.gz"),
-        );
-
-        return path.join(root, subfolder.name);
+        await fsPromises.access(path.join(subfolderPath, "mongodb-data.gz"));
+        return subfolderPath;
       } catch (error) {
-        // Ignore
+        // Neither manifest.json nor mongodb-data.gz found in this subfolder
       }
     }
   }
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 61c893a and ef62c35.

📒 Files selected for processing (1)
  • app/client/packages/rts/src/ctl/restore.ts (4 hunks)
🔇 Additional comments (2)
app/client/packages/rts/src/ctl/restore.ts (2)

252-253: LGTM: Improved path handling

Using path.join is the correct approach for cross-platform compatibility.


357-359: LGTM: Clean integration of new function

The integration is clean and maintains good code readability with appropriate spacing.

@sharat87 sharat87 merged commit b9ce30e into release Nov 25, 2024
51 checks passed
@sharat87 sharat87 deleted the chore/fix-backup-renaming branch November 25, 2024 07:23
github-actions bot pushed a commit to Zeral-Zhang/appsmith that referenced this pull request Nov 27, 2024
)

When backups are renamed from their original generated name, restore
operation can't process that backup anymore. This PR should fix that.


## Automation

/test sanity

### 🔍 Cypress test results
<!-- This is an auto-generated comment: Cypress test results  -->
> [!TIP]
> 🟢 🟢 🟢 All cypress tests have passed! 🎉 🎉 🎉
> Workflow run:
<https://github.com/appsmithorg/appsmith/actions/runs/12004342912>
> Commit: ef62c35
> <a
href="https://internal.appsmith.com/app/cypress-dashboard/rundetails-65890b3c81d7400d08fa9ee5?branch=master&workflowId=12004342912&attempt=1"
target="_blank">Cypress dashboard</a>.
> Tags: `@tag.Sanity`
> Spec:
> <hr>Mon, 25 Nov 2024 06:54:45 UTC
<!-- end of auto-generated comment: Cypress test results  -->


## Communication
Should the DevRel and Marketing teams inform users about this change?
- [ ] Yes
- [x] No


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

- **New Features**
- Introduced a new function to determine the contents path within backup
archives, enhancing backup restoration capabilities.
  
- **Bug Fixes**
- Improved error handling for invalid backup file indices and password
attempts during restoration.

- **Refactor**
- Streamlined output and error handling in the backup process, including
cleanup and email notifications for backup errors.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Something isn't working ok-to-test Required label for CI
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants