Skip to content

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

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs/release-notes/release-notes-0.19.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ when running LND with an aux component injected (custom channels).
* [Fixed a bug](https://github.com/lightningnetwork/lnd/pull/9750): if a Taproot
address is added to LND using the `ImportTapscript` RPC, LND previously failed
to perform a cooperative close to that address.
* [Fixed a bug](https://github.com/lightningnetwork/lnd/pull/9744) where unnecessary
warnings about existing bbolt database files would appear when using SQLite backend
with already migrated databases.

# New Features

Expand Down
67 changes: 59 additions & 8 deletions lncfg/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package lncfg
import (
"context"
"fmt"
"os"
"path"
"path/filepath"
"time"
Expand Down Expand Up @@ -68,6 +69,10 @@ const (

// NSNeutrinoDB is the namespace name that we use for the neutrino DB.
NSNeutrinoDB = "neutrinodb"

// MigrationMarkerFile is a marker file created by lndinit migrate-db
// that indicates successful database migration.
MigrationMarkerFile = ".migration-complete"
)

// DB holds database configuration for LND.
Expand Down Expand Up @@ -484,6 +489,13 @@ func (db *DB) GetBackends(ctx context.Context, chanDBPath,
warnExistingBoltDBs(
logger, "postgres", chanDBPath, ChannelDBName,
)
// Also check for macaroons.db and sphinxreplay.db bbolt files
warnExistingBoltDBs(
logger, "postgres", walletDBPath, MacaroonDBName,
)
warnExistingBoltDBs(
logger, "postgres", chanDBPath, DecayedLogDbName,
)

returnEarly = false

Expand Down Expand Up @@ -599,14 +611,29 @@ func (db *DB) GetBackends(ctx context.Context, chanDBPath,
closeFuncs[SqliteBackend] = nativeSQLiteStore.Close
}

// Warn if the user is trying to switch over to a sqlite DB
// while there is a wallet or channel bbolt DB still present.
warnExistingBoltDBs(
logger, "sqlite", walletDBPath, WalletDBName,
)
warnExistingBoltDBs(
logger, "sqlite", chanDBPath, ChannelDBName,
)
// Check if migration has been completed before warning about
// existing bbolt databases
migrationComplete := hasMigrationCompleted(walletDBPath) ||
hasMigrationCompleted(chanDBPath)

// Only show warnings if migration hasn't been completed
if !migrationComplete {
// Warn if the user is trying to switch over to a sqlite DB
// while there is a wallet or channel bbolt DB still present.
warnExistingBoltDBs(
logger, "sqlite", walletDBPath, WalletDBName,
)
warnExistingBoltDBs(
logger, "sqlite", chanDBPath, ChannelDBName,
)
// Also check for macaroons.db and sphinxreplay.db bbolt files
warnExistingBoltDBs(
logger, "sqlite", walletDBPath, MacaroonDBName,
)
warnExistingBoltDBs(
logger, "sqlite", chanDBPath, DecayedLogDbName,
)
}

returnEarly = false

Expand Down Expand Up @@ -735,6 +762,30 @@ func (db *DB) GetBackends(ctx context.Context, chanDBPath,
}, nil
}

// hasMigrationCompleted checks if migration has been completed by looking for
// SQLite files with substantial size or a migration marker file.
func hasMigrationCompleted(dbPath string) bool {
// Check for marker file (which lndinit could create after migration)
markerPath := filepath.Join(dbPath, MigrationMarkerFile)
if _, err := os.Stat(markerPath); err == nil {
return true
}

// Check for non-empty SQLite files
sqliteFiles := []string{SqliteChainDBName, SqliteChannelDBName}
for _, fileName := range sqliteFiles {
filePath := filepath.Join(dbPath, fileName)
fileInfo, err := os.Stat(filePath)

// If file exists and has substantial size (not just created empty)
if err == nil && fileInfo.Size() > 1024 {
return true
}
}

return false
}

// warnExistingBoltDBs checks if there is an existing bbolt database in the
// given location and logs a warning if so.
func warnExistingBoltDBs(log btclog.Logger, dbType, dir, fileName string) {
Expand Down