From ac28fbde61afcbef745e40404418b984c7469e5d Mon Sep 17 00:00:00 2001
From: Dhiren-Mhatre <kp064669@gmail.com>
Date: Mon, 21 Apr 2025 00:48:11 +0530
Subject: [PATCH 1/2] lncfg: don't warn user about existing bbolt DB if SQLite
 files exist

Signed-off-by: Dhiren-Mhatre <kp064669@gmail.com>
---
 docs/release-notes/release-notes-0.19.0.md |  3 ++
 lncfg/db.go                                | 46 +++++++++++++++++++---
 2 files changed, 44 insertions(+), 5 deletions(-)

diff --git a/docs/release-notes/release-notes-0.19.0.md b/docs/release-notes/release-notes-0.19.0.md
index ee36d17d11..116e7ddc66 100644
--- a/docs/release-notes/release-notes-0.19.0.md
+++ b/docs/release-notes/release-notes-0.19.0.md
@@ -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
 
diff --git a/lncfg/db.go b/lncfg/db.go
index b45ee6dab2..c1eea7449c 100644
--- a/lncfg/db.go
+++ b/lncfg/db.go
@@ -738,12 +738,48 @@ func (db *DB) GetBackends(ctx context.Context, chanDBPath,
 // 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) {
-	if lnrpc.FileExists(filepath.Join(dir, fileName)) {
-		log.Warnf("Found existing bbolt database file in %s/%s while "+
-			"using database type %s. Existing data will NOT be "+
-			"migrated to %s automatically!", dir, fileName, dbType,
-			dbType)
+	// Check if the bbolt file exists
+	bboltPath := filepath.Join(dir, fileName)
+	if !lnrpc.FileExists(bboltPath) {
+		// No bbolt file, no need for warning
+		return
 	}
+
+	// Determine the corresponding SQLite file name based on the bbolt file
+	var sqliteFileName string
+	switch fileName {
+	case WalletDBName:
+		sqliteFileName = SqliteChainDBName
+	case ChannelDBName:
+		sqliteFileName = SqliteChannelDBName
+	case MacaroonDBName:
+		sqliteFileName = SqliteChainDBName
+	case DecayedLogDbName:
+		sqliteFileName = SqliteChannelDBName
+	case TowerClientDBName:
+		sqliteFileName = SqliteChannelDBName
+	case TowerServerDBName:
+		sqliteFileName = SqliteTowerDBName
+	default:
+		// For any other file types, still show the warning
+		log.Warnf("Found existing bbolt database file in %s/%s "+
+			"while using database type %s. Existing data will "+
+			"NOT be migrated to %s automatically!",
+			dir, fileName, dbType, dbType)
+
+		return
+	}
+
+	// Check if the corresponding SQLite file exists
+	sqlitePath := filepath.Join(dir, sqliteFileName)
+	if !lnrpc.FileExists(sqlitePath) {
+		// SQLite file doesn't exist, show the warning
+		log.Warnf("Found existing bbolt database file in %s/%s "+
+			"while using database type %s. Existing data will "+
+			"NOT be migrated to %s automatically!",
+			dir, fileName, dbType, dbType)
+	}
+	// If SQLite file exists, don't show the warning
 }
 
 // Compile-time constraint to ensure Workers implements the Validator interface.

From 4db2891d32f0cbcda20e68e710f15961386fa145 Mon Sep 17 00:00:00 2001
From: Dhiren-Mhatre <kp064669@gmail.com>
Date: Mon, 28 Apr 2025 19:52:29 +0530
Subject: [PATCH 2/2] lncfg: add checks for macaroons.db and sphinxreplay.db
 files

Signed-off-by: Dhiren-Mhatre <kp064669@gmail.com>
---
 lncfg/db.go | 111 +++++++++++++++++++++++++++++-----------------------
 1 file changed, 63 insertions(+), 48 deletions(-)

diff --git a/lncfg/db.go b/lncfg/db.go
index c1eea7449c..f087662dc9 100644
--- a/lncfg/db.go
+++ b/lncfg/db.go
@@ -3,6 +3,7 @@ package lncfg
 import (
 	"context"
 	"fmt"
+	"os"
 	"path"
 	"path/filepath"
 	"time"
@@ -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.
@@ -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
 
@@ -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
 
@@ -735,51 +762,39 @@ func (db *DB) GetBackends(ctx context.Context, chanDBPath,
 	}, nil
 }
 
-// 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) {
-	// Check if the bbolt file exists
-	bboltPath := filepath.Join(dir, fileName)
-	if !lnrpc.FileExists(bboltPath) {
-		// No bbolt file, no need for warning
-		return
+// 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
 	}
 
-	// Determine the corresponding SQLite file name based on the bbolt file
-	var sqliteFileName string
-	switch fileName {
-	case WalletDBName:
-		sqliteFileName = SqliteChainDBName
-	case ChannelDBName:
-		sqliteFileName = SqliteChannelDBName
-	case MacaroonDBName:
-		sqliteFileName = SqliteChainDBName
-	case DecayedLogDbName:
-		sqliteFileName = SqliteChannelDBName
-	case TowerClientDBName:
-		sqliteFileName = SqliteChannelDBName
-	case TowerServerDBName:
-		sqliteFileName = SqliteTowerDBName
-	default:
-		// For any other file types, still show the warning
-		log.Warnf("Found existing bbolt database file in %s/%s "+
-			"while using database type %s. Existing data will "+
-			"NOT be migrated to %s automatically!",
-			dir, fileName, dbType, dbType)
-
-		return
+	// 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
+}
 
-	// Check if the corresponding SQLite file exists
-	sqlitePath := filepath.Join(dir, sqliteFileName)
-	if !lnrpc.FileExists(sqlitePath) {
-		// SQLite file doesn't exist, show the warning
-		log.Warnf("Found existing bbolt database file in %s/%s "+
-			"while using database type %s. Existing data will "+
-			"NOT be migrated to %s automatically!",
-			dir, fileName, dbType, dbType)
+// 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) {
+	if lnrpc.FileExists(filepath.Join(dir, fileName)) {
+		log.Warnf("Found existing bbolt database file in %s/%s while "+
+			"using database type %s. Existing data will NOT be "+
+			"migrated to %s automatically!", dir, fileName, dbType,
+			dbType)
 	}
-	// If SQLite file exists, don't show the warning
 }
 
 // Compile-time constraint to ensure Workers implements the Validator interface.