Skip to content

Commit 381d7d1

Browse files
committed
config: Postgres and SQLite config options for accounts to dev build
In this commit, we expand the dev build's DevConfig options struct with Postgres and SQL backend options that can be used to initialise the account store.
1 parent 7a091fc commit 381d7d1

File tree

1 file changed

+83
-4
lines changed

1 file changed

+83
-4
lines changed

config_dev.go

Lines changed: 83 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,33 +6,112 @@ import (
66
"path/filepath"
77

88
"github.com/lightninglabs/lightning-terminal/accounts"
9+
"github.com/lightninglabs/lightning-terminal/db"
910
"github.com/lightningnetwork/lnd/clock"
1011
)
1112

13+
const (
14+
// DatabaseBackendSqlite is the name of the SQLite database backend.
15+
DatabaseBackendSqlite = "sqlite"
16+
17+
// DatabaseBackendPostgres is the name of the Postgres database backend.
18+
DatabaseBackendPostgres = "postgres"
19+
20+
// DatabaseBackendBbolt is the name of the bbolt database backend.
21+
DatabaseBackendBbolt = "bbolt"
22+
23+
// defaultSqliteDatabaseFileName is the default name of the SQLite
24+
// database file.
25+
defaultSqliteDatabaseFileName = "litd.db"
26+
)
27+
28+
// defaultSqliteDatabasePath is the default path under which we store
29+
// the SQLite database file.
30+
var defaultSqliteDatabasePath = filepath.Join(
31+
DefaultLitDir, DefaultNetwork, defaultSqliteDatabaseFileName,
32+
)
33+
1234
// DevConfig is a struct that holds the configuration options for a development
1335
// environment. The purpose of this struct is to hold config options for
1436
// features not yet available in production. Since our itests are built with
1537
// the dev tag, we can test these features in our itests.
1638
//
1739
// nolint:lll
1840
type DevConfig struct {
41+
// DatabaseBackend is the database backend we will use for storing all
42+
// account related data. While this feature is still in development, we
43+
// include the bbolt type here so that our itests can continue to be
44+
// tested against a bbolt backend. Once the full bbolt to SQL migration
45+
// is complete, however, we will remove the bbolt option.
46+
DatabaseBackend string `long:"databasebackend" description:"The database backend to use for storing all account related data." choice:"bbolt" choice:"sqlite" choice:"postgres"`
47+
48+
// Sqlite holds the configuration options for a SQLite database
49+
// backend.
50+
Sqlite *db.SqliteConfig `group:"sqlite" namespace:"sqlite"`
51+
52+
// Postgres holds the configuration options for a Postgres database
53+
Postgres *db.PostgresConfig `group:"postgres" namespace:"postgres"`
1954
}
2055

2156
// Validate checks that all the values set in our DevConfig are valid and uses
2257
// the passed parameters to override any defaults if necessary.
2358
func (c *DevConfig) Validate(dbDir, network string) error {
59+
// We'll update the database file location if it wasn't set.
60+
if c.Sqlite.DatabaseFileName == defaultSqliteDatabasePath {
61+
c.Sqlite.DatabaseFileName = filepath.Join(
62+
dbDir, network, defaultSqliteDatabaseFileName,
63+
)
64+
}
65+
2466
return nil
2567
}
2668

2769
// defaultDevConfig returns a new DevConfig with default values set.
2870
func defaultDevConfig() *DevConfig {
29-
return &DevConfig{}
71+
return &DevConfig{
72+
Sqlite: &db.SqliteConfig{
73+
DatabaseFileName: defaultSqliteDatabasePath,
74+
},
75+
Postgres: &db.PostgresConfig{
76+
Host: "localhost",
77+
Port: 5432,
78+
MaxOpenConnections: 10,
79+
},
80+
}
3081
}
3182

3283
// NewAccountStore creates a new account store based on the chosen database
3384
// backend.
3485
func NewAccountStore(cfg *Config, clock clock.Clock) (accounts.Store, error) {
35-
return accounts.NewBoltStore(
36-
filepath.Dir(cfg.MacaroonPath), accounts.DBFilename, clock,
37-
)
86+
switch cfg.DatabaseBackend {
87+
case DatabaseBackendSqlite:
88+
// Before we initialize the SQLite store, we'll make sure that
89+
// the directory where we will store the database file exists.
90+
networkDir := filepath.Join(cfg.LitDir, cfg.Network)
91+
err := makeDirectories(networkDir)
92+
if err != nil {
93+
return nil, err
94+
}
95+
96+
sqlStore, err := db.NewSqliteStore(cfg.Sqlite)
97+
if err != nil {
98+
return nil, err
99+
}
100+
101+
return accounts.NewSQLStore(sqlStore.BaseDB, clock), nil
102+
103+
case DatabaseBackendPostgres:
104+
sqlStore, err := db.NewPostgresStore(cfg.Postgres)
105+
if err != nil {
106+
return nil, err
107+
}
108+
109+
return accounts.NewSQLStore(sqlStore.BaseDB, clock), nil
110+
111+
default:
112+
return accounts.NewBoltStore(
113+
filepath.Dir(cfg.MacaroonPath), accounts.DBFilename,
114+
clock,
115+
)
116+
}
38117
}

0 commit comments

Comments
 (0)