mirror of
https://github.com/lightninglabs/lightning-terminal.git
synced 2026-08-13 12:33:36 +02:00
firewalldb: use sqldb/v2 in firewalldb package
Update the firewalldb package to use the `sqldb/v2` package instead of the older version.
This commit is contained in:
parent
e890a1c5a8
commit
297d203db2
9 changed files with 106 additions and 92 deletions
|
|
@ -103,13 +103,6 @@ func NewStores(cfg *Config, clock clock.Clock) (*stores, error) {
|
|||
return stores, err
|
||||
}
|
||||
|
||||
// Until we have fully added support for sqldb/v2 in all of our
|
||||
// stores, we need to use the db packages definition of the
|
||||
// SQLite store for the packages that still haven't added
|
||||
// support for sqldb/v2. This is only temporary and will be
|
||||
// removed once all stores have been updated to use sqldb/v2.
|
||||
legacySqlStore, err := db.NewSqliteStore(cfg.Sqlite)
|
||||
|
||||
sqlStore, err := sqldb.NewSqliteStore(&sqldb.SqliteConfig{
|
||||
SkipMigrations: cfg.Sqlite.SkipMigrations,
|
||||
SkipMigrationDbBackup: cfg.Sqlite.SkipMigrationDbBackup,
|
||||
|
|
@ -138,7 +131,7 @@ func NewStores(cfg *Config, clock clock.Clock) (*stores, error) {
|
|||
sqlStore.BaseDB, queries, clock,
|
||||
)
|
||||
firewallStore := firewalldb.NewSQLDB(
|
||||
legacySqlStore.BaseDB, clock,
|
||||
sqlStore.BaseDB, queries, clock,
|
||||
)
|
||||
|
||||
stores.accounts = acctStore
|
||||
|
|
@ -147,13 +140,6 @@ func NewStores(cfg *Config, clock clock.Clock) (*stores, error) {
|
|||
stores.closeFns["sqlite"] = sqlStore.BaseDB.Close
|
||||
|
||||
case DatabaseBackendPostgres:
|
||||
// Until we have fully added support for sqldb/v2 in all of our
|
||||
// stores, we need to use the db packages definition of the
|
||||
// Postgres store for the packages that still haven't added
|
||||
// support for sqldb/v2. This is only temporary and will be
|
||||
// removed once all stores have been updated to use sqldb/v2.
|
||||
legacySqlStore, err := db.NewPostgresStore(cfg.Postgres)
|
||||
|
||||
sqlStore, err := sqldb.NewPostgresStore(&sqldb.PostgresConfig{
|
||||
Dsn: cfg.Postgres.DSN(false),
|
||||
MaxOpenConnections: cfg.Postgres.MaxOpenConnections,
|
||||
|
|
@ -187,7 +173,7 @@ func NewStores(cfg *Config, clock clock.Clock) (*stores, error) {
|
|||
sqlStore.BaseDB, queries, clock,
|
||||
)
|
||||
firewallStore := firewalldb.NewSQLDB(
|
||||
legacySqlStore.BaseDB, clock,
|
||||
sqlStore.BaseDB, queries, clock,
|
||||
)
|
||||
|
||||
stores.accounts = acctStore
|
||||
|
|
|
|||
|
|
@ -13,7 +13,7 @@ import (
|
|||
"github.com/lightninglabs/lightning-terminal/db/sqlc"
|
||||
"github.com/lightninglabs/lightning-terminal/session"
|
||||
"github.com/lightningnetwork/lnd/fn"
|
||||
"github.com/lightningnetwork/lnd/sqldb"
|
||||
"github.com/lightningnetwork/lnd/sqldb/v2"
|
||||
)
|
||||
|
||||
// SQLAccountQueries is a subset of the sqlc.Queries interface that can be used
|
||||
|
|
@ -172,7 +172,7 @@ func (s *SQLDB) AddAction(ctx context.Context,
|
|||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
}, sqldb.NoOpReset)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
@ -207,7 +207,7 @@ func (s *SQLDB) SetActionState(ctx context.Context, al ActionLocator,
|
|||
Valid: errReason != "",
|
||||
},
|
||||
})
|
||||
})
|
||||
}, sqldb.NoOpReset)
|
||||
}
|
||||
|
||||
// ListActions returns a list of Actions. The query IndexOffset and MaxNum
|
||||
|
|
@ -355,7 +355,7 @@ func (s *SQLDB) ListActions(ctx context.Context,
|
|||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
}, sqldb.NoOpReset)
|
||||
|
||||
return actions, lastIndex, uint64(totalCount), err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,6 +11,7 @@ import (
|
|||
"github.com/lightninglabs/lightning-terminal/db/sqlc"
|
||||
"github.com/lightninglabs/lightning-terminal/session"
|
||||
"github.com/lightningnetwork/lnd/fn"
|
||||
"github.com/lightningnetwork/lnd/sqldb/v2"
|
||||
)
|
||||
|
||||
// SQLKVStoreQueries is a subset of the sqlc.Queries interface that can be
|
||||
|
|
@ -46,7 +47,7 @@ func (s *SQLDB) DeleteTempKVStores(ctx context.Context) error {
|
|||
|
||||
return s.db.ExecTx(ctx, &writeTxOpts, func(tx SQLQueries) error {
|
||||
return tx.DeleteAllTempKVStores(ctx)
|
||||
})
|
||||
}, sqldb.NoOpReset)
|
||||
}
|
||||
|
||||
// GetKVStores constructs a new rules.KVStores in a namespace defined by the
|
||||
|
|
|
|||
|
|
@ -16,7 +16,7 @@ import (
|
|||
"github.com/lightninglabs/lightning-terminal/db/sqlc"
|
||||
"github.com/lightninglabs/lightning-terminal/session"
|
||||
"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"
|
||||
)
|
||||
|
|
@ -88,12 +88,11 @@ type privacyPairs = map[int64]map[string]string
|
|||
// NOTE: As sessions may contain linked sessions and accounts, the sessions and
|
||||
// accounts sql migration MUST be run prior to this migration.
|
||||
func MigrateFirewallDBToSQL(ctx context.Context, kvStore *bbolt.DB,
|
||||
sqlTx SQLQueries, sessionDB session.SQLQueries,
|
||||
accountDB accounts.SQLQueries, macRootKeyIDs [][]byte) error {
|
||||
sqlTx SQLQueries, queries *sqlc.Queries, macRootKeyIDs [][]byte) error {
|
||||
|
||||
log.Infof("Starting migration of the rules DB to SQL")
|
||||
|
||||
sessions, err := sessionDB.ListSessions(ctx)
|
||||
sessions, err := queries.ListSessions(ctx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("listing sessions failed: %w", err)
|
||||
}
|
||||
|
|
@ -114,8 +113,7 @@ func MigrateFirewallDBToSQL(ctx context.Context, kvStore *bbolt.DB,
|
|||
}
|
||||
|
||||
err = migrateActionsToSQL(
|
||||
ctx, kvStore, sqlTx, sessionDB, accountDB, macRootKeyIDs,
|
||||
sessionMap,
|
||||
ctx, kvStore, sqlTx, queries, macRootKeyIDs, sessionMap,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
@ -844,8 +842,7 @@ func validateGroupPairsMigration(ctx context.Context, sqlTx SQLQueries,
|
|||
// database to the SQL database. The function also asserts that the migrated
|
||||
// values match the original values in the actions store.
|
||||
func migrateActionsToSQL(ctx context.Context, kvStore *bbolt.DB,
|
||||
sqlTx SQLQueries, sessionDB session.SQLQueries,
|
||||
accountsDB accounts.SQLQueries, macRootKeyIDs [][]byte,
|
||||
sqlTx SQLQueries, queries *sqlc.Queries, macRootKeyIDs [][]byte,
|
||||
sessMap map[[4]byte]sqlc.Session) error {
|
||||
|
||||
log.Infof("Starting migration of the actions store to SQL")
|
||||
|
|
@ -853,7 +850,7 @@ func migrateActionsToSQL(ctx context.Context, kvStore *bbolt.DB,
|
|||
// Start by fetching all accounts and sessions, and map them by their
|
||||
// IDs. This will allow us to quickly look up any account(s) and/or
|
||||
// session that match a specific action's macaroon identifier.
|
||||
accts, err := accountsDB.ListAllAccounts(ctx)
|
||||
accts, err := queries.ListAllAccounts(ctx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("listing accounts failed: %w", err)
|
||||
}
|
||||
|
|
@ -970,8 +967,8 @@ func migrateActionsToSQL(ctx context.Context, kvStore *bbolt.DB,
|
|||
// validate that the action was correctly
|
||||
// migrated.
|
||||
err = migrateActionToSQL(
|
||||
ctx, sqlTx, sessionDB, accountsDB,
|
||||
acctsMap, sessMap, action, macRootKeyID,
|
||||
ctx, sqlTx, queries, acctsMap, sessMap,
|
||||
action, macRootKeyID,
|
||||
)
|
||||
if err != nil {
|
||||
return fmt.Errorf("migrating action "+
|
||||
|
|
@ -995,9 +992,9 @@ func migrateActionsToSQL(ctx context.Context, kvStore *bbolt.DB,
|
|||
// migrateActionToSQL migrates a single action to the SQL database, and
|
||||
// validates that the action was correctly migrated.
|
||||
func migrateActionToSQL(ctx context.Context, sqlTx SQLQueries,
|
||||
sessionDB session.SQLQueries, accountsDB accounts.SQLQueries,
|
||||
acctsMap map[[4]byte][]sqlc.Account, sessMap map[[4]byte]sqlc.Session,
|
||||
action *Action, macRootKeyID []byte) error {
|
||||
queries *sqlc.Queries, acctsMap map[[4]byte][]sqlc.Account,
|
||||
sessMap map[[4]byte]sqlc.Session, action *Action,
|
||||
macRootKeyID []byte) error {
|
||||
|
||||
var (
|
||||
macIDSuffix [4]byte
|
||||
|
|
@ -1030,7 +1027,7 @@ func migrateActionToSQL(ctx context.Context, sqlTx SQLQueries,
|
|||
case hasAccounts && hasSessions:
|
||||
// Alternative (3) above.
|
||||
insertParams, err = paramsFromBothSessionAndAccounts(
|
||||
ctx, accountsDB, action, actAccounts, actSession,
|
||||
ctx, queries, action, actAccounts, actSession,
|
||||
macRootKeyID,
|
||||
)
|
||||
case hasSessions:
|
||||
|
|
@ -1041,7 +1038,7 @@ func migrateActionToSQL(ctx context.Context, sqlTx SQLQueries,
|
|||
case hasAccounts:
|
||||
// Alternative (2) above.
|
||||
insertParams, err = paramsFromAccounts(
|
||||
ctx, accountsDB, action, actAccounts, macRootKeyID)
|
||||
ctx, queries, action, actAccounts, macRootKeyID)
|
||||
default:
|
||||
// Alternative (4) above.
|
||||
insertParams = paramsFromAction(action, macRootKeyID)
|
||||
|
|
@ -1061,7 +1058,7 @@ func migrateActionToSQL(ctx context.Context, sqlTx SQLQueries,
|
|||
|
||||
// Finally, validate that the action was correctly migrated.
|
||||
return validateMigratedAction(
|
||||
ctx, sqlTx, sessionDB, action, insertParams, migratedActionID,
|
||||
ctx, sqlTx, queries, action, insertParams, migratedActionID,
|
||||
)
|
||||
}
|
||||
|
||||
|
|
@ -1070,7 +1067,7 @@ func migrateActionToSQL(ctx context.Context, sqlTx SQLQueries,
|
|||
// action, the insert params used to insert the action into the SQL DB,
|
||||
// and the ID of the migrated action in the SQL DB.
|
||||
func validateMigratedAction(ctx context.Context, sqlTx SQLQueries,
|
||||
sessionDB session.SQLQueries, kvAction *Action,
|
||||
queries *sqlc.Queries, kvAction *Action,
|
||||
insertParams sqlc.InsertActionParams, migratedActionID int64) error {
|
||||
|
||||
// First, fetch the action back from the SQL DB.
|
||||
|
|
@ -1096,7 +1093,7 @@ func validateMigratedAction(ctx context.Context, sqlTx SQLQueries,
|
|||
// fields were set to. This is required in order to make the KVDB and
|
||||
// SQL actions comparable.
|
||||
if insertParams.SessionID.Valid {
|
||||
sess, err := sessionDB.GetSessionByID(
|
||||
sess, err := queries.GetSessionByID(
|
||||
ctx, insertParams.SessionID.Int64,
|
||||
)
|
||||
if err != nil {
|
||||
|
|
@ -1108,7 +1105,7 @@ func validateMigratedAction(ctx context.Context, sqlTx SQLQueries,
|
|||
}
|
||||
|
||||
if insertParams.AccountID.Valid {
|
||||
acct, err := sessionDB.GetAccount(
|
||||
acct, err := queries.GetAccount(
|
||||
ctx, insertParams.AccountID.Int64,
|
||||
)
|
||||
if err != nil {
|
||||
|
|
@ -1172,7 +1169,7 @@ func validateMigratedAction(ctx context.Context, sqlTx SQLQueries,
|
|||
// to the potential linked account with the earliest expiry (where accounts
|
||||
// that do not expire is seen as the earliest).
|
||||
func paramsFromBothSessionAndAccounts(ctx context.Context,
|
||||
accountsDB accounts.SQLQueries, action *Action, actAccts []sqlc.Account,
|
||||
queries *sqlc.Queries, action *Action, actAccts []sqlc.Account,
|
||||
sess sqlc.Session, macRootKeyID []byte) (sqlc.InsertActionParams,
|
||||
error) {
|
||||
|
||||
|
|
@ -1180,7 +1177,7 @@ func paramsFromBothSessionAndAccounts(ctx context.Context,
|
|||
// be responsible for the action, or if they should be filtered out.
|
||||
sessOpt := getMatchingSessionForAction(action, sess)
|
||||
acctOpt, err := getMatchingAccountForAction(
|
||||
ctx, accountsDB, action, actAccts,
|
||||
ctx, queries, action, actAccts,
|
||||
)
|
||||
if err != nil {
|
||||
return sqlc.InsertActionParams{}, err
|
||||
|
|
@ -1195,7 +1192,7 @@ func paramsFromBothSessionAndAccounts(ctx context.Context,
|
|||
// If the session was filtered out, but we still have an
|
||||
// account, we link the action to the account.
|
||||
return paramsFromAccounts(
|
||||
ctx, accountsDB, action, actAccts, macRootKeyID,
|
||||
ctx, queries, action, actAccts, macRootKeyID,
|
||||
)
|
||||
case sessOpt.IsSome():
|
||||
return paramsFromSession(action, sess, macRootKeyID)
|
||||
|
|
@ -1227,12 +1224,12 @@ func paramsFromSession(action *Action, actSess sqlc.Session,
|
|||
// paramsFromAccounts returns the insert params for an action linked to an
|
||||
// account. If no matching account is found for the action, the action will not
|
||||
// be linked to any account.
|
||||
func paramsFromAccounts(ctx context.Context, accountsDB accounts.SQLQueries,
|
||||
func paramsFromAccounts(ctx context.Context, queries *sqlc.Queries,
|
||||
action *Action, actAccts []sqlc.Account,
|
||||
macRootKeyID []byte) (sqlc.InsertActionParams, error) {
|
||||
|
||||
acctOpt, err := getMatchingAccountForAction(
|
||||
ctx, accountsDB, action, actAccts,
|
||||
ctx, queries, action, actAccts,
|
||||
)
|
||||
if err != nil {
|
||||
return sqlc.InsertActionParams{}, err
|
||||
|
|
@ -1305,7 +1302,7 @@ func getMatchingSessionForAction(action *Action,
|
|||
// reasoning that such accounts were more likely to have existed at the time of
|
||||
// the action, as we have no way of tracking when the account was created.
|
||||
func getMatchingAccountForAction(ctx context.Context,
|
||||
accountsDB accounts.SQLQueries, action *Action,
|
||||
queries *sqlc.Queries, action *Action,
|
||||
actAccts []sqlc.Account) (fn.Option[sqlc.Account], error) {
|
||||
|
||||
// sendMethods is the RPC methods that trigger payments to be added an
|
||||
|
|
@ -1336,12 +1333,12 @@ func getMatchingAccountForAction(ctx context.Context,
|
|||
continue
|
||||
}
|
||||
|
||||
invoices, err := accountsDB.ListAccountInvoices(ctx, a.ID)
|
||||
invoices, err := queries.ListAccountInvoices(ctx, a.ID)
|
||||
if err != nil && !errors.Is(err, sql.ErrNoRows) {
|
||||
return fn.None[sqlc.Account](), fmt.Errorf("listing "+
|
||||
"invoices for account %d failed: %w", a.ID, err)
|
||||
}
|
||||
payments, err := accountsDB.ListAccountPayments(ctx, a.ID)
|
||||
payments, err := queries.ListAccountPayments(ctx, a.ID)
|
||||
if err != nil && !errors.Is(err, sql.ErrNoRows) {
|
||||
return fn.None[sqlc.Account](), fmt.Errorf("listing "+
|
||||
"payments for account %d failed: %w", a.ID, err)
|
||||
|
|
|
|||
|
|
@ -12,13 +12,12 @@ import (
|
|||
"time"
|
||||
|
||||
"github.com/lightninglabs/lightning-terminal/accounts"
|
||||
"github.com/lightninglabs/lightning-terminal/db"
|
||||
"github.com/lightninglabs/lightning-terminal/db/sqlc"
|
||||
"github.com/lightninglabs/lightning-terminal/session"
|
||||
"github.com/lightningnetwork/lnd/clock"
|
||||
"github.com/lightningnetwork/lnd/fn"
|
||||
"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"
|
||||
|
|
@ -126,7 +125,7 @@ func TestFirewallDBMigration(t *testing.T) {
|
|||
}
|
||||
|
||||
makeSQLDB := func(t *testing.T, sessionsStore session.Store) (*SQLDB,
|
||||
*db.TransactionExecutor[SQLQueries]) {
|
||||
*sqlQueriesExecutor[SQLQueries]) {
|
||||
|
||||
testDBStore := NewTestDBWithSessions(t, sessionsStore, clock)
|
||||
|
||||
|
|
@ -135,13 +134,9 @@ func TestFirewallDBMigration(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)
|
||||
}
|
||||
|
||||
// The assertKvStoreMigrationResults function will currently assert that
|
||||
|
|
@ -160,7 +155,9 @@ func TestFirewallDBMigration(t *testing.T) {
|
|||
getRuleID := func(ruleName string) int64 {
|
||||
ruleID, ok := ruleIDs[ruleName]
|
||||
if !ok {
|
||||
ruleID, err = store.GetRuleID(ctx, ruleName)
|
||||
ruleID, err = store.db.GetRuleID(
|
||||
ctx, ruleName,
|
||||
)
|
||||
require.NoError(t, err)
|
||||
|
||||
ruleIDs[ruleName] = ruleID
|
||||
|
|
@ -172,7 +169,7 @@ func TestFirewallDBMigration(t *testing.T) {
|
|||
getGroupID := func(groupAlias []byte) int64 {
|
||||
groupID, ok := groupIDs[string(groupAlias)]
|
||||
if !ok {
|
||||
groupID, err = store.GetSessionIDByAlias(
|
||||
groupID, err = store.db.GetSessionIDByAlias(
|
||||
ctx, groupAlias,
|
||||
)
|
||||
require.NoError(t, err)
|
||||
|
|
@ -186,7 +183,7 @@ func TestFirewallDBMigration(t *testing.T) {
|
|||
getFeatureID := func(featureName string) int64 {
|
||||
featureID, ok := featureIDs[featureName]
|
||||
if !ok {
|
||||
featureID, err = store.GetFeatureID(
|
||||
featureID, err = store.db.GetFeatureID(
|
||||
ctx, featureName,
|
||||
)
|
||||
require.NoError(t, err)
|
||||
|
|
@ -200,7 +197,7 @@ func TestFirewallDBMigration(t *testing.T) {
|
|||
// First we extract all migrated kv entries from the SQLDB,
|
||||
// in order to be able to compare them to the original kv
|
||||
// entries, to ensure that the migration was successful.
|
||||
sqlKvEntries, err := store.ListAllKVStoresRecords(ctx)
|
||||
sqlKvEntries, err := store.db.ListAllKVStoresRecords(ctx)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, len(kvEntries), len(sqlKvEntries))
|
||||
|
||||
|
|
@ -216,7 +213,7 @@ func TestFirewallDBMigration(t *testing.T) {
|
|||
ruleID := getRuleID(entry.ruleName)
|
||||
|
||||
if entry.groupAlias.IsNone() {
|
||||
sqlVal, err := store.GetGlobalKVStoreRecord(
|
||||
sqlVal, err := store.db.GetGlobalKVStoreRecord(
|
||||
ctx,
|
||||
sqlc.GetGlobalKVStoreRecordParams{
|
||||
Key: entry.key,
|
||||
|
|
@ -234,7 +231,7 @@ func TestFirewallDBMigration(t *testing.T) {
|
|||
groupAlias := entry.groupAlias.UnwrapOrFail(t)
|
||||
groupID := getGroupID(groupAlias[:])
|
||||
|
||||
v, err := store.GetGroupKVStoreRecord(
|
||||
v, err := store.db.GetGroupKVStoreRecord(
|
||||
ctx,
|
||||
sqlc.GetGroupKVStoreRecordParams{
|
||||
Key: entry.key,
|
||||
|
|
@ -259,7 +256,7 @@ func TestFirewallDBMigration(t *testing.T) {
|
|||
entry.featureName.UnwrapOrFail(t),
|
||||
)
|
||||
|
||||
sqlVal, err := store.GetFeatureKVStoreRecord(
|
||||
sqlVal, err := store.db.GetFeatureKVStoreRecord(
|
||||
ctx,
|
||||
sqlc.GetFeatureKVStoreRecordParams{
|
||||
Key: entry.key,
|
||||
|
|
@ -297,7 +294,7 @@ func TestFirewallDBMigration(t *testing.T) {
|
|||
// First assert that the SQLDB contains the expected privacy
|
||||
// pairs.
|
||||
for groupID, groupPairs := range privPairs {
|
||||
storePairs, err := sqlStore.GetAllPrivacyPairs(
|
||||
storePairs, err := sqlStore.db.GetAllPrivacyPairs(
|
||||
ctx, groupID,
|
||||
)
|
||||
require.NoError(t, err)
|
||||
|
|
@ -319,11 +316,12 @@ func TestFirewallDBMigration(t *testing.T) {
|
|||
|
||||
// Then assert that SQLDB doesn't contain any other privacy
|
||||
// pairs than the expected ones.
|
||||
sessions, err := sqlStore.ListSessions(ctx)
|
||||
queries := sqlc.NewForType(sqlStore, sqlStore.BackendType)
|
||||
sessions, err := queries.ListSessions(ctx)
|
||||
require.NoError(t, err)
|
||||
|
||||
for _, dbSession := range sessions {
|
||||
sessionPairs, err := sqlStore.GetAllPrivacyPairs(
|
||||
sessionPairs, err := sqlStore.db.GetAllPrivacyPairs(
|
||||
ctx, dbSession.ID,
|
||||
)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
|
|
@ -574,10 +572,9 @@ func TestFirewallDBMigration(t *testing.T) {
|
|||
|
||||
return MigrateFirewallDBToSQL(
|
||||
ctx, firewallStore.DB, tx, qs,
|
||||
qs,
|
||||
rootKeyStore.getAllRootKeys(),
|
||||
)
|
||||
},
|
||||
}, sqldb.NoOpReset,
|
||||
)
|
||||
require.NoError(t, err)
|
||||
|
||||
|
|
|
|||
|
|
@ -5,7 +5,9 @@ import (
|
|||
"database/sql"
|
||||
|
||||
"github.com/lightninglabs/lightning-terminal/db"
|
||||
"github.com/lightninglabs/lightning-terminal/db/sqlc"
|
||||
"github.com/lightningnetwork/lnd/clock"
|
||||
"github.com/lightningnetwork/lnd/sqldb/v2"
|
||||
)
|
||||
|
||||
// SQLSessionQueries is a subset of the sqlc.Queries interface that can be used
|
||||
|
|
@ -18,17 +20,20 @@ type SQLSessionQueries interface {
|
|||
// SQLQueries is a subset of the sqlc.Queries interface that can be used to
|
||||
// interact with various firewalldb tables.
|
||||
type SQLQueries interface {
|
||||
sqldb.BaseQuerier
|
||||
|
||||
SQLKVStoreQueries
|
||||
SQLPrivacyPairQueries
|
||||
SQLActionQueries
|
||||
}
|
||||
|
||||
// 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]
|
||||
}
|
||||
|
||||
// SQLDB represents a storage backend.
|
||||
|
|
@ -38,11 +43,31 @@ type SQLDB struct {
|
|||
db BatchedSQLQueries
|
||||
|
||||
// BaseDB represents the underlying database connection.
|
||||
*db.BaseDB
|
||||
*sqldb.BaseDB
|
||||
|
||||
clock clock.Clock
|
||||
}
|
||||
|
||||
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,
|
||||
}
|
||||
}
|
||||
|
||||
// A compile-time assertion to ensure that SQLDB implements the RulesDB
|
||||
// interface.
|
||||
var _ RulesDB = (*SQLDB)(nil)
|
||||
|
|
@ -53,12 +78,10 @@ var _ ActionDB = (*SQLDB)(nil)
|
|||
|
||||
// NewSQLDB creates a new SQLStore instance given an open SQLQueries
|
||||
// storage backend.
|
||||
func NewSQLDB(sqlDB *db.BaseDB, clock clock.Clock) *SQLDB {
|
||||
executor := db.NewTransactionExecutor(
|
||||
sqlDB, func(tx *sql.Tx) SQLQueries {
|
||||
return sqlDB.WithTx(tx)
|
||||
},
|
||||
)
|
||||
func NewSQLDB(sqlDB *sqldb.BaseDB, queries *sqlc.Queries,
|
||||
clock clock.Clock) *SQLDB {
|
||||
|
||||
executor := newSQLQueriesExecutor(sqlDB, queries)
|
||||
|
||||
return &SQLDB{
|
||||
db: executor,
|
||||
|
|
@ -88,7 +111,7 @@ func (e *sqlExecutor[T]) Update(ctx context.Context,
|
|||
var txOpts db.QueriesTxOptions
|
||||
return e.db.ExecTx(ctx, &txOpts, func(queries SQLQueries) error {
|
||||
return fn(ctx, e.wrapTx(queries))
|
||||
})
|
||||
}, sqldb.NoOpReset)
|
||||
}
|
||||
|
||||
// View opens a database read transaction and executes the function f with the
|
||||
|
|
@ -104,5 +127,5 @@ func (e *sqlExecutor[T]) View(ctx context.Context,
|
|||
|
||||
return e.db.ExecTx(ctx, &txOpts, func(queries SQLQueries) error {
|
||||
return fn(ctx, e.wrapTx(queries))
|
||||
})
|
||||
}, sqldb.NoOpReset)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,11 +14,11 @@ var isSqlite = false
|
|||
|
||||
// NewTestDB is a helper function that creates an BBolt database for testing.
|
||||
func NewTestDB(t *testing.T, clock clock.Clock) FirewallDBs {
|
||||
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 BoltStore with a
|
||||
// connection to an existing BBolt database for testing.
|
||||
func NewTestDBFromPath(t *testing.T, _ string, clock clock.Clock) FirewallDBs {
|
||||
return createStore(t, db.NewTestPostgresDB(t).BaseDB, clock)
|
||||
return createStore(t, db.NewTestPostgresV2DB(t).BaseDB, clock)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,10 +6,12 @@ import (
|
|||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/lightninglabs/lightning-terminal/db/sqlc"
|
||||
|
||||
"github.com/lightninglabs/lightning-terminal/accounts"
|
||||
"github.com/lightninglabs/lightning-terminal/db"
|
||||
"github.com/lightninglabs/lightning-terminal/session"
|
||||
"github.com/lightningnetwork/lnd/clock"
|
||||
"github.com/lightningnetwork/lnd/sqldb/v2"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
|
|
@ -55,8 +57,10 @@ func assertEqualActions(t *testing.T, expected, got *Action) {
|
|||
|
||||
// createStore is a helper function that creates a new SQLDB and ensure that
|
||||
// it is closed when during the test cleanup.
|
||||
func createStore(t *testing.T, sqlDB *db.BaseDB, clock clock.Clock) *SQLDB {
|
||||
store := NewSQLDB(sqlDB, clock)
|
||||
func createStore(t *testing.T, sqlDB *sqldb.BaseDB, clock clock.Clock) *SQLDB {
|
||||
queries := sqlc.NewForType(sqlDB, sqlDB.BackendType)
|
||||
|
||||
store := NewSQLDB(sqlDB, queries, clock)
|
||||
t.Cleanup(func() {
|
||||
require.NoError(t, store.Close())
|
||||
})
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ import (
|
|||
|
||||
"github.com/lightninglabs/lightning-terminal/db"
|
||||
"github.com/lightningnetwork/lnd/clock"
|
||||
"github.com/lightningnetwork/lnd/sqldb/v2"
|
||||
)
|
||||
|
||||
// isSqlite is true if the test_db_sqlite build flag is set.
|
||||
|
|
@ -14,13 +15,18 @@ var isSqlite = true
|
|||
|
||||
// NewTestDB is a helper function that creates an BBolt database for testing.
|
||||
func NewTestDB(t *testing.T, clock clock.Clock) FirewallDBs {
|
||||
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 BoltStore with a
|
||||
// connection to an existing BBolt database for testing.
|
||||
func NewTestDBFromPath(t *testing.T, dbPath string, clock clock.Clock) FirewallDBs {
|
||||
return createStore(
|
||||
t, db.NewTestSqliteDbHandleFromPath(t, dbPath).BaseDB, clock,
|
||||
)
|
||||
func NewTestDBFromPath(t *testing.T, dbPath string,
|
||||
clock clock.Clock) FirewallDBs {
|
||||
|
||||
tDb := sqldb.NewTestSqliteDBFromPath(t, dbPath, db.LitdMigrationStreams)
|
||||
|
||||
return createStore(t, tDb.BaseDB, clock)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue