Skip to content

Commit

Permalink
multi: warn user if they try to switch db types
Browse files Browse the repository at this point in the history
Warn a user if they attempt to initialise a new db type (sqlite or
postgres) if an old bbolt db file is found.
  • Loading branch information
guggero authored and ellemouton committed Jan 25, 2023
1 parent c89e5b6 commit fe52545
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
2 changes: 1 addition & 1 deletion config_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 "+
Expand Down
35 changes: 34 additions & 1 deletion lncfg/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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)

0 comments on commit fe52545

Please sign in to comment.