Skip to content

Commit 4db2891

Browse files
committed
lncfg: add checks for macaroons.db and sphinxreplay.db files
Signed-off-by: Dhiren-Mhatre <[email protected]>
1 parent ac28fbd commit 4db2891

File tree

1 file changed

+63
-48
lines changed

1 file changed

+63
-48
lines changed

lncfg/db.go

+63-48
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package lncfg
33
import (
44
"context"
55
"fmt"
6+
"os"
67
"path"
78
"path/filepath"
89
"time"
@@ -68,6 +69,10 @@ const (
6869

6970
// NSNeutrinoDB is the namespace name that we use for the neutrino DB.
7071
NSNeutrinoDB = "neutrinodb"
72+
73+
// MigrationMarkerFile is a marker file created by lndinit migrate-db
74+
// that indicates successful database migration.
75+
MigrationMarkerFile = ".migration-complete"
7176
)
7277

7378
// DB holds database configuration for LND.
@@ -484,6 +489,13 @@ func (db *DB) GetBackends(ctx context.Context, chanDBPath,
484489
warnExistingBoltDBs(
485490
logger, "postgres", chanDBPath, ChannelDBName,
486491
)
492+
// Also check for macaroons.db and sphinxreplay.db bbolt files
493+
warnExistingBoltDBs(
494+
logger, "postgres", walletDBPath, MacaroonDBName,
495+
)
496+
warnExistingBoltDBs(
497+
logger, "postgres", chanDBPath, DecayedLogDbName,
498+
)
487499

488500
returnEarly = false
489501

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

602-
// Warn if the user is trying to switch over to a sqlite DB
603-
// while there is a wallet or channel bbolt DB still present.
604-
warnExistingBoltDBs(
605-
logger, "sqlite", walletDBPath, WalletDBName,
606-
)
607-
warnExistingBoltDBs(
608-
logger, "sqlite", chanDBPath, ChannelDBName,
609-
)
614+
// Check if migration has been completed before warning about
615+
// existing bbolt databases
616+
migrationComplete := hasMigrationCompleted(walletDBPath) ||
617+
hasMigrationCompleted(chanDBPath)
618+
619+
// Only show warnings if migration hasn't been completed
620+
if !migrationComplete {
621+
// Warn if the user is trying to switch over to a sqlite DB
622+
// while there is a wallet or channel bbolt DB still present.
623+
warnExistingBoltDBs(
624+
logger, "sqlite", walletDBPath, WalletDBName,
625+
)
626+
warnExistingBoltDBs(
627+
logger, "sqlite", chanDBPath, ChannelDBName,
628+
)
629+
// Also check for macaroons.db and sphinxreplay.db bbolt files
630+
warnExistingBoltDBs(
631+
logger, "sqlite", walletDBPath, MacaroonDBName,
632+
)
633+
warnExistingBoltDBs(
634+
logger, "sqlite", chanDBPath, DecayedLogDbName,
635+
)
636+
}
610637

611638
returnEarly = false
612639

@@ -735,51 +762,39 @@ func (db *DB) GetBackends(ctx context.Context, chanDBPath,
735762
}, nil
736763
}
737764

738-
// warnExistingBoltDBs checks if there is an existing bbolt database in the
739-
// given location and logs a warning if so.
740-
func warnExistingBoltDBs(log btclog.Logger, dbType, dir, fileName string) {
741-
// Check if the bbolt file exists
742-
bboltPath := filepath.Join(dir, fileName)
743-
if !lnrpc.FileExists(bboltPath) {
744-
// No bbolt file, no need for warning
745-
return
765+
// hasMigrationCompleted checks if migration has been completed by looking for
766+
// SQLite files with substantial size or a migration marker file.
767+
func hasMigrationCompleted(dbPath string) bool {
768+
// Check for marker file (which lndinit could create after migration)
769+
markerPath := filepath.Join(dbPath, MigrationMarkerFile)
770+
if _, err := os.Stat(markerPath); err == nil {
771+
return true
746772
}
747773

748-
// Determine the corresponding SQLite file name based on the bbolt file
749-
var sqliteFileName string
750-
switch fileName {
751-
case WalletDBName:
752-
sqliteFileName = SqliteChainDBName
753-
case ChannelDBName:
754-
sqliteFileName = SqliteChannelDBName
755-
case MacaroonDBName:
756-
sqliteFileName = SqliteChainDBName
757-
case DecayedLogDbName:
758-
sqliteFileName = SqliteChannelDBName
759-
case TowerClientDBName:
760-
sqliteFileName = SqliteChannelDBName
761-
case TowerServerDBName:
762-
sqliteFileName = SqliteTowerDBName
763-
default:
764-
// For any other file types, still show the warning
765-
log.Warnf("Found existing bbolt database file in %s/%s "+
766-
"while using database type %s. Existing data will "+
767-
"NOT be migrated to %s automatically!",
768-
dir, fileName, dbType, dbType)
769-
770-
return
774+
// Check for non-empty SQLite files
775+
sqliteFiles := []string{SqliteChainDBName, SqliteChannelDBName}
776+
for _, fileName := range sqliteFiles {
777+
filePath := filepath.Join(dbPath, fileName)
778+
fileInfo, err := os.Stat(filePath)
779+
780+
// If file exists and has substantial size (not just created empty)
781+
if err == nil && fileInfo.Size() > 1024 {
782+
return true
783+
}
771784
}
785+
786+
return false
787+
}
772788

773-
// Check if the corresponding SQLite file exists
774-
sqlitePath := filepath.Join(dir, sqliteFileName)
775-
if !lnrpc.FileExists(sqlitePath) {
776-
// SQLite file doesn't exist, show the warning
777-
log.Warnf("Found existing bbolt database file in %s/%s "+
778-
"while using database type %s. Existing data will "+
779-
"NOT be migrated to %s automatically!",
780-
dir, fileName, dbType, dbType)
789+
// warnExistingBoltDBs checks if there is an existing bbolt database in the
790+
// given location and logs a warning if so.
791+
func warnExistingBoltDBs(log btclog.Logger, dbType, dir, fileName string) {
792+
if lnrpc.FileExists(filepath.Join(dir, fileName)) {
793+
log.Warnf("Found existing bbolt database file in %s/%s while "+
794+
"using database type %s. Existing data will NOT be "+
795+
"migrated to %s automatically!", dir, fileName, dbType,
796+
dbType)
781797
}
782-
// If SQLite file exists, don't show the warning
783798
}
784799

785800
// Compile-time constraint to ensure Workers implements the Validator interface.

0 commit comments

Comments
 (0)