From fe5254510e5cfdd8283708014181acf490aab95d Mon Sep 17 00:00:00 2001 From: Oliver Gugger Date: Fri, 13 Jan 2023 16:55:02 +0200 Subject: [PATCH] multi: warn user if they try to switch db types Warn a user if they attempt to initialise a new db type (sqlite or postgres) if an old bbolt db file is found. --- config_builder.go | 2 +- lncfg/db.go | 35 ++++++++++++++++++++++++++++++++++- 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/config_builder.go b/config_builder.go index a257153e5..db4e7f194 100644 --- a/config_builder.go +++ b/config_builder.go @@ -830,7 +830,7 @@ func (d *DefaultDatabaseBuilder) BuildDatabase( cfg.Watchtower.TowerDir, cfg.registeredChains.PrimaryChain().String(), lncfg.NormalizeNetwork(cfg.ActiveNetParams.Name), - ), cfg.WtClient.Active, cfg.Watchtower.Active, + ), cfg.WtClient.Active, cfg.Watchtower.Active, d.logger, ) if err != nil { return nil, nil, fmt.Errorf("unable to obtain database "+ diff --git a/lncfg/db.go b/lncfg/db.go index 4d73cb231..e446553ae 100644 --- a/lncfg/db.go +++ b/lncfg/db.go @@ -3,13 +3,16 @@ package lncfg import ( "context" "fmt" + "path/filepath" "time" + "github.com/btcsuite/btclog" "github.com/lightningnetwork/lnd/kvdb" "github.com/lightningnetwork/lnd/kvdb/etcd" "github.com/lightningnetwork/lnd/kvdb/postgres" "github.com/lightningnetwork/lnd/kvdb/sqlbase" "github.com/lightningnetwork/lnd/kvdb/sqlite" + "github.com/lightningnetwork/lnd/lnrpc" "github.com/lightningnetwork/lnd/lnwallet/btcwallet" ) @@ -221,7 +224,8 @@ type DatabaseBackends struct { // GetBackends returns a set of kvdb.Backends as set in the DB config. func (db *DB) GetBackends(ctx context.Context, chanDBPath, walletDBPath, towerServerDBPath string, towerClientEnabled, - towerServerEnabled bool) (*DatabaseBackends, error) { + towerServerEnabled bool, logger btclog.Logger) (*DatabaseBackends, + error) { // We keep track of all the kvdb backends we actually open and return a // reference to their close function so they can be cleaned up properly @@ -396,6 +400,15 @@ func (db *DB) GetBackends(ctx context.Context, chanDBPath, } closeFuncs[NSWalletDB] = postgresWalletBackend.Close + // Warn if the user is trying to switch over to a Postgres DB + // while there is a wallet or channel bbolt DB still present. + warnExistingBoltDBs( + logger, "postgres", walletDBPath, WalletDBName, + ) + warnExistingBoltDBs( + logger, "postgres", chanDBPath, ChannelDBName, + ) + returnEarly = false return &DatabaseBackends{ @@ -489,6 +502,15 @@ func (db *DB) GetBackends(ctx context.Context, chanDBPath, } closeFuncs[NSWalletDB] = sqliteWalletBackend.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, + ) + returnEarly = false return &DatabaseBackends{ @@ -615,5 +637,16 @@ 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) { + 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) + } +} + // Compile-time constraint to ensure Workers implements the Validator interface. var _ Validator = (*DB)(nil)