Skip to content

[sql-43] Add support for sqldb/v2 in litd #1085

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 11 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 6 additions & 14 deletions accounts/sql_migration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,17 @@ package accounts

import (
"context"
"database/sql"
"fmt"
"testing"
"time"

"github.com/lightninglabs/lightning-terminal/db"
"github.com/lightninglabs/lightning-terminal/db/sqlc"
"github.com/lightningnetwork/lnd/clock"
"github.com/lightningnetwork/lnd/fn"
"github.com/lightningnetwork/lnd/lnrpc"
"github.com/lightningnetwork/lnd/lntypes"
"github.com/lightningnetwork/lnd/lnwire"
"github.com/lightningnetwork/lnd/sqldb"
"github.com/lightningnetwork/lnd/sqldb/v2"
"github.com/stretchr/testify/require"
"golang.org/x/exp/rand"
"pgregory.net/rapid"
Expand All @@ -36,25 +35,18 @@ func TestAccountStoreMigration(t *testing.T) {
}

makeSQLDB := func(t *testing.T) (*SQLStore,
*db.TransactionExecutor[SQLQueries]) {
*SQLQueriesExecutor[SQLQueries]) {

testDBStore := NewTestDB(t, clock)
t.Cleanup(func() {
require.NoError(t, testDBStore.Close())
})

store, ok := testDBStore.(*SQLStore)
require.True(t, ok)

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 := func(t *testing.T, sqlStore *SQLStore,
Expand Down Expand Up @@ -346,7 +338,7 @@ func TestAccountStoreMigration(t *testing.T) {
return MigrateAccountStoreToSQL(
ctx, kvStore, tx,
)
},
}, sqldb.NoOpReset,
)
require.NoError(t, err)

Expand Down
66 changes: 44 additions & 22 deletions accounts/store_sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/lightningnetwork/lnd/lnrpc"
"github.com/lightningnetwork/lnd/lntypes"
"github.com/lightningnetwork/lnd/lnwire"
"github.com/lightningnetwork/lnd/sqldb/v2"
)

const (
Expand All @@ -33,6 +34,8 @@ const (
//
//nolint:lll
type SQLQueries interface {
sqldb.BaseQuerier

AddAccountInvoice(ctx context.Context, arg sqlc.AddAccountInvoiceParams) error
DeleteAccount(ctx context.Context, id int64) error
DeleteAccountPayment(ctx context.Context, arg sqlc.DeleteAccountPaymentParams) error
Expand All @@ -53,12 +56,13 @@ type SQLQueries interface {
GetAccountInvoice(ctx context.Context, arg sqlc.GetAccountInvoiceParams) (sqlc.AccountInvoice, error)
}

// 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.
Expand All @@ -68,19 +72,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,
Expand Down Expand Up @@ -157,7 +179,7 @@ func (s *SQLStore) NewAccount(ctx context.Context, balance lnwire.MilliSatoshi,
}

return nil
})
}, sqldb.NoOpReset)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -299,7 +321,7 @@ func (s *SQLStore) AddAccountInvoice(ctx context.Context, alias AccountID,
}

return s.markAccountUpdated(ctx, db, acctID)
})
}, sqldb.NoOpReset)
}

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

return s.markAccountUpdated(ctx, db, id)
})
}, sqldb.NoOpReset)
}

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

return s.markAccountUpdated(ctx, db, id)
})
}, sqldb.NoOpReset)
}

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

return s.markAccountUpdated(ctx, db, id)
})
}, sqldb.NoOpReset)
}

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

account, err = getAndMarshalAccount(ctx, db, id)
return err
})
}, sqldb.NoOpReset)

return account, err
}
Expand Down Expand Up @@ -507,7 +529,7 @@ func (s *SQLStore) Accounts(ctx context.Context) ([]*OffChainBalanceAccount,
}

return nil
})
}, sqldb.NoOpReset)

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

return db.DeleteAccount(ctx, id)
})
}, sqldb.NoOpReset)
}

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

return s.markAccountUpdated(ctx, db, id)
})
}, sqldb.NoOpReset)
}

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

return s.markAccountUpdated(ctx, db, id)
})
}, sqldb.NoOpReset)
}

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

return err
})
}, sqldb.NoOpReset)

return uint64(addIndex), uint64(settleIndex), err
}
Expand All @@ -729,7 +751,7 @@ func (s *SQLStore) StoreLastIndexes(ctx context.Context, addIndex,
Name: settleIndexName,
Value: int64(settleIndex),
})
})
}, sqldb.NoOpReset)
}

// Close closes the underlying store.
Expand Down
4 changes: 2 additions & 2 deletions accounts/test_postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ 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 NewSQLStore(db.NewTestPostgresDB(t).BaseDB, clock)
return createStore(t, db.NewTestPostgresDB(t).BaseDB, clock)
}

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

return NewSQLStore(db.NewTestPostgresDB(t).BaseDB, clock)
return createStore(t, db.NewTestPostgresDB(t).BaseDB, clock)
}
27 changes: 27 additions & 0 deletions accounts/test_sql.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//go:build test_db_postgres || test_db_sqlite

package accounts

import (
"testing"

"github.com/lightninglabs/lightning-terminal/db/sqlc"
"github.com/lightningnetwork/lnd/clock"
"github.com/lightningnetwork/lnd/sqldb/v2"
"github.com/stretchr/testify/require"
)

// 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 *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())
})

return store
}
12 changes: 8 additions & 4 deletions accounts/test_sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -16,15 +17,18 @@ 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 NewSQLStore(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
// connection to an existing SQL database for testing.
func NewTestDBFromPath(t *testing.T, dbPath string,
clock clock.Clock) Store {

return NewSQLStore(
db.NewTestSqliteDbHandleFromPath(t, dbPath).BaseDB, clock,
)
tDb := sqldb.NewTestSqliteDBFromPath(t, dbPath, db.LitdMigrationStreams)

return createStore(t, tDb.BaseDB, clock)
}
Loading