lnd icon indicating copy to clipboard operation
lnd copied to clipboard

lncfg: don't warn user about existing bbolt DB if SQLite files exist

Open Dhiren-Mhatre opened this issue 8 months ago • 7 comments

Change Description

Fix issue #9708 by checking for existing SQLite database files before warning about bbolt files.

Currently, when users start LND with the SQLite backend, they see warnings about existing bbolt database files even if they've already migrated their data using lndinit migrate-db. This PR enhances the warning system to only display these messages when a SQLite file doesn't exist yet, reducing confusion for users who have already completed migration.

Steps to Test

  1. Create a test environment with bbolt database files:

    mkdir -p ~/lnd-test-bolt
    lnd --lnddir=~/lnd-test-bolt --datadir=~/lnd-test-bolt
    # Create and fund a wallet
    lncli --lnddir=~/lnd-test-bolt create
    
  2. Migrate to SQLite:

    lndinit --lnddir=~/lnd-test-bolt migrate-db
    
  3. Run with SQLite backend before applying this fix:

    lnd --lnddir=~/lnd-test-bolt --db.backend=sqlite --db.use-native-sql
    

    Observe the warnings about existing bbolt files.

  4. Run with SQLite backend after applying this fix:

    lnd --lnddir=~/lnd-test-bolt --db.backend=sqlite --db.use-native-sql
    

    No warnings should appear since SQLite files exist.

  5. Delete the SQLite files and run again:

    rm ~/lnd-test-bolt/data/chain/bitcoin/regtest/chain.sqlite
    rm ~/lnd-test-bolt/data/graph/regtest/channel.sqlite
    lnd --lnddir=~/lnd-test-bolt --db.backend=sqlite --db.use-native-sql
    

    Warnings should reappear because bbolt files exist but SQLite files don't.

Pull Request Checklist

Testing

  • [x] Your PR passes all CI checks.
  • [x] Tests covering the positive and negative (error paths) are included.
  • [x] Bug fixes contain tests triggering the bug to prevent regressions.

Code Style and Documentation

Dhiren-Mhatre avatar Apr 20 '25 19:04 Dhiren-Mhatre

[!IMPORTANT]

Review skipped

Auto reviews are limited to specific labels.

:label: Labels to auto review (1)
  • llm-review

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ 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 generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this 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.

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.

coderabbitai[bot] avatar Apr 20 '25 19:04 coderabbitai[bot]

Please read the contribution guidelines. https://github.com/lightningnetwork/lnd/blob/master/docs/development_guidelines.md#ideal-git-commit-structure

I've updated my commit messages as per the contribution guidelines.Thank you for pointing this out!

Dhiren-Mhatre avatar Apr 26 '25 09:04 Dhiren-Mhatre

Have you tested the PR locally? I tried testing it, and here are the results:

  1. Running with SQLite only (no bbolt and no existing SQLite data) → warning does not show up (test passed).
  2. Running with SQLite only (no bbolt but with existing SQLite data) → warning does not show up (test passed).
  3. Running with SQLite (with bbolt data and no existing SQLite data) → warning does not show up (test failed).
  4. Running with SQLite (with bbolt data and with existing SQLite data) → warning does not show up (test passed).

The warning does not appear in any of these cases. I believe that, if the .sqlite file does not exist, it is created automatically, so your check !lnrpc.FileExists(sqlitePath) is never executed.

Additionally, I noticed that we are only checking here:

https://github.com/lightningnetwork/lnd/blob/7e50b8438ef5f88841002c4a8c23510928cfe64b/lncfg/db.go#L604-L609

So, currently no warnings are thrown for macaroons.db and sphinxreplay.db.

You're right about those points. I was worried about the FileExists check, but I wasn't sure if the SQLite files would be auto-created. Based on your testing, it seems like my implementation isn't behaving as expected. For the macaroons.db and sphinxreplay.db checks, I thought I was handling them in the switch statement, but you're right that they're not being checked in the main function calls. I'll update the PR to: Fix the logic to ensure warnings appear only when appropriate Add explicit checks for macaroons.db and sphinxreplay.db Thanks for the thorough review

Dhiren-Mhatre avatar Apr 28 '25 12:04 Dhiren-Mhatre

Did a quick pass: Could you explain what .migration-complete is? I couldn't find any such file.

Also, I think the code shouldn't be specific to migration but should handle general scenarios (including the migration case as well). wdyt?

NishantBansal2003 avatar Apr 29 '25 07:04 NishantBansal2003

The .migration-complete file was intended as a theoretical enhancement that doesn't actually exist yet. I added that check thinking it could be a reliable way to detect completed migrations (since SQLite files are auto-created even without migration).

I agree the code shouldn't be migration-specific. The simple approach in my second commit (just adding checks for macaroons.db and sphinxreplay.db) addresses what's actually needed:

  1. It checks for all database types mentioned in the issue
  2. It follows the existing pattern without introducing complex migration detection

I can update the PR to remove the migration-specific code and focus only on adding the additional file checks, which is what users need right now. Would that approach work better?

Dhiren-Mhatre avatar Apr 30 '25 08:04 Dhiren-Mhatre

I think it would be better if the approach focused on solving the original issue:

  1. I think it should only look for a bbolt DB and inform the user of its existence if the user has requested a SQLite DB AND the SQLite DB does not actually exist
  2. Also, if we do do these checks because the SQLite DB doesn't exist and we are trying to warn the user of what they actually have, we should also check for macaroons.db and sphinxreplay.db

I believe there are some straightforward ways to solve this issue using general logic.

Suggestion: It would be better if you push fully working and tested code that reflects the intended behavior.

NishantBansal2003 avatar Apr 30 '25 08:04 NishantBansal2003

@nishantbansal2003: review reminder

lightninglabs-deploy avatar Dec 11 '25 03:12 lightninglabs-deploy