Skip to content

Commit 75f8b77

Browse files
accounts: use sqldb/v2 in accounts package
Update the accounts package to use the `sqldb/v2` package instead of the older version.
1 parent ae8370a commit 75f8b77

File tree

6 files changed

+142
-51
lines changed

6 files changed

+142
-51
lines changed

accounts/sql_migration_test.go

Lines changed: 11 additions & 12 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,
@@ -337,13 +332,17 @@ func TestAccountStoreMigration(t *testing.T) {
337332
}
338333

339334
// Perform the migration.
335+
//
336+
// TODO(viktor): remove sqldb.MigrationTxOptions once
337+
// sqldb v2 is based on the latest version of lnd/sqldb.
338+
var opts sqldb.MigrationTxOptions
340339
err = txEx.ExecTx(
341-
ctx, sqldb.WriteTxOpt(),
340+
ctx, &opts,
342341
func(tx SQLQueries) error {
343342
return MigrateAccountStoreToSQL(
344343
ctx, kvStore.db, tx,
345344
)
346-
},
345+
}, sqldb.NoOpReset,
347346
)
348347
require.NoError(t, err)
349348

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_postgres.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ var ErrDBClosed = errors.New("database is closed")
1616

1717
// NewTestDB is a helper function that creates an SQLStore database for testing.
1818
func NewTestDB(t *testing.T, clock clock.Clock) Store {
19-
return createStore(t, db.NewTestPostgresDB(t).BaseDB, clock)
19+
return createStore(t, db.NewTestPostgresV2DB(t).BaseDB, clock)
2020
}
2121

2222
// NewTestDBFromPath is a helper function that creates a new SQLStore with a
2323
// connection to an existing postgres database for testing.
2424
func NewTestDBFromPath(t *testing.T, dbPath string,
2525
clock clock.Clock) Store {
2626

27-
return createStore(t, db.NewTestPostgresDB(t).BaseDB, clock)
27+
return createStore(t, db.NewTestPostgresV2DB(t).BaseDB, clock)
2828
}

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
}

0 commit comments

Comments
 (0)