Skip to content

Commit aec2da6

Browse files
session: use sqldb v2 in session package
1 parent a79e039 commit aec2da6

File tree

6 files changed

+77
-44
lines changed

6 files changed

+77
-44
lines changed

config_dev.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,9 @@ func NewStores(cfg *Config, clock clock.Clock) (*stores, error) {
134134
acctStore := accounts.NewSQLStore(
135135
sqlStore.BaseDB, queries, clock,
136136
)
137-
sessStore := session.NewSQLStore(legacySqlStore.BaseDB, clock)
137+
sessStore := session.NewSQLStore(
138+
sqlStore.BaseDB, queries, clock,
139+
)
138140
firewallStore := firewalldb.NewSQLDB(
139141
legacySqlStore.BaseDB, clock,
140142
)
@@ -181,7 +183,9 @@ func NewStores(cfg *Config, clock clock.Clock) (*stores, error) {
181183
acctStore := accounts.NewSQLStore(
182184
sqlStore.BaseDB, queries, clock,
183185
)
184-
sessStore := session.NewSQLStore(legacySqlStore.BaseDB, clock)
186+
sessStore := session.NewSQLStore(
187+
sqlStore.BaseDB, queries, clock,
188+
)
185189
firewallStore := firewalldb.NewSQLDB(
186190
legacySqlStore.BaseDB, clock,
187191
)

session/sql_migration_test.go

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,16 @@ package session
22

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

109
"github.com/lightninglabs/lightning-terminal/accounts"
11-
"github.com/lightninglabs/lightning-terminal/db"
10+
"github.com/lightninglabs/lightning-terminal/db/sqlc"
1211
"github.com/lightningnetwork/lnd/clock"
1312
"github.com/lightningnetwork/lnd/lnwire"
1413
"github.com/lightningnetwork/lnd/macaroons"
15-
"github.com/lightningnetwork/lnd/sqldb"
14+
"github.com/lightningnetwork/lnd/sqldb/v2"
1615
"github.com/stretchr/testify/require"
1716
"go.etcd.io/bbolt"
1817
"golang.org/x/exp/rand"
@@ -38,7 +37,7 @@ func TestSessionsStoreMigration(t *testing.T) {
3837
}
3938

4039
makeSQLDB := func(t *testing.T, acctStore accounts.Store) (*SQLStore,
41-
*db.TransactionExecutor[SQLQueries]) {
40+
*SQLQueriesExecutor[SQLQueries]) {
4241

4342
// Create a sql store with a linked account store.
4443
testDBStore := NewTestDBWithAccounts(t, clock, acctStore)
@@ -48,13 +47,9 @@ func TestSessionsStoreMigration(t *testing.T) {
4847

4948
baseDB := store.BaseDB
5049

51-
genericExecutor := db.NewTransactionExecutor(
52-
baseDB, func(tx *sql.Tx) SQLQueries {
53-
return baseDB.WithTx(tx)
54-
},
55-
)
50+
queries := sqlc.NewForType(baseDB, baseDB.BackendType)
5651

57-
return store, genericExecutor
52+
return store, NewSQLQueriesExecutor(baseDB, queries)
5853
}
5954

6055
// assertMigrationResults asserts that the sql store contains the
@@ -369,13 +364,16 @@ func TestSessionsStoreMigration(t *testing.T) {
369364
// migration.
370365
sqlStore, txEx := makeSQLDB(t, accountStore)
371366

367+
// TODO(viktor): remove sqldb.MigrationTxOptions once
368+
// sqldb v2 is based on the latest version of lnd/sqldb.
369+
var opts sqldb.MigrationTxOptions
372370
err = txEx.ExecTx(
373-
ctx, sqldb.WriteTxOpt(),
371+
ctx, &opts,
374372
func(tx SQLQueries) error {
375373
return MigrateSessionStoreToSQL(
376374
ctx, kvStore.DB, tx,
377375
)
378-
},
376+
}, sqldb.NoOpReset,
379377
)
380378
require.NoError(t, err)
381379

session/sql_store.go

Lines changed: 43 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,16 @@ import (
1414
"github.com/lightninglabs/lightning-terminal/db/sqlc"
1515
"github.com/lightningnetwork/lnd/clock"
1616
"github.com/lightningnetwork/lnd/fn"
17+
"github.com/lightningnetwork/lnd/sqldb/v2"
1718
"gopkg.in/macaroon-bakery.v2/bakery"
1819
"gopkg.in/macaroon.v2"
1920
)
2021

2122
// SQLQueries is a subset of the sqlc.Queries interface that can be used to
2223
// interact with session related tables.
2324
type SQLQueries interface {
25+
sqldb.BaseQuerier
26+
2427
GetAliasBySessionID(ctx context.Context, id int64) ([]byte, error)
2528
GetSessionByID(ctx context.Context, id int64) (sqlc.Session, error)
2629
GetSessionsInGroup(ctx context.Context, groupID sql.NullInt64) ([]sqlc.Session, error)
@@ -51,12 +54,13 @@ type SQLQueries interface {
5154

5255
var _ Store = (*SQLStore)(nil)
5356

54-
// BatchedSQLQueries is a version of the SQLQueries that's capable of batched
55-
// database operations.
57+
// BatchedSQLQueries combines the SQLQueries interface with the BatchedTx
58+
// interface, allowing for multiple queries to be executed in single SQL
59+
// transaction.
5660
type BatchedSQLQueries interface {
5761
SQLQueries
5862

59-
db.BatchedTx[SQLQueries]
63+
sqldb.BatchedTx[SQLQueries]
6064
}
6165

6266
// SQLStore represents a storage backend.
@@ -66,19 +70,37 @@ type SQLStore struct {
6670
db BatchedSQLQueries
6771

6872
// BaseDB represents the underlying database connection.
69-
*db.BaseDB
73+
*sqldb.BaseDB
7074

7175
clock clock.Clock
7276
}
7377

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

83105
return &SQLStore{
84106
db: executor,
@@ -281,7 +303,7 @@ func (s *SQLStore) NewSession(ctx context.Context, label string, typ Type,
281303
}
282304

283305
return nil
284-
})
306+
}, sqldb.NoOpReset)
285307
if err != nil {
286308
mappedSQLErr := db.MapSQLError(err)
287309
var uniqueConstraintErr *db.ErrSqlUniqueConstraintViolation
@@ -325,7 +347,7 @@ func (s *SQLStore) ListSessionsByType(ctx context.Context, t Type) ([]*Session,
325347
}
326348

327349
return nil
328-
})
350+
}, sqldb.NoOpReset)
329351

330352
return sessions, err
331353
}
@@ -358,7 +380,7 @@ func (s *SQLStore) ListSessionsByState(ctx context.Context, state State) (
358380
}
359381

360382
return nil
361-
})
383+
}, sqldb.NoOpReset)
362384

363385
return sessions, err
364386
}
@@ -417,7 +439,7 @@ func (s *SQLStore) ShiftState(ctx context.Context, alias ID, dest State) error {
417439
State: int16(dest),
418440
},
419441
)
420-
})
442+
}, sqldb.NoOpReset)
421443
}
422444

423445
// DeleteReservedSessions deletes all sessions that are in the StateReserved
@@ -428,7 +450,7 @@ func (s *SQLStore) DeleteReservedSessions(ctx context.Context) error {
428450
var writeTxOpts db.QueriesTxOptions
429451
return s.db.ExecTx(ctx, &writeTxOpts, func(db SQLQueries) error {
430452
return db.DeleteSessionsWithState(ctx, int16(StateReserved))
431-
})
453+
}, sqldb.NoOpReset)
432454
}
433455

434456
// GetSessionByLocalPub fetches the session with the given local pub key.
@@ -458,7 +480,7 @@ func (s *SQLStore) GetSessionByLocalPub(ctx context.Context,
458480
}
459481

460482
return nil
461-
})
483+
}, sqldb.NoOpReset)
462484
if err != nil {
463485
return nil, err
464486
}
@@ -491,7 +513,7 @@ func (s *SQLStore) ListAllSessions(ctx context.Context) ([]*Session, error) {
491513
}
492514

493515
return nil
494-
})
516+
}, sqldb.NoOpReset)
495517

496518
return sessions, err
497519
}
@@ -521,7 +543,7 @@ func (s *SQLStore) UpdateSessionRemotePubKey(ctx context.Context, alias ID,
521543
RemotePublicKey: remoteKey,
522544
},
523545
)
524-
})
546+
}, sqldb.NoOpReset)
525547
}
526548

527549
// getSqlUnusedAliasAndKeyPair can be used to generate a new, unused, local
@@ -576,7 +598,7 @@ func (s *SQLStore) GetSession(ctx context.Context, alias ID) (*Session, error) {
576598
}
577599

578600
return nil
579-
})
601+
}, sqldb.NoOpReset)
580602

581603
return sess, err
582604
}
@@ -617,7 +639,7 @@ func (s *SQLStore) GetGroupID(ctx context.Context, sessionID ID) (ID, error) {
617639
legacyGroupID, err = IDFromBytes(legacyGroupIDB)
618640

619641
return err
620-
})
642+
}, sqldb.NoOpReset)
621643
if err != nil {
622644
return ID{}, err
623645
}
@@ -666,7 +688,7 @@ func (s *SQLStore) GetSessionIDs(ctx context.Context, legacyGroupID ID) ([]ID,
666688
}
667689

668690
return nil
669-
})
691+
}, sqldb.NoOpReset)
670692
if err != nil {
671693
return nil, err
672694
}

session/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
}

session/test_sql.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ import (
66
"testing"
77

88
"github.com/lightninglabs/lightning-terminal/accounts"
9-
"github.com/lightninglabs/lightning-terminal/db"
9+
"github.com/lightninglabs/lightning-terminal/db/sqlc"
1010
"github.com/lightningnetwork/lnd/clock"
11+
"github.com/lightningnetwork/lnd/sqldb/v2"
1112
"github.com/stretchr/testify/require"
1213
)
1314

@@ -22,8 +23,12 @@ func NewTestDBWithAccounts(t *testing.T, clock clock.Clock,
2223

2324
// createStore is a helper function that creates a new SQLStore and ensure that
2425
// it is closed when during the test cleanup.
25-
func createStore(t *testing.T, sqlDB *db.BaseDB, clock clock.Clock) *SQLStore {
26-
store := NewSQLStore(sqlDB, clock)
26+
func createStore(t *testing.T, sqlDB *sqldb.BaseDB,
27+
clock clock.Clock) *SQLStore {
28+
29+
queries := sqlc.NewForType(sqlDB, sqlDB.BackendType)
30+
31+
store := NewSQLStore(sqlDB, queries, clock)
2732
t.Cleanup(func() {
2833
require.NoError(t, store.Close())
2934
})

session/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 sqlite 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)