Skip to content

[sql-45] package queries & models present at the time of the kvdb to sql migration #1113

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 20 commits into
base: master
Choose a base branch
from
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
3e635f3
mod: add sqldb/v2 dependency to `litd`
ViktorTigerstrom Jun 4, 2025
d0aaf36
db: add `LitdMigrationStream`
ViktorTigerstrom Jul 24, 2025
d70cd76
db+sqlc: use sqldb/v2 `BackendType` definition
ViktorTigerstrom Jul 24, 2025
4012073
sqlc: introduce `NewForType` helper method
ViktorTigerstrom Jul 24, 2025
ae8370a
db: add sqldb/v2 `PostgresStore` creation helper
ViktorTigerstrom Jul 24, 2025
1400793
accounts: use `sqldb/v2` in accounts package
ViktorTigerstrom Jul 22, 2025
b556cac
session: use `sqldb/v2` in session package
ViktorTigerstrom Jul 22, 2025
6e79dd3
firewalldb: add `ListAllKVStoresRecords` to queries
ViktorTigerstrom Jul 10, 2025
1ee4ee0
firewalldb: rename sqlStore to store in mig test
ViktorTigerstrom Jul 11, 2025
f3be2c1
firewalldb: use `sqldb/v2` in firewalldb package
ViktorTigerstrom Jul 22, 2025
80520e6
multi: remove unused db code
ViktorTigerstrom Jul 23, 2025
c53e85b
mutli: rename `db.NewTestPostgresV2DB` function
ViktorTigerstrom Jul 23, 2025
2b27e82
sqlcmig6: add `sqlcmig6` package
ViktorTigerstrom Jul 10, 2025
b01e37d
accounts: add `SQLMig6Queries` to `accounts`
ViktorTigerstrom Jul 10, 2025
ae685ea
accounts: use `sqlcmig6` for kvdb to sql migration
ViktorTigerstrom Jul 10, 2025
592d117
session: add `SQLMig6Queries` to `session`
ViktorTigerstrom Jul 10, 2025
afc0008
session: use `sqlcmig6` for kvdb to sql migration
ViktorTigerstrom Jul 10, 2025
8c98988
firewalldb: add `SQLMig6Queries` to `firewalldb`
ViktorTigerstrom Jul 11, 2025
5dbf467
firewalldb: use queries to assert migration results
ViktorTigerstrom Jul 11, 2025
06b8f1c
firewalldb: use `sqlcmig6` for kvdb to sql migration
ViktorTigerstrom Jul 11, 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
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 SQLMig6Queries) 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 SQLMig6Queries) 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 SQLMig6Queries,
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 SQLMig6Queries,
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 SQLMig6Queries, 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 SQLMig6Queries) 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
25 changes: 12 additions & 13 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]) {
*SQLMig6QueriesExecutor[SQLMig6Queries]) {

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, NewSQLMig6QueriesExecutor(baseDB, queries)
}

assertMigrationResults := func(t *testing.T, sqlStore *SQLStore,
Expand Down Expand Up @@ -337,13 +332,17 @@ func TestAccountStoreMigration(t *testing.T) {
}

// Perform the migration.
//
// TODO(viktor): remove sqldb.MigrationTxOptions once
// sqldb v2 is based on the latest version of lnd/sqldb.
var opts sqldb.MigrationTxOptions
err = txEx.ExecTx(
ctx, sqldb.WriteTxOpt(),
func(tx SQLQueries) error {
ctx, &opts,
func(tx SQLMig6Queries) error {
return MigrateAccountStoreToSQL(
ctx, kvStore.db, tx,
)
},
}, sqldb.NoOpReset,
)
require.NoError(t, err)

Expand Down
Loading
Loading