Skip to content

Commit 45d36a7

Browse files
multi: use sqldb v2 in litd
This commit updates litd to use the new sqldb v2 package. Note that this with just this commit, litd will not utilize the capabilities of sqldb v2 to run specific post migrations steps (such as migrating the kvdb to SQL). That functionality will be added in later commits. Instead, this commit just focuses on adding support for the new sqldb v2 package, and the functionality of the SQL stores are expected to remain the same as prior to this commit.
1 parent 05d2dd3 commit 45d36a7

19 files changed

+375
-157
lines changed

accounts/sql_migration_test.go

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,17 @@ package accounts
22

33
import (
44
"context"
5-
"database/sql"
65
"fmt"
76
"testing"
87
"time"
98

10-
"github.com/lightninglabs/lightning-terminal/db"
9+
"github.com/lightninglabs/lightning-terminal/db/sqlc"
1110
"github.com/lightningnetwork/lnd/clock"
1211
"github.com/lightningnetwork/lnd/fn"
1312
"github.com/lightningnetwork/lnd/lnrpc"
1413
"github.com/lightningnetwork/lnd/lntypes"
1514
"github.com/lightningnetwork/lnd/lnwire"
16-
"github.com/lightningnetwork/lnd/sqldb"
15+
"github.com/lightningnetwork/lnd/sqldb/v2"
1716
"github.com/stretchr/testify/require"
1817
"golang.org/x/exp/rand"
1918
"pgregory.net/rapid"
@@ -36,7 +35,7 @@ func TestAccountStoreMigration(t *testing.T) {
3635
}
3736

3837
makeSQLDB := func(t *testing.T) (*SQLStore,
39-
*db.TransactionExecutor[SQLQueries]) {
38+
*SQLQueriesExecutor[SQLQueries]) {
4039

4140
testDBStore := NewTestDB(t, clock)
4241

@@ -45,13 +44,9 @@ func TestAccountStoreMigration(t *testing.T) {
4544

4645
baseDB := store.BaseDB
4746

48-
genericExecutor := db.NewTransactionExecutor(
49-
baseDB, func(tx *sql.Tx) SQLQueries {
50-
return baseDB.WithTx(tx)
51-
},
52-
)
47+
queries := sqlc.NewForType(baseDB, baseDB.BackendType)
5348

54-
return store, genericExecutor
49+
return store, NewSQLQueriesExecutor(baseDB, queries)
5550
}
5651

5752
assertMigrationResults := func(t *testing.T, sqlStore *SQLStore,
@@ -343,7 +338,7 @@ func TestAccountStoreMigration(t *testing.T) {
343338
return MigrateAccountStoreToSQL(
344339
ctx, kvStore, tx,
345340
)
346-
},
341+
}, sqldb.NoOpReset,
347342
)
348343
require.NoError(t, err)
349344

accounts/store_sql.go

Lines changed: 44 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/lightningnetwork/lnd/lnrpc"
1717
"github.com/lightningnetwork/lnd/lntypes"
1818
"github.com/lightningnetwork/lnd/lnwire"
19+
"github.com/lightningnetwork/lnd/sqldb/v2"
1920
)
2021

2122
const (
@@ -33,6 +34,8 @@ const (
3334
//
3435
//nolint:lll
3536
type SQLQueries interface {
37+
sqldb.BaseQuerier
38+
3639
AddAccountInvoice(ctx context.Context, arg sqlc.AddAccountInvoiceParams) error
3740
DeleteAccount(ctx context.Context, id int64) error
3841
DeleteAccountPayment(ctx context.Context, arg sqlc.DeleteAccountPaymentParams) error
@@ -53,12 +56,13 @@ type SQLQueries interface {
5356
GetAccountInvoice(ctx context.Context, arg sqlc.GetAccountInvoiceParams) (sqlc.AccountInvoice, error)
5457
}
5558

56-
// BatchedSQLQueries is a version of the SQLQueries that's capable
57-
// of batched database operations.
59+
// BatchedSQLQueries combines the SQLQueries interface with the BatchedTx
60+
// interface, allowing for multiple queries to be executed in single SQL
61+
// transaction.
5862
type BatchedSQLQueries interface {
5963
SQLQueries
6064

61-
db.BatchedTx[SQLQueries]
65+
sqldb.BatchedTx[SQLQueries]
6266
}
6367

6468
// SQLStore represents a storage backend.
@@ -68,19 +72,37 @@ type SQLStore struct {
6872
db BatchedSQLQueries
6973

7074
// BaseDB represents the underlying database connection.
71-
*db.BaseDB
75+
*sqldb.BaseDB
7276

7377
clock clock.Clock
7478
}
7579

76-
// NewSQLStore creates a new SQLStore instance given an open BatchedSQLQueries
77-
// storage backend.
78-
func NewSQLStore(sqlDB *db.BaseDB, clock clock.Clock) *SQLStore {
79-
executor := db.NewTransactionExecutor(
80-
sqlDB, func(tx *sql.Tx) SQLQueries {
81-
return sqlDB.WithTx(tx)
80+
type SQLQueriesExecutor[T sqldb.BaseQuerier] struct {
81+
*sqldb.TransactionExecutor[T]
82+
83+
SQLQueries
84+
}
85+
86+
func NewSQLQueriesExecutor(baseDB *sqldb.BaseDB,
87+
queries *sqlc.Queries) *SQLQueriesExecutor[SQLQueries] {
88+
89+
executor := sqldb.NewTransactionExecutor(
90+
baseDB, func(tx *sql.Tx) SQLQueries {
91+
return queries.WithTx(tx)
8292
},
8393
)
94+
return &SQLQueriesExecutor[SQLQueries]{
95+
TransactionExecutor: executor,
96+
SQLQueries: queries,
97+
}
98+
}
99+
100+
// NewSQLStore creates a new SQLStore instance given an open BatchedSQLQueries
101+
// storage backend.
102+
func NewSQLStore(sqlDB *sqldb.BaseDB, queries *sqlc.Queries,
103+
clock clock.Clock) *SQLStore {
104+
105+
executor := NewSQLQueriesExecutor(sqlDB, queries)
84106

85107
return &SQLStore{
86108
db: executor,
@@ -157,7 +179,7 @@ func (s *SQLStore) NewAccount(ctx context.Context, balance lnwire.MilliSatoshi,
157179
}
158180

159181
return nil
160-
})
182+
}, sqldb.NoOpReset)
161183
if err != nil {
162184
return nil, err
163185
}
@@ -299,7 +321,7 @@ func (s *SQLStore) AddAccountInvoice(ctx context.Context, alias AccountID,
299321
}
300322

301323
return s.markAccountUpdated(ctx, db, acctID)
302-
})
324+
}, sqldb.NoOpReset)
303325
}
304326

305327
func getAccountIDByAlias(ctx context.Context, db SQLQueries, alias AccountID) (
@@ -377,7 +399,7 @@ func (s *SQLStore) UpdateAccountBalanceAndExpiry(ctx context.Context,
377399
}
378400

379401
return s.markAccountUpdated(ctx, db, id)
380-
})
402+
}, sqldb.NoOpReset)
381403
}
382404

383405
// CreditAccount increases the balance of the account with the given alias by
@@ -412,7 +434,7 @@ func (s *SQLStore) CreditAccount(ctx context.Context, alias AccountID,
412434
}
413435

414436
return s.markAccountUpdated(ctx, db, id)
415-
})
437+
}, sqldb.NoOpReset)
416438
}
417439

418440
// DebitAccount decreases the balance of the account with the given alias by the
@@ -453,7 +475,7 @@ func (s *SQLStore) DebitAccount(ctx context.Context, alias AccountID,
453475
}
454476

455477
return s.markAccountUpdated(ctx, db, id)
456-
})
478+
}, sqldb.NoOpReset)
457479
}
458480

459481
// Account retrieves an account from the SQL store and un-marshals it. If the
@@ -475,7 +497,7 @@ func (s *SQLStore) Account(ctx context.Context, alias AccountID) (
475497

476498
account, err = getAndMarshalAccount(ctx, db, id)
477499
return err
478-
})
500+
}, sqldb.NoOpReset)
479501

480502
return account, err
481503
}
@@ -507,7 +529,7 @@ func (s *SQLStore) Accounts(ctx context.Context) ([]*OffChainBalanceAccount,
507529
}
508530

509531
return nil
510-
})
532+
}, sqldb.NoOpReset)
511533

512534
return accounts, err
513535
}
@@ -524,7 +546,7 @@ func (s *SQLStore) RemoveAccount(ctx context.Context, alias AccountID) error {
524546
}
525547

526548
return db.DeleteAccount(ctx, id)
527-
})
549+
}, sqldb.NoOpReset)
528550
}
529551

530552
// UpsertAccountPayment updates or inserts a payment entry for the given
@@ -634,7 +656,7 @@ func (s *SQLStore) UpsertAccountPayment(ctx context.Context, alias AccountID,
634656
}
635657

636658
return s.markAccountUpdated(ctx, db, id)
637-
})
659+
}, sqldb.NoOpReset)
638660
}
639661

640662
// DeleteAccountPayment removes a payment entry from the account with the given
@@ -677,7 +699,7 @@ func (s *SQLStore) DeleteAccountPayment(ctx context.Context, alias AccountID,
677699
}
678700

679701
return s.markAccountUpdated(ctx, db, id)
680-
})
702+
}, sqldb.NoOpReset)
681703
}
682704

683705
// LastIndexes returns the last invoice add and settle index or
@@ -704,7 +726,7 @@ func (s *SQLStore) LastIndexes(ctx context.Context) (uint64, uint64, error) {
704726
}
705727

706728
return err
707-
})
729+
}, sqldb.NoOpReset)
708730

709731
return uint64(addIndex), uint64(settleIndex), err
710732
}
@@ -729,7 +751,7 @@ func (s *SQLStore) StoreLastIndexes(ctx context.Context, addIndex,
729751
Name: settleIndexName,
730752
Value: int64(settleIndex),
731753
})
732-
})
754+
}, sqldb.NoOpReset)
733755
}
734756

735757
// Close closes the underlying store.

accounts/test_sql.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,20 @@ package accounts
55
import (
66
"testing"
77

8-
"github.com/lightninglabs/lightning-terminal/db"
8+
"github.com/lightninglabs/lightning-terminal/db/sqlc"
99
"github.com/lightningnetwork/lnd/clock"
10+
"github.com/lightningnetwork/lnd/sqldb/v2"
1011
"github.com/stretchr/testify/require"
1112
)
1213

1314
// createStore is a helper function that creates a new SQLStore and ensure that
1415
// it is closed when during the test cleanup.
15-
func createStore(t *testing.T, sqlDB *db.BaseDB, clock clock.Clock) *SQLStore {
16-
store := NewSQLStore(sqlDB, clock)
16+
func createStore(t *testing.T, sqlDB *sqldb.BaseDB,
17+
clock clock.Clock) *SQLStore {
18+
19+
queries := sqlc.NewForType(sqlDB, sqlDB.BackendType)
20+
21+
store := NewSQLStore(sqlDB, queries, clock)
1722
t.Cleanup(func() {
1823
require.NoError(t, store.Close())
1924
})

accounts/test_sqlite.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
"github.com/lightninglabs/lightning-terminal/db"
1010
"github.com/lightningnetwork/lnd/clock"
11+
"github.com/lightningnetwork/lnd/sqldb/v2"
1112
)
1213

1314
// ErrDBClosed is an error that is returned when a database operation is
@@ -16,15 +17,18 @@ var ErrDBClosed = errors.New("database is closed")
1617

1718
// NewTestDB is a helper function that creates an SQLStore database for testing.
1819
func NewTestDB(t *testing.T, clock clock.Clock) Store {
19-
return createStore(t, db.NewTestSqliteDB(t).BaseDB, clock)
20+
return createStore(
21+
t, sqldb.NewTestSqliteDB(t, db.LitdMigrationStreams).BaseDB,
22+
clock,
23+
)
2024
}
2125

2226
// NewTestDBFromPath is a helper function that creates a new SQLStore with a
2327
// connection to an existing SQL database for testing.
2428
func NewTestDBFromPath(t *testing.T, dbPath string,
2529
clock clock.Clock) Store {
2630

27-
return createStore(
28-
t, db.NewTestSqliteDbHandleFromPath(t, dbPath).BaseDB, clock,
29-
)
31+
tDb := sqldb.NewTestSqliteDBFromPath(t, dbPath, db.LitdMigrationStreams)
32+
33+
return createStore(t, tDb.BaseDB, clock)
3034
}

config_dev.go

Lines changed: 59 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@ package terminal
44

55
import (
66
"fmt"
7+
"github.com/lightninglabs/lightning-terminal/db/sqlc"
78
"path/filepath"
89

910
"github.com/lightninglabs/lightning-terminal/accounts"
1011
"github.com/lightninglabs/lightning-terminal/db"
1112
"github.com/lightninglabs/lightning-terminal/firewalldb"
1213
"github.com/lightninglabs/lightning-terminal/session"
1314
"github.com/lightningnetwork/lnd/clock"
15+
"github.com/lightningnetwork/lnd/sqldb/v2"
1416
)
1517

1618
const (
@@ -101,29 +103,78 @@ func NewStores(cfg *Config, clock clock.Clock) (*stores, error) {
101103
return stores, err
102104
}
103105

104-
sqlStore, err := db.NewSqliteStore(cfg.Sqlite)
106+
sqlStore, err := sqldb.NewSqliteStore(&sqldb.SqliteConfig{
107+
SkipMigrations: cfg.Sqlite.SkipMigrations,
108+
SkipMigrationDbBackup: cfg.Sqlite.SkipMigrationDbBackup,
109+
}, cfg.Sqlite.DatabaseFileName)
105110
if err != nil {
106111
return stores, err
107112
}
108113

109-
acctStore := accounts.NewSQLStore(sqlStore.BaseDB, clock)
110-
sessStore := session.NewSQLStore(sqlStore.BaseDB, clock)
111-
firewallStore := firewalldb.NewSQLDB(sqlStore.BaseDB, clock)
114+
if !cfg.Sqlite.SkipMigrations {
115+
err = sqldb.ApplyAllMigrations(
116+
sqlStore, db.LitdMigrationStreams,
117+
)
118+
if err != nil {
119+
return stores, fmt.Errorf("error applying "+
120+
"migrations to SQLlite store: %w", err,
121+
)
122+
}
123+
}
124+
125+
queries := sqlc.NewForType(sqlStore, sqlStore.BackendType)
126+
127+
acctStore := accounts.NewSQLStore(
128+
sqlStore.BaseDB, queries, clock,
129+
)
130+
sessStore := session.NewSQLStore(
131+
sqlStore.BaseDB, queries, clock,
132+
)
133+
firewallStore := firewalldb.NewSQLDB(
134+
sqlStore.BaseDB, queries, clock,
135+
)
112136

113137
stores.accounts = acctStore
114138
stores.sessions = sessStore
115139
stores.firewall = firewalldb.NewDB(firewallStore)
116140
stores.closeFns["sqlite"] = sqlStore.BaseDB.Close
117141

118142
case DatabaseBackendPostgres:
119-
sqlStore, err := db.NewPostgresStore(cfg.Postgres)
143+
sqlStore, err := sqldb.NewPostgresStore(&sqldb.PostgresConfig{
144+
Dsn: cfg.Postgres.DSN(false),
145+
MaxOpenConnections: cfg.Postgres.MaxOpenConnections,
146+
MaxIdleConnections: cfg.Postgres.MaxIdleConnections,
147+
ConnMaxLifetime: cfg.Postgres.ConnMaxLifetime,
148+
ConnMaxIdleTime: cfg.Postgres.ConnMaxIdleTime,
149+
RequireSSL: cfg.Postgres.RequireSSL,
150+
SkipMigrations: cfg.Postgres.SkipMigrations,
151+
})
120152
if err != nil {
121153
return stores, err
122154
}
123155

124-
acctStore := accounts.NewSQLStore(sqlStore.BaseDB, clock)
125-
sessStore := session.NewSQLStore(sqlStore.BaseDB, clock)
126-
firewallStore := firewalldb.NewSQLDB(sqlStore.BaseDB, clock)
156+
if !cfg.Sqlite.SkipMigrations {
157+
err = sqldb.ApplyAllMigrations(
158+
sqlStore, db.LitdMigrationStreams,
159+
)
160+
if err != nil {
161+
return stores, fmt.Errorf("error applying "+
162+
"migrations to Postgres store: %w", err,
163+
)
164+
}
165+
}
166+
167+
queries := sqlc.NewForType(sqlStore, sqlStore.BackendType)
168+
169+
acctStore := accounts.NewSQLStore(
170+
sqlStore.BaseDB, queries, clock,
171+
)
172+
sessStore := session.NewSQLStore(
173+
sqlStore.BaseDB, queries, clock,
174+
)
175+
firewallStore := firewalldb.NewSQLDB(
176+
sqlStore.BaseDB, queries, clock,
177+
)
127178

128179
stores.accounts = acctStore
129180
stores.sessions = sessStore

0 commit comments

Comments
 (0)