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.
This commit is contained in:
Oliver Gugger 2023-01-13 16:55:02 +02:00 committed by Elle Mouton
parent c89e5b68b0
commit fe5254510e
No known key found for this signature in database
GPG Key ID: D7D916376026F177
2 changed files with 35 additions and 2 deletions

View File

@ -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 "+

View File

@ -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)