Skip to content

Commit 7ec62fe

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 42b6230 commit 7ec62fe

File tree

1 file changed

+85
-4
lines changed

1 file changed

+85
-4
lines changed

config_dev.go

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

88
"github.com/lightninglabs/lightning-terminal/accounts"
9+
"github.com/lightninglabs/lightning-terminal/db"
10+
"github.com/lightningnetwork/lnd/clock"
11+
)
12+
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,
932
)
1033

1134
// DevConfig is a struct that holds the configuration options for a development
@@ -15,23 +38,81 @@ import (
1538
//
1639
// nolint:lll
1740
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"`
1854
}
1955

2056
// Validate checks that all the values set in our DevConfig are valid and uses
2157
// the passed parameters to override any defaults if necessary.
2258
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+
2366
return nil
2467
}
2568

2669
// defaultDevConfig returns a new DevConfig with default values set.
2770
func defaultDevConfig() *DevConfig {
28-
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+
}
2981
}
3082

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

0 commit comments

Comments
 (0)