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: failing backup and restore appsmithctl #35162

Merged
merged 5 commits into from
Aug 6, 2024

Conversation

pratapaprasanna
Copy link
Contributor

@pratapaprasanna pratapaprasanna commented Jul 24, 2024

Fixes: #34996

Summary by CodeRabbit

  • New Features
    • Introduced a new utility function for dynamic database URL retrieval, improving configuration management and flexibility across the application.
    • Enhanced existing database export, import, and restore functionalities to utilize the new utility for obtaining database URLs.
  • Bug Fixes
    • Replaced hardcoded database URLs with dynamic retrieval methods to reduce potential errors and improve maintainability.

Copy link
Contributor

coderabbitai bot commented Jul 24, 2024

Caution

Review failed

The pull request is closed.

Walkthrough

The recent updates enhance the database URL retrieval process in the application by introducing the getDburl utility function. This function integrates the logic for fetching the database URL from both environment variables and a configuration file, promoting better maintainability. Additionally, the new ENV_PATH constant improves Docker configuration management. Overall, these changes facilitate a cleaner, more flexible codebase while reducing reliance on hardcoded values.

Changes

Files Change Summary
deploy/docker/fs/opt/appsmith/utils/bin/backup.js Updated exportDatabase and createManifestFile functions to use utils.getDburl() for obtaining the database URL, enhancing maintainability and consistency.
deploy/docker/fs/opt/appsmith/utils/bin/constants.js Introduced a new constant ENV_PATH for the Docker environment configuration file and added it to module exports to enhance configuration management.
deploy/docker/fs/opt/appsmith/utils/bin/utils.js Added getDburl function for retrieving the database URL from environment variables and a specified file, exported the function, and included the Node.js fs module.
deploy/docker/fs/opt/appsmith/utils/bin/restore.js Modified restoreDatabase to accept a dbUrl parameter, enhancing flexibility, and updated references to use utils.getDburl() for consistency in URL handling.
deploy/docker/fs/opt/appsmith/utils/bin/export_db.js Enhanced export_database function to utilize utils.getDburl() instead of hardcoded environment variables, improving flexibility and maintainability.
deploy/docker/fs/opt/appsmith/utils/bin/import_db.js Updated import_database function to fetch the database URL dynamically via utils.getDburl() instead of using a hardcoded value, enhancing maintainability.

Sequence Diagram(s)

sequenceDiagram
    participant App as Application
    participant Utils as Utils Module
    participant Config as Configuration File
    participant Env as Environment Variables

    App->>Utils: Call getDburl()
    Utils->>Env: Check APPSMITH_DB_URL
    alt If defined
        Utils-->>App: Return APPSMITH_DB_URL
    else If not defined
        Utils->>Config: Read from ENV_PATH
        Config-->>Utils: Provide DB URL
        Utils-->>App: Return DB URL
    end
Loading

Assessment against linked issues

Objective Addressed Explanation
Improve backup functionality to avoid failures (#34996)
Centralize database URL management for flexibility
Ensure dynamic retrieval of database URL

Poem

In the realm of code, where changes abound,
A utility is born, with purpose profound.
From constants to functions, our paths intertwine,
With each line we craft, a solution we find.
Let’s raise our cheers for this tidy design,
As code and creation together align! ✨


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>.
    • 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 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 Configuration 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: 1

Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between ddc2304 and bb0509f.

Files selected for processing (3)
  • deploy/docker/fs/opt/appsmith/utils/bin/backup.js (3 hunks)
  • deploy/docker/fs/opt/appsmith/utils/bin/constants.js (2 hunks)
  • deploy/docker/fs/opt/appsmith/utils/bin/utils.js (3 hunks)
Additional context used
Biome
deploy/docker/fs/opt/appsmith/utils/bin/utils.js

[error] 49-49: Shouldn't redeclare 'dbUrl'. Consider to delete it or rename it.

'dbUrl' is defined here:

(lint/suspicious/noRedeclare)

Additional comments not posted (6)
deploy/docker/fs/opt/appsmith/utils/bin/constants.js (2)

12-13: Good addition of ENV_PATH constant.

The ENV_PATH constant provides a clear reference to the Docker environment configuration file, which enhances maintainability.


29-30: Proper inclusion of ENV_PATH in module exports.

Including ENV_PATH in the module exports makes it accessible to other modules, promoting reusability.

deploy/docker/fs/opt/appsmith/utils/bin/utils.js (2)

5-5: Correct inclusion of the fs module.

The fs module is necessary for file operations within the new getDburl function.


197-198: Proper inclusion of getDburl in module exports.

Including getDburl in the module exports makes it accessible to other modules, promoting reusability.

deploy/docker/fs/opt/appsmith/utils/bin/backup.js (2)

128-128: Good use of utils.getDburl() in exportDatabase.

Using utils.getDburl() centralizes the logic for obtaining the database URL, enhancing maintainability.


144-144: Good use of utils.getDburl() in createManifestFile.

Using utils.getDburl() centralizes the logic for obtaining the database URL, enhancing maintainability.

deploy/docker/fs/opt/appsmith/utils/bin/utils.js Outdated Show resolved Hide resolved
Copy link

github-actions bot commented Aug 1, 2024

This PR has not seen activitiy for a while. It will be closed in 7 days unless further activity is detected.

@github-actions github-actions bot added the Stale label Aug 1, 2024
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.yaml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between bb0509f and c32725f.

Files selected for processing (1)
  • deploy/docker/fs/opt/appsmith/utils/bin/restore.js (3 hunks)
Additional comments not posted (2)
deploy/docker/fs/opt/appsmith/utils/bin/restore.js (2)

Line range hint 60-71:
Great job on improving the flexibility of the restoreDatabase function!

The changes to accept dbUrl as a parameter and use it in the mongorestore command enhance the function's adaptability. Ensure that the dbUrl parameter is correctly passed and sanitized to prevent any security vulnerabilities.


212-213: Nice update to the run function!

The changes to pass utils.getDburl() as an argument to restoreDatabase align well with the updated function signature. Ensure that utils.getDburl() returns a valid and sanitized database URL.

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

Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between c32725f and c4e22957ae130ef010b60e6f035a03ead5be837d.

Files selected for processing (1)
  • deploy/docker/fs/opt/appsmith/utils/bin/utils.js (3 hunks)
Additional comments not posted (2)
deploy/docker/fs/opt/appsmith/utils/bin/utils.js (2)

198-198: Confirm the addition of getDburl to module exports.

The getDburl function has been correctly added to the module exports.


48-52: Use a fallback mechanism for environment variables.

Ensure that the fallback mechanism for environment variables is properly implemented.

-  let dbEnvUrl = process.env.APPSMITH_DB_URL || process.env.APPSMITH_MONGO_DB_URI;
-  if (dbEnvUrl && dbEnvUrl !== "undefined") {
-    dbUrl = dbEnvUrl;
-  }
+  const dbEnvUrl = process.env.APPSMITH_DB_URL || process.env.APPSMITH_MONGO_DB_URI;
+  if (dbEnvUrl && dbEnvUrl !== "undefined") {
+    dbUrl = dbEnvUrl;
+  }

Likely invalid or redundant comment.

Comment on lines +37 to +47
try {
let env_array = fs.readFileSync(Constants.ENV_PATH, 'utf8').toString().split("\n");
for (let i in env_array) {
if (env_array[i].startsWith("APPSMITH_MONGODB_URI") || env_array[i].startsWith("APPSMITH_DB_URL")) {
dbUrl = env_array[i].toString().split("=")[1];
break; // Break early when the desired line is found
}
}
} catch (err) {
console.error("Error reading the environment file:", err);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Avoid redeclaring variables and optimize the loop.

The dbUrl variable is redeclared, which can be avoided by declaring it once and updating its value conditionally. Additionally, the loop can be optimized by breaking early when the desired line is found.

-  let dbUrl;
-  try {
-    let env_array = fs.readFileSync(Constants.ENV_PATH, 'utf8').toString().split("\n");
-    for (let i in env_array) {
-      if (env_array[i].startsWith("APPSMITH_MONGODB_URI") || env_array[i].startsWith("APPSMITH_DB_URL")) {
-        dbUrl = env_array[i].toString().split("=")[1];
-        break; // Break early when the desired line is found
-      }
-    }
-  } catch (err) {
-    console.error("Error reading the environment file:", err);
-  }
+  let dbUrl = '';
+  try {
+    let env_array = fs.readFileSync(Constants.ENV_PATH, 'utf8').toString().split("\n");
+    for (let i in env_array) {
+      if (env_array[i].startsWith("APPSMITH_MONGODB_URI") || env_array[i].startsWith("APPSMITH_DB_URL")) {
+        dbUrl = env_array[i].toString().split("=")[1];
+        break; // Break early when the desired line is found
+      }
+    }
+  } catch (err) {
+    console.error("Error reading the environment file:", err);
+  }
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
try {
let env_array = fs.readFileSync(Constants.ENV_PATH, 'utf8').toString().split("\n");
for (let i in env_array) {
if (env_array[i].startsWith("APPSMITH_MONGODB_URI") || env_array[i].startsWith("APPSMITH_DB_URL")) {
dbUrl = env_array[i].toString().split("=")[1];
break; // Break early when the desired line is found
}
}
} catch (err) {
console.error("Error reading the environment file:", err);
}
let dbUrl = '';
try {
let env_array = fs.readFileSync(Constants.ENV_PATH, 'utf8').toString().split("\n");
for (let i in env_array) {
if (env_array[i].startsWith("APPSMITH_MONGODB_URI") || env_array[i].startsWith("APPSMITH_DB_URL")) {
dbUrl = env_array[i].toString().split("=")[1];
break; // Break early when the desired line is found
}
}
} catch (err) {
console.error("Error reading the environment file:", err);
}

@pratapaprasanna
Copy link
Contributor Author

/build-deploy-preview skip-tests=true

Copy link

github-actions bot commented Aug 5, 2024

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

@pratapaprasanna pratapaprasanna force-pushed the fix/failing/backup/restore branch from c4e2295 to 1f89992 Compare August 5, 2024 17:27
@pratapaprasanna pratapaprasanna changed the title [wip]fix: failing backup and restore appsmithctl fix: failing backup and restore appsmithctl Aug 5, 2024
@github-actions github-actions bot added the Bug Something isn't working label Aug 5, 2024
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.yaml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between c4e22957ae130ef010b60e6f035a03ead5be837d and 1f89992.

Files selected for processing (1)
  • deploy/docker/fs/opt/appsmith/utils/bin/utils.js (3 hunks)
Files skipped from review as they are similar to previous changes (1)
  • deploy/docker/fs/opt/appsmith/utils/bin/utils.js

Copy link

github-actions bot commented Aug 5, 2024

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

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.yaml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 1f89992 and 2273ffe.

Files selected for processing (3)
  • deploy/docker/fs/opt/appsmith/utils/bin/export_db.js (2 hunks)
  • deploy/docker/fs/opt/appsmith/utils/bin/import_db.js (2 hunks)
  • deploy/docker/fs/opt/appsmith/utils/bin/restore.js (4 hunks)
Files skipped from review as they are similar to previous changes (1)
  • deploy/docker/fs/opt/appsmith/utils/bin/restore.js
Additional comments not posted (4)
deploy/docker/fs/opt/appsmith/utils/bin/export_db.js (2)

4-4: Good practice: Importing utility functions.

Importing the utils module helps in maintaining cleaner and more modular code.


8-8: Dynamic DB URL retrieval.

Using utils.getDburl() to fetch the database URL dynamically enhances flexibility and maintainability.

deploy/docker/fs/opt/appsmith/utils/bin/import_db.js (2)

6-6: Good practice: Importing utility functions.

Importing the utils module helps in maintaining cleaner and more modular code.


11-11: Dynamic DB URL retrieval.

Using utils.getDburl() to fetch the database URL dynamically enhances flexibility and maintainability.

@pratapaprasanna
Copy link
Contributor Author

/build-deploy-preview skip-tests=true

Copy link

github-actions bot commented Aug 6, 2024

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

Copy link

github-actions bot commented Aug 6, 2024

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

@github-actions github-actions bot added Backup & Restore Issues related to backup and restore DevOps Pod Issues related to devops High This issue blocks a user from building or impacts a lot of users Needs Triaging Needs attention from maintainers to triage Production labels Aug 6, 2024
Comment on lines 48 to 51
let dbEnvUrl = process.env.APPSMITH_DB_URL || process.env.APPSMITH_MONGO_DB_URI;
if (dbEnvUrl && dbEnvUrl !== "undefined") {
dbUrl = dbEnvUrl;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Utility functions should generally be pure and env variables should not be a part of it. If you were to drive the utility function the env values as a parameter is okay, however i think you can move this function into a separate file and not be a part of the utility file.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Merging this for the time being because of the urgency. I will address this in the next release
thanks @vsvamsi1

@@ -31,6 +32,26 @@ function start(apps) {
console.log("Started " + appsStr);
}

function getDburl() {
let dbUrl = '';
Copy link
Contributor

Choose a reason for hiding this comment

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

I have a nitpick comment.
It appears to me that dbUrl variable job is to be just eventually returned, i think we can remove this and directly the return the result.

Copy link
Contributor

Choose a reason for hiding this comment

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

An example

function getDburl() {

  try {
    const env_array = fs.readFileSync(Constants.ENV_PATH, 'utf8').toString().split("\n");
    for (let i in env_array) {
      if (env_array[i].startsWith("APPSMITH_MONGODB_URI") || env_array[i].startsWith("APPSMITH_DB_URL")) {
       // return early when the desired line is found
       return  env_array[i].toString().split("=")[1];
        
      }
    }
  } catch (err) {
    console.error("Error reading the environment file:", err);
  }
}

  let dbEnvUrl = process.env.APPSMITH_DB_URL || process.env.APPSMITH_MONGO_DB_URI;
  if (dbEnvUrl && dbEnvUrl !== "undefined") {
   return dbEnvUrl;
  }

 return "";
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Backup & Restore Issues related to backup and restore Bug Something isn't working DevOps Pod Issues related to devops High This issue blocks a user from building or impacts a lot of users Needs Triaging Needs attention from maintainers to triage Production QA Needs QA attention Stale
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug]: appsmith backup fails while taking backup
2 participants