Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
ea20fb5
mod: add sqldb/v2 dependency to `litd`
ViktorTigerstrom Jun 4, 2025
47ab4f8
db: add `LitdMigrationStream`
ViktorTigerstrom Jul 24, 2025
4bffead
db+sqlc: use sqldb/v2 `BackendType` definition
ViktorTigerstrom Jul 24, 2025
c7e3b9d
sqlc: introduce `NewForType` helper method
ViktorTigerstrom Jul 24, 2025
d8bbce8
db: add sqldb/v2 `PostgresStore` creation helper
ViktorTigerstrom Jul 24, 2025
b00d77d
accounts: use `sqldb/v2` in accounts package
ViktorTigerstrom Jul 22, 2025
d869a91
multi: use `sqldb/v2` in session package
ViktorTigerstrom Jul 22, 2025
431c3d6
firewalldb: add `ListAllKVStoresRecords` to queries
ViktorTigerstrom Jul 10, 2025
8d6b9d0
firewalldb: rename sqlStore to store in mig test
ViktorTigerstrom Jul 11, 2025
b5d4438
firewalldb: use `sqldb/v2` in firewalldb package
ViktorTigerstrom Jul 22, 2025
133ac68
multi: remove unused db code
ViktorTigerstrom Jul 23, 2025
7792848
mutli: rename `db.NewTestPostgresV2DB` function
ViktorTigerstrom Jul 23, 2025
d8d2331
sqlcmig6: add `sqlcmig6` package
ViktorTigerstrom Jul 10, 2025
0355557
sqlcmig6: add `CustomQueries` to `Queries`
ViktorTigerstrom Aug 27, 2025
0f3b78b
sqlcmig6: add transaction executor for Queries
ViktorTigerstrom Aug 27, 2025
310a1b5
accounts: use `sqlcmig6` for kvdb to sql migration
ViktorTigerstrom Jul 10, 2025
91432b0
session: use `sqlcmig6` for kvdb to sql migration
ViktorTigerstrom Jul 10, 2025
6693fbc
firewalldb: use queries to assert migration results
ViktorTigerstrom Aug 28, 2025
2c246ce
firewalldb: use `sqlcmig6` for kvdb to sql migration
ViktorTigerstrom Jul 11, 2025
abb4781
firewalldb: refactor execution of mig test assert
ViktorTigerstrom Aug 28, 2025
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
6 changes: 6 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,9 @@ issues:
- unused
- deadcode
- varcheck
# As the db/sqlcmig6 package has been copied from sqlc generated code,
# but isn't marked as code generated by sqlc, the code line length in the
# package will often exceed the lll limit.
- path: db/sqlcmig6/.*
linters:
- lll
87 changes: 76 additions & 11 deletions accounts/sql_migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@ import (
"time"

"github.com/davecgh/go-spew/spew"
"github.com/lightninglabs/lightning-terminal/db/sqlc"
"github.com/lightninglabs/lightning-terminal/db/sqlcmig6"
"github.com/lightningnetwork/lnd/kvdb"
"github.com/lightningnetwork/lnd/lnrpc"
"github.com/lightningnetwork/lnd/lntypes"
"github.com/lightningnetwork/lnd/lnwire"
"github.com/pmezard/go-difflib/difflib"
)

Expand All @@ -27,7 +30,7 @@ var (
// the KV database to the SQL database. The migration is done in a single
// transaction to ensure that all accounts are migrated or none at all.
func MigrateAccountStoreToSQL(ctx context.Context, kvStore kvdb.Backend,
tx SQLQueries) error {
tx *sqlcmig6.Queries) error {

log.Infof("Starting migration of the KV accounts store to SQL")

Expand All @@ -50,7 +53,7 @@ func MigrateAccountStoreToSQL(ctx context.Context, kvStore kvdb.Backend,
// to the SQL database. The migration is done in a single transaction to ensure
// that all accounts are migrated or none at all.
func migrateAccountsToSQL(ctx context.Context, kvStore kvdb.Backend,
tx SQLQueries) error {
tx *sqlcmig6.Queries) error {

log.Infof("Starting migration of accounts from KV to SQL")

Expand All @@ -68,7 +71,7 @@ func migrateAccountsToSQL(ctx context.Context, kvStore kvdb.Backend,
kvAccount.ID, err)
}

migratedAccount, err := getAndMarshalAccount(
migratedAccount, err := getAndMarshalMig6Account(
ctx, tx, migratedAccountID,
)
if err != nil {
Expand Down Expand Up @@ -151,17 +154,79 @@ func getBBoltAccounts(db kvdb.Backend) ([]*OffChainBalanceAccount, error) {
return accounts, nil
}

// getAndMarshalAccount retrieves the account with the given ID. If the account
// cannot be found, then ErrAccNotFound is returned.
func getAndMarshalMig6Account(ctx context.Context, db *sqlcmig6.Queries,
id int64) (*OffChainBalanceAccount, error) {

dbAcct, err := db.GetAccount(ctx, id)
if errors.Is(err, sql.ErrNoRows) {
return nil, ErrAccNotFound
} else if err != nil {
return nil, err
}

return marshalDBMig6Account(ctx, db, dbAcct)
}

func marshalDBMig6Account(ctx context.Context, db *sqlcmig6.Queries,
dbAcct sqlcmig6.Account) (*OffChainBalanceAccount, error) {

alias, err := AccountIDFromInt64(dbAcct.Alias)
if err != nil {
return nil, err
}

account := &OffChainBalanceAccount{
ID: alias,
Type: AccountType(dbAcct.Type),
InitialBalance: lnwire.MilliSatoshi(dbAcct.InitialBalanceMsat),
CurrentBalance: dbAcct.CurrentBalanceMsat,
LastUpdate: dbAcct.LastUpdated.UTC(),
ExpirationDate: dbAcct.Expiration.UTC(),
Invoices: make(AccountInvoices),
Payments: make(AccountPayments),
Label: dbAcct.Label.String,
}

invoices, err := db.ListAccountInvoices(ctx, dbAcct.ID)
if err != nil {
return nil, err
}
for _, invoice := range invoices {
var hash lntypes.Hash
copy(hash[:], invoice.Hash)
account.Invoices[hash] = struct{}{}
}

payments, err := db.ListAccountPayments(ctx, dbAcct.ID)
if err != nil {
return nil, err
}

for _, payment := range payments {
var hash lntypes.Hash
copy(hash[:], payment.Hash)
account.Payments[hash] = &PaymentEntry{
Status: lnrpc.Payment_PaymentStatus(payment.Status),
FullAmount: lnwire.MilliSatoshi(payment.FullAmountMsat),
}
}

return account, nil
}

// migrateSingleAccountToSQL runs the migration for a single account from the
// KV database to the SQL database.
func migrateSingleAccountToSQL(ctx context.Context,
tx SQLQueries, account *OffChainBalanceAccount) (int64, error) {
tx *sqlcmig6.Queries, account *OffChainBalanceAccount) (int64, error) {

accountAlias, err := account.ID.ToInt64()
if err != nil {
return 0, err
}

insertAccountParams := sqlc.InsertAccountParams{
insertAccountParams := sqlcmig6.InsertAccountParams{
Type: int16(account.Type),
InitialBalanceMsat: int64(account.InitialBalance),
CurrentBalanceMsat: account.CurrentBalance,
Expand All @@ -180,7 +245,7 @@ func migrateSingleAccountToSQL(ctx context.Context,
}

for hash := range account.Invoices {
addInvoiceParams := sqlc.AddAccountInvoiceParams{
addInvoiceParams := sqlcmig6.AddAccountInvoiceParams{
AccountID: sqlId,
Hash: hash[:],
}
Expand All @@ -192,7 +257,7 @@ func migrateSingleAccountToSQL(ctx context.Context,
}

for hash, paymentEntry := range account.Payments {
upsertPaymentParams := sqlc.UpsertAccountPaymentParams{
upsertPaymentParams := sqlcmig6.UpsertAccountPaymentParams{
AccountID: sqlId,
Hash: hash[:],
Status: int16(paymentEntry.Status),
Expand All @@ -211,7 +276,7 @@ func migrateSingleAccountToSQL(ctx context.Context,
// migrateAccountsIndicesToSQL runs the migration for the account indices from
// the KV database to the SQL database.
func migrateAccountsIndicesToSQL(ctx context.Context, kvStore kvdb.Backend,
tx SQLQueries) error {
tx *sqlcmig6.Queries) error {

log.Infof("Starting migration of accounts indices from KV to SQL")

Expand All @@ -233,7 +298,7 @@ func migrateAccountsIndicesToSQL(ctx context.Context, kvStore kvdb.Backend,
settleIndexName, settleIndex)
}

setAddIndexParams := sqlc.SetAccountIndexParams{
setAddIndexParams := sqlcmig6.SetAccountIndexParams{
Name: addIndexName,
Value: int64(addIndex),
}
Expand All @@ -243,7 +308,7 @@ func migrateAccountsIndicesToSQL(ctx context.Context, kvStore kvdb.Backend,
return err
}

setSettleIndexParams := sqlc.SetAccountIndexParams{
setSettleIndexParams := sqlcmig6.SetAccountIndexParams{
Name: settleIndexName,
Value: int64(settleIndex),
}
Expand Down
19 changes: 7 additions & 12 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/sqlcmig6"
"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,7 +35,7 @@ func TestAccountStoreMigration(t *testing.T) {
}

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

testDBStore := NewTestDB(t, clock)

Expand All @@ -45,13 +44,9 @@ func TestAccountStoreMigration(t *testing.T) {

baseDB := store.BaseDB

genericExecutor := db.NewTransactionExecutor(
baseDB, func(tx *sql.Tx) SQLQueries {
return baseDB.WithTx(tx)
},
)
queries := sqlcmig6.NewForType(baseDB, baseDB.BackendType)

return store, genericExecutor
return store, sqlcmig6.NewTxExecutor(baseDB, queries)
}

assertMigrationResults := func(t *testing.T, sqlStore *SQLStore,
Expand Down Expand Up @@ -339,11 +334,11 @@ func TestAccountStoreMigration(t *testing.T) {
// Perform the migration.
err = txEx.ExecTx(
ctx, sqldb.WriteTxOpt(),
func(tx SQLQueries) error {
func(tx *sqlcmig6.Queries) error {
return MigrateAccountStoreToSQL(
ctx, kvStore.db, tx,
)
},
}, sqldb.NoOpReset,
)
require.NoError(t, err)

Expand Down
Loading
Loading