From 252d1206b683892bbb953721b8ad3cef14cf2945 Mon Sep 17 00:00:00 2001 From: Viktor Torstensson Date: Tue, 22 Jul 2025 12:21:06 +0200 Subject: [PATCH] multi: use `sqldb/v2` in session package Update the session package to use the `sqldb/v2` package instead of the older version. --- config_dev.go | 8 +++- firewalldb/sql_migration_test.go | 12 +++--- session/sql_migration.go | 2 +- session/sql_migration_test.go | 17 +++----- session/sql_store.go | 66 +++++++++++++++++++++----------- session/test_postgres.go | 4 +- session/test_sql.go | 11 ++++-- session/test_sqlite.go | 12 ++++-- 8 files changed, 82 insertions(+), 50 deletions(-) diff --git a/config_dev.go b/config_dev.go index 857a8662..aa0824d0 100644 --- a/config_dev.go +++ b/config_dev.go @@ -134,7 +134,9 @@ func NewStores(cfg *Config, clock clock.Clock) (*stores, error) { acctStore := accounts.NewSQLStore( sqlStore.BaseDB, queries, clock, ) - sessStore := session.NewSQLStore(legacySqlStore.BaseDB, clock) + sessStore := session.NewSQLStore( + sqlStore.BaseDB, queries, clock, + ) firewallStore := firewalldb.NewSQLDB( legacySqlStore.BaseDB, clock, ) @@ -181,7 +183,9 @@ func NewStores(cfg *Config, clock clock.Clock) (*stores, error) { acctStore := accounts.NewSQLStore( sqlStore.BaseDB, queries, clock, ) - sessStore := session.NewSQLStore(legacySqlStore.BaseDB, clock) + sessStore := session.NewSQLStore( + sqlStore.BaseDB, queries, clock, + ) firewallStore := firewalldb.NewSQLDB( legacySqlStore.BaseDB, clock, ) diff --git a/firewalldb/sql_migration_test.go b/firewalldb/sql_migration_test.go index a5e42cd4..29efef9c 100644 --- a/firewalldb/sql_migration_test.go +++ b/firewalldb/sql_migration_test.go @@ -542,8 +542,6 @@ func TestFirewallDBMigration(t *testing.T) { sessionsStore := session.NewTestDBWithAccounts( t, clock, accountStore, ) - sessSQLStore, ok := sessionsStore.(*session.SQLStore) - require.True(t, ok) // Create a new firewall store to populate with test // data. @@ -576,7 +574,7 @@ func TestFirewallDBMigration(t *testing.T) { return MigrateFirewallDBToSQL( ctx, firewallStore.DB, tx, qs, - sessSQLStore, + qs, rootKeyStore.getAllRootKeys(), ) }, @@ -1151,6 +1149,8 @@ func createPrivacyPairs(t *testing.T, ctx context.Context, sessSQLStore, ok := sessionStore.(*session.SQLStore) require.True(t, ok) + queries := sqlc.NewForType(sessSQLStore, sessSQLStore.BackendType) + for i := range numSessions { sess, err := sessionStore.NewSession( ctx, fmt.Sprintf("session-%d", i), @@ -1160,7 +1160,7 @@ func createPrivacyPairs(t *testing.T, ctx context.Context, require.NoError(t, err) groupID := sess.GroupID - sqlGroupID, err := sessSQLStore.GetSessionIDByAlias( + sqlGroupID, err := queries.GetSessionIDByAlias( ctx, groupID[:], ) require.NoError(t, err) @@ -1206,6 +1206,8 @@ func randomPrivacyPairs(t *testing.T, ctx context.Context, sessSQLStore, ok := sessionStore.(*session.SQLStore) require.True(t, ok) + queries := sqlc.NewForType(sessSQLStore, sessSQLStore.BackendType) + for i := range numSessions { sess, err := sessionStore.NewSession( ctx, fmt.Sprintf("session-%d", i), @@ -1215,7 +1217,7 @@ func randomPrivacyPairs(t *testing.T, ctx context.Context, require.NoError(t, err) groupID := sess.GroupID - sqlGroupID, err := sessSQLStore.GetSessionIDByAlias( + sqlGroupID, err := queries.GetSessionIDByAlias( ctx, groupID[:], ) require.NoError(t, err) diff --git a/session/sql_migration.go b/session/sql_migration.go index 676cd5b3..eeb583ff 100644 --- a/session/sql_migration.go +++ b/session/sql_migration.go @@ -14,7 +14,7 @@ import ( "github.com/lightninglabs/lightning-terminal/accounts" "github.com/lightninglabs/lightning-terminal/db/sqlc" "github.com/lightningnetwork/lnd/fn" - "github.com/lightningnetwork/lnd/sqldb" + "github.com/lightningnetwork/lnd/sqldb/v2" "github.com/pmezard/go-difflib/difflib" "go.etcd.io/bbolt" ) diff --git a/session/sql_migration_test.go b/session/sql_migration_test.go index 0e79b3ba..1026fbbb 100644 --- a/session/sql_migration_test.go +++ b/session/sql_migration_test.go @@ -2,17 +2,16 @@ package session import ( "context" - "database/sql" "fmt" "testing" "time" "github.com/lightninglabs/lightning-terminal/accounts" - "github.com/lightninglabs/lightning-terminal/db" + "github.com/lightninglabs/lightning-terminal/db/sqlc" "github.com/lightningnetwork/lnd/clock" "github.com/lightningnetwork/lnd/lnwire" "github.com/lightningnetwork/lnd/macaroons" - "github.com/lightningnetwork/lnd/sqldb" + "github.com/lightningnetwork/lnd/sqldb/v2" "github.com/stretchr/testify/require" "go.etcd.io/bbolt" "golang.org/x/exp/rand" @@ -38,7 +37,7 @@ func TestSessionsStoreMigration(t *testing.T) { } makeSQLDB := func(t *testing.T, acctStore accounts.Store) (*SQLStore, - *db.TransactionExecutor[SQLQueries]) { + *sqlQueriesExecutor[SQLQueries]) { // Create a sql store with a linked account store. testDBStore := NewTestDBWithAccounts(t, clock, acctStore) @@ -48,13 +47,9 @@ func TestSessionsStoreMigration(t *testing.T) { baseDB := store.BaseDB - genericExecutor := db.NewTransactionExecutor( - baseDB, func(tx *sql.Tx) SQLQueries { - return baseDB.WithTx(tx) - }, - ) + queries := sqlc.NewForType(baseDB, baseDB.BackendType) - return store, genericExecutor + return store, newSQLQueriesExecutor(baseDB, queries) } // assertMigrationResults asserts that the sql store contains the @@ -597,7 +592,7 @@ func TestSessionsStoreMigration(t *testing.T) { return MigrateSessionStoreToSQL( ctx, kvStore.DB, tx, ) - }, + }, sqldb.NoOpReset, ) require.NoError(t, err) diff --git a/session/sql_store.go b/session/sql_store.go index a9c53fd6..045e6aae 100644 --- a/session/sql_store.go +++ b/session/sql_store.go @@ -14,6 +14,7 @@ import ( "github.com/lightninglabs/lightning-terminal/db/sqlc" "github.com/lightningnetwork/lnd/clock" "github.com/lightningnetwork/lnd/fn" + "github.com/lightningnetwork/lnd/sqldb/v2" "gopkg.in/macaroon-bakery.v2/bakery" "gopkg.in/macaroon.v2" ) @@ -23,6 +24,8 @@ import ( // // nolint:ll type SQLQueries interface { + sqldb.BaseQuerier + GetAliasBySessionID(ctx context.Context, id int64) ([]byte, error) GetSessionByID(ctx context.Context, id int64) (sqlc.Session, error) GetSessionsInGroup(ctx context.Context, groupID sql.NullInt64) ([]sqlc.Session, error) @@ -54,12 +57,13 @@ type SQLQueries interface { var _ Store = (*SQLStore)(nil) -// BatchedSQLQueries is a version of the SQLQueries that's capable of batched -// database operations. +// BatchedSQLQueries combines the SQLQueries interface with the BatchedTx +// interface, allowing for multiple queries to be executed in single SQL +// transaction. type BatchedSQLQueries interface { SQLQueries - db.BatchedTx[SQLQueries] + sqldb.BatchedTx[SQLQueries] } // SQLStore represents a storage backend. @@ -69,19 +73,37 @@ type SQLStore struct { db BatchedSQLQueries // BaseDB represents the underlying database connection. - *db.BaseDB + *sqldb.BaseDB clock clock.Clock } -// NewSQLStore creates a new SQLStore instance given an open BatchedSQLQueries -// storage backend. -func NewSQLStore(sqlDB *db.BaseDB, clock clock.Clock) *SQLStore { - executor := db.NewTransactionExecutor( - sqlDB, func(tx *sql.Tx) SQLQueries { - return sqlDB.WithTx(tx) +type sqlQueriesExecutor[T sqldb.BaseQuerier] struct { + *sqldb.TransactionExecutor[T] + + SQLQueries +} + +func newSQLQueriesExecutor(baseDB *sqldb.BaseDB, + queries *sqlc.Queries) *sqlQueriesExecutor[SQLQueries] { + + executor := sqldb.NewTransactionExecutor( + baseDB, func(tx *sql.Tx) SQLQueries { + return queries.WithTx(tx) }, ) + return &sqlQueriesExecutor[SQLQueries]{ + TransactionExecutor: executor, + SQLQueries: queries, + } +} + +// NewSQLStore creates a new SQLStore instance given an open BatchedSQLQueries +// storage backend. +func NewSQLStore(sqlDB *sqldb.BaseDB, queries *sqlc.Queries, + clock clock.Clock) *SQLStore { + + executor := newSQLQueriesExecutor(sqlDB, queries) return &SQLStore{ db: executor, @@ -288,7 +310,7 @@ func (s *SQLStore) NewSession(ctx context.Context, label string, typ Type, } return nil - }) + }, sqldb.NoOpReset) if err != nil { mappedSQLErr := db.MapSQLError(err) var uniqueConstraintErr *db.ErrSqlUniqueConstraintViolation @@ -332,7 +354,7 @@ func (s *SQLStore) ListSessionsByType(ctx context.Context, t Type) ([]*Session, } return nil - }) + }, sqldb.NoOpReset) return sessions, err } @@ -365,7 +387,7 @@ func (s *SQLStore) ListSessionsByState(ctx context.Context, state State) ( } return nil - }) + }, sqldb.NoOpReset) return sessions, err } @@ -424,7 +446,7 @@ func (s *SQLStore) ShiftState(ctx context.Context, alias ID, dest State) error { State: int16(dest), }, ) - }) + }, sqldb.NoOpReset) } // DeleteReservedSessions deletes all sessions that are in the StateReserved @@ -435,7 +457,7 @@ func (s *SQLStore) DeleteReservedSessions(ctx context.Context) error { var writeTxOpts db.QueriesTxOptions return s.db.ExecTx(ctx, &writeTxOpts, func(db SQLQueries) error { return db.DeleteSessionsWithState(ctx, int16(StateReserved)) - }) + }, sqldb.NoOpReset) } // DeleteReservedSession removes a given session that is in the reserved state @@ -459,7 +481,7 @@ func (s *SQLStore) DeleteReservedSession(ctx context.Context, id ID) error { } return db.DeleteSession(ctx, session.ID) - }) + }, sqldb.NoOpReset) } // GetSessionByLocalPub fetches the session with the given local pub key. @@ -489,7 +511,7 @@ func (s *SQLStore) GetSessionByLocalPub(ctx context.Context, } return nil - }) + }, sqldb.NoOpReset) if err != nil { return nil, err } @@ -522,7 +544,7 @@ func (s *SQLStore) ListAllSessions(ctx context.Context) ([]*Session, error) { } return nil - }) + }, sqldb.NoOpReset) return sessions, err } @@ -552,7 +574,7 @@ func (s *SQLStore) UpdateSessionRemotePubKey(ctx context.Context, alias ID, RemotePublicKey: remoteKey, }, ) - }) + }, sqldb.NoOpReset) } // getSqlUnusedAliasAndKeyPair can be used to generate a new, unused, local @@ -607,7 +629,7 @@ func (s *SQLStore) GetSession(ctx context.Context, alias ID) (*Session, error) { } return nil - }) + }, sqldb.NoOpReset) return sess, err } @@ -649,7 +671,7 @@ func (s *SQLStore) GetGroupID(ctx context.Context, sessionID ID) (ID, error) { legacyGroupID, err = IDFromBytes(legacyGroupIDB) return err - }) + }, sqldb.NoOpReset) if err != nil { return ID{}, err } @@ -698,7 +720,7 @@ func (s *SQLStore) GetSessionIDs(ctx context.Context, legacyGroupID ID) ([]ID, } return nil - }) + }, sqldb.NoOpReset) if err != nil { return nil, err } diff --git a/session/test_postgres.go b/session/test_postgres.go index cb5aa061..d8d5d211 100644 --- a/session/test_postgres.go +++ b/session/test_postgres.go @@ -16,7 +16,7 @@ var ErrDBClosed = errors.New("database is closed") // NewTestDB is a helper function that creates an SQLStore database for testing. func NewTestDB(t *testing.T, clock clock.Clock) Store { - return createStore(t, db.NewTestPostgresDB(t).BaseDB, clock) + return createStore(t, db.NewTestPostgresV2DB(t).BaseDB, clock) } // NewTestDBFromPath is a helper function that creates a new SQLStore with a @@ -24,5 +24,5 @@ func NewTestDB(t *testing.T, clock clock.Clock) Store { func NewTestDBFromPath(t *testing.T, dbPath string, clock clock.Clock) Store { - return createStore(t, db.NewTestPostgresDB(t).BaseDB, clock) + return createStore(t, db.NewTestPostgresV2DB(t).BaseDB, clock) } diff --git a/session/test_sql.go b/session/test_sql.go index a8318606..5623c820 100644 --- a/session/test_sql.go +++ b/session/test_sql.go @@ -6,8 +6,9 @@ import ( "testing" "github.com/lightninglabs/lightning-terminal/accounts" - "github.com/lightninglabs/lightning-terminal/db" + "github.com/lightninglabs/lightning-terminal/db/sqlc" "github.com/lightningnetwork/lnd/clock" + "github.com/lightningnetwork/lnd/sqldb/v2" "github.com/stretchr/testify/require" ) @@ -22,8 +23,12 @@ func NewTestDBWithAccounts(t *testing.T, clock clock.Clock, // createStore is a helper function that creates a new SQLStore and ensure that // it is closed when during the test cleanup. -func createStore(t *testing.T, sqlDB *db.BaseDB, clock clock.Clock) *SQLStore { - store := NewSQLStore(sqlDB, clock) +func createStore(t *testing.T, sqlDB *sqldb.BaseDB, + clock clock.Clock) *SQLStore { + + queries := sqlc.NewForType(sqlDB, sqlDB.BackendType) + + store := NewSQLStore(sqlDB, queries, clock) t.Cleanup(func() { require.NoError(t, store.Close()) }) diff --git a/session/test_sqlite.go b/session/test_sqlite.go index 0ceb0e04..84d946ce 100644 --- a/session/test_sqlite.go +++ b/session/test_sqlite.go @@ -8,6 +8,7 @@ import ( "github.com/lightninglabs/lightning-terminal/db" "github.com/lightningnetwork/lnd/clock" + "github.com/lightningnetwork/lnd/sqldb/v2" ) // ErrDBClosed is an error that is returned when a database operation is @@ -16,7 +17,10 @@ var ErrDBClosed = errors.New("database is closed") // NewTestDB is a helper function that creates an SQLStore database for testing. func NewTestDB(t *testing.T, clock clock.Clock) Store { - return createStore(t, db.NewTestSqliteDB(t).BaseDB, clock) + return createStore( + t, sqldb.NewTestSqliteDB(t, db.LitdMigrationStreams).BaseDB, + clock, + ) } // NewTestDBFromPath is a helper function that creates a new SQLStore with a @@ -24,7 +28,7 @@ func NewTestDB(t *testing.T, clock clock.Clock) Store { func NewTestDBFromPath(t *testing.T, dbPath string, clock clock.Clock) Store { - return createStore( - t, db.NewTestSqliteDbHandleFromPath(t, dbPath).BaseDB, clock, - ) + tDb := sqldb.NewTestSqliteDBFromPath(t, dbPath, db.LitdMigrationStreams) + + return createStore(t, tDb.BaseDB, clock) }