@@ -11,8 +11,11 @@ import (
1111 "time"
1212
1313 "github.com/davecgh/go-spew/spew"
14- "github.com/lightninglabs/lightning-terminal/db/sqlc "
14+ "github.com/lightninglabs/lightning-terminal/db/sqlcmig6 "
1515 "github.com/lightningnetwork/lnd/kvdb"
16+ "github.com/lightningnetwork/lnd/lnrpc"
17+ "github.com/lightningnetwork/lnd/lntypes"
18+ "github.com/lightningnetwork/lnd/lnwire"
1619 "github.com/pmezard/go-difflib/difflib"
1720)
1821
2730// the KV database to the SQL database. The migration is done in a single
2831// transaction to ensure that all accounts are migrated or none at all.
2932func MigrateAccountStoreToSQL (ctx context.Context , kvStore kvdb.Backend ,
30- tx SQLQueries ) error {
33+ tx SQLMig6Queries ) error {
3134
3235 log .Infof ("Starting migration of the KV accounts store to SQL" )
3336
@@ -50,7 +53,7 @@ func MigrateAccountStoreToSQL(ctx context.Context, kvStore kvdb.Backend,
5053// to the SQL database. The migration is done in a single transaction to ensure
5154// that all accounts are migrated or none at all.
5255func migrateAccountsToSQL (ctx context.Context , kvStore kvdb.Backend ,
53- tx SQLQueries ) error {
56+ tx SQLMig6Queries ) error {
5457
5558 log .Infof ("Starting migration of accounts from KV to SQL" )
5659
@@ -68,7 +71,7 @@ func migrateAccountsToSQL(ctx context.Context, kvStore kvdb.Backend,
6871 kvAccount .ID , err )
6972 }
7073
71- migratedAccount , err := getAndMarshalAccount (
74+ migratedAccount , err := getAndMarshalMig6Account (
7275 ctx , tx , migratedAccountID ,
7376 )
7477 if err != nil {
@@ -151,17 +154,79 @@ func getBBoltAccounts(db kvdb.Backend) ([]*OffChainBalanceAccount, error) {
151154 return accounts , nil
152155}
153156
157+ // getAndMarshalAccount retrieves the account with the given ID. If the account
158+ // cannot be found, then ErrAccNotFound is returned.
159+ func getAndMarshalMig6Account (ctx context.Context , db SQLMig6Queries ,
160+ id int64 ) (* OffChainBalanceAccount , error ) {
161+
162+ dbAcct , err := db .GetAccount (ctx , id )
163+ if errors .Is (err , sql .ErrNoRows ) {
164+ return nil , ErrAccNotFound
165+ } else if err != nil {
166+ return nil , err
167+ }
168+
169+ return marshalDBMig6Account (ctx , db , dbAcct )
170+ }
171+
172+ func marshalDBMig6Account (ctx context.Context , db SQLMig6Queries ,
173+ dbAcct sqlcmig6.Account ) (* OffChainBalanceAccount , error ) {
174+
175+ alias , err := AccountIDFromInt64 (dbAcct .Alias )
176+ if err != nil {
177+ return nil , err
178+ }
179+
180+ account := & OffChainBalanceAccount {
181+ ID : alias ,
182+ Type : AccountType (dbAcct .Type ),
183+ InitialBalance : lnwire .MilliSatoshi (dbAcct .InitialBalanceMsat ),
184+ CurrentBalance : dbAcct .CurrentBalanceMsat ,
185+ LastUpdate : dbAcct .LastUpdated .UTC (),
186+ ExpirationDate : dbAcct .Expiration .UTC (),
187+ Invoices : make (AccountInvoices ),
188+ Payments : make (AccountPayments ),
189+ Label : dbAcct .Label .String ,
190+ }
191+
192+ invoices , err := db .ListAccountInvoices (ctx , dbAcct .ID )
193+ if err != nil {
194+ return nil , err
195+ }
196+ for _ , invoice := range invoices {
197+ var hash lntypes.Hash
198+ copy (hash [:], invoice .Hash )
199+ account .Invoices [hash ] = struct {}{}
200+ }
201+
202+ payments , err := db .ListAccountPayments (ctx , dbAcct .ID )
203+ if err != nil {
204+ return nil , err
205+ }
206+
207+ for _ , payment := range payments {
208+ var hash lntypes.Hash
209+ copy (hash [:], payment .Hash )
210+ account .Payments [hash ] = & PaymentEntry {
211+ Status : lnrpc .Payment_PaymentStatus (payment .Status ),
212+ FullAmount : lnwire .MilliSatoshi (payment .FullAmountMsat ),
213+ }
214+ }
215+
216+ return account , nil
217+ }
218+
154219// migrateSingleAccountToSQL runs the migration for a single account from the
155220// KV database to the SQL database.
156221func migrateSingleAccountToSQL (ctx context.Context ,
157- tx SQLQueries , account * OffChainBalanceAccount ) (int64 , error ) {
222+ tx SQLMig6Queries , account * OffChainBalanceAccount ) (int64 , error ) {
158223
159224 accountAlias , err := account .ID .ToInt64 ()
160225 if err != nil {
161226 return 0 , err
162227 }
163228
164- insertAccountParams := sqlc .InsertAccountParams {
229+ insertAccountParams := sqlcmig6 .InsertAccountParams {
165230 Type : int16 (account .Type ),
166231 InitialBalanceMsat : int64 (account .InitialBalance ),
167232 CurrentBalanceMsat : account .CurrentBalance ,
@@ -180,7 +245,7 @@ func migrateSingleAccountToSQL(ctx context.Context,
180245 }
181246
182247 for hash := range account .Invoices {
183- addInvoiceParams := sqlc .AddAccountInvoiceParams {
248+ addInvoiceParams := sqlcmig6 .AddAccountInvoiceParams {
184249 AccountID : sqlId ,
185250 Hash : hash [:],
186251 }
@@ -192,7 +257,7 @@ func migrateSingleAccountToSQL(ctx context.Context,
192257 }
193258
194259 for hash , paymentEntry := range account .Payments {
195- upsertPaymentParams := sqlc .UpsertAccountPaymentParams {
260+ upsertPaymentParams := sqlcmig6 .UpsertAccountPaymentParams {
196261 AccountID : sqlId ,
197262 Hash : hash [:],
198263 Status : int16 (paymentEntry .Status ),
@@ -211,7 +276,7 @@ func migrateSingleAccountToSQL(ctx context.Context,
211276// migrateAccountsIndicesToSQL runs the migration for the account indices from
212277// the KV database to the SQL database.
213278func migrateAccountsIndicesToSQL (ctx context.Context , kvStore kvdb.Backend ,
214- tx SQLQueries ) error {
279+ tx SQLMig6Queries ) error {
215280
216281 log .Infof ("Starting migration of accounts indices from KV to SQL" )
217282
@@ -233,7 +298,7 @@ func migrateAccountsIndicesToSQL(ctx context.Context, kvStore kvdb.Backend,
233298 settleIndexName , settleIndex )
234299 }
235300
236- setAddIndexParams := sqlc .SetAccountIndexParams {
301+ setAddIndexParams := sqlcmig6 .SetAccountIndexParams {
237302 Name : addIndexName ,
238303 Value : int64 (addIndex ),
239304 }
@@ -243,7 +308,7 @@ func migrateAccountsIndicesToSQL(ctx context.Context, kvStore kvdb.Backend,
243308 return err
244309 }
245310
246- setSettleIndexParams := sqlc .SetAccountIndexParams {
311+ setSettleIndexParams := sqlcmig6 .SetAccountIndexParams {
247312 Name : settleIndexName ,
248313 Value : int64 (settleIndex ),
249314 }
0 commit comments