Skip to content

[sql-22] session: sessions CRUD #996

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 50 additions & 7 deletions config_dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/lightninglabs/lightning-terminal/accounts"
"github.com/lightninglabs/lightning-terminal/db"
"github.com/lightninglabs/lightning-terminal/session"
"github.com/lightningnetwork/lnd/clock"
)

Expand Down Expand Up @@ -80,14 +81,19 @@ func defaultDevConfig() *DevConfig {
}
}

// NewAccountStore creates a new account store based on the chosen database
// backend.
func NewAccountStore(cfg *Config, clock clock.Clock) (accounts.Store, error) {
// NewStores creates a new stores instance based on the chosen database backend.
func NewStores(cfg *Config, clock clock.Clock) (*stores, error) {
var (
networkDir = filepath.Join(cfg.LitDir, cfg.Network)
acctStore accounts.Store
sessStore session.Store
closeFn func() error
)

switch cfg.DatabaseBackend {
case DatabaseBackendSqlite:
// Before we initialize the SQLite store, we'll make sure that
// the directory where we will store the database file exists.
networkDir := filepath.Join(cfg.LitDir, cfg.Network)
err := makeDirectories(networkDir)
if err != nil {
return nil, err
Expand All @@ -98,20 +104,57 @@ func NewAccountStore(cfg *Config, clock clock.Clock) (accounts.Store, error) {
return nil, err
}

return accounts.NewSQLStore(sqlStore.BaseDB, clock), nil
acctStore = accounts.NewSQLStore(sqlStore.BaseDB, clock)
sessStore = session.NewSQLStore(sqlStore.BaseDB, clock)
closeFn = sqlStore.BaseDB.Close

case DatabaseBackendPostgres:
sqlStore, err := db.NewPostgresStore(cfg.Postgres)
if err != nil {
return nil, err
}

return accounts.NewSQLStore(sqlStore.BaseDB, clock), nil
acctStore = accounts.NewSQLStore(sqlStore.BaseDB, clock)
sessStore = session.NewSQLStore(sqlStore.BaseDB, clock)
closeFn = sqlStore.BaseDB.Close

default:
return accounts.NewBoltStore(
accountStore, err := accounts.NewBoltStore(
filepath.Dir(cfg.MacaroonPath), accounts.DBFilename,
clock,
)
if err != nil {
return nil, err
}

sessionStore, err := session.NewDB(
networkDir, session.DBFilename, clock, accountStore,
)
if err != nil {
return nil, err
}

acctStore = accountStore
sessStore = sessionStore
closeFn = func() error {
var returnErr error
err = accountStore.Close()
if err != nil {
returnErr = err
}

err = sessionStore.Close()
if err != nil {
returnErr = err
}

return returnErr
}
}

return &stores{
accounts: acctStore,
sessions: sessStore,
close: closeFn,
}, nil
}
41 changes: 37 additions & 4 deletions config_prod.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
package terminal

import (
"fmt"
"path/filepath"

"github.com/lightninglabs/lightning-terminal/accounts"
"github.com/lightninglabs/lightning-terminal/session"
"github.com/lightningnetwork/lnd/clock"
)

Expand All @@ -24,10 +26,41 @@ func (c *DevConfig) Validate(_, _ string) error {
return nil
}

// NewAccountStore creates a new account store using the default Bolt backend
// since in production, this is the only backend supported currently.
func NewAccountStore(cfg *Config, clock clock.Clock) (accounts.Store, error) {
return accounts.NewBoltStore(
// NewStores creates a new instance of the stores struct using the default Bolt
// backend since in production, this is currently the only backend supported.
func NewStores(cfg *Config, clock clock.Clock) (*stores, error) {
networkDir := filepath.Join(cfg.LitDir, cfg.Network)

acctStore, err := accounts.NewBoltStore(
filepath.Dir(cfg.MacaroonPath), accounts.DBFilename, clock,
)
if err != nil {
return nil, err
}

sessStore, err := session.NewDB(
networkDir, session.DBFilename, clock, acctStore,
)
if err != nil {
return nil, fmt.Errorf("error creating session BoltStore: %v",
err)
}

return &stores{
accounts: acctStore,
sessions: sessStore,
close: func() error {
var returnErr error
if err := acctStore.Close(); err != nil {
returnErr = fmt.Errorf("error closing "+
"account store: %v", err)
}
if err := sessStore.Close(); err != nil {
returnErr = fmt.Errorf("error closing "+
"session store: %v", err)
}

return returnErr
},
}, nil
}
Loading
Loading