@@ -11,8 +11,11 @@ import (
11
11
"time"
12
12
13
13
"github.com/davecgh/go-spew/spew"
14
- "github.com/lightninglabs/lightning-terminal/db/sqlc "
14
+ "github.com/lightninglabs/lightning-terminal/db/sqlcmig6 "
15
15
"github.com/lightningnetwork/lnd/kvdb"
16
+ "github.com/lightningnetwork/lnd/lnrpc"
17
+ "github.com/lightningnetwork/lnd/lntypes"
18
+ "github.com/lightningnetwork/lnd/lnwire"
16
19
"github.com/pmezard/go-difflib/difflib"
17
20
)
18
21
27
30
// the KV database to the SQL database. The migration is done in a single
28
31
// transaction to ensure that all accounts are migrated or none at all.
29
32
func MigrateAccountStoreToSQL (ctx context.Context , kvStore kvdb.Backend ,
30
- tx SQLQueries ) error {
33
+ tx SQLMig6Queries ) error {
31
34
32
35
log .Infof ("Starting migration of the KV accounts store to SQL" )
33
36
@@ -50,7 +53,7 @@ func MigrateAccountStoreToSQL(ctx context.Context, kvStore kvdb.Backend,
50
53
// to the SQL database. The migration is done in a single transaction to ensure
51
54
// that all accounts are migrated or none at all.
52
55
func migrateAccountsToSQL (ctx context.Context , kvStore kvdb.Backend ,
53
- tx SQLQueries ) error {
56
+ tx SQLMig6Queries ) error {
54
57
55
58
log .Infof ("Starting migration of accounts from KV to SQL" )
56
59
@@ -68,7 +71,7 @@ func migrateAccountsToSQL(ctx context.Context, kvStore kvdb.Backend,
68
71
kvAccount .ID , err )
69
72
}
70
73
71
- migratedAccount , err := getAndMarshalAccount (
74
+ migratedAccount , err := getAndMarshalMig6Account (
72
75
ctx , tx , migratedAccountID ,
73
76
)
74
77
if err != nil {
@@ -151,17 +154,79 @@ func getBBoltAccounts(db kvdb.Backend) ([]*OffChainBalanceAccount, error) {
151
154
return accounts , nil
152
155
}
153
156
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
+
154
219
// migrateSingleAccountToSQL runs the migration for a single account from the
155
220
// KV database to the SQL database.
156
221
func migrateSingleAccountToSQL (ctx context.Context ,
157
- tx SQLQueries , account * OffChainBalanceAccount ) (int64 , error ) {
222
+ tx SQLMig6Queries , account * OffChainBalanceAccount ) (int64 , error ) {
158
223
159
224
accountAlias , err := account .ID .ToInt64 ()
160
225
if err != nil {
161
226
return 0 , err
162
227
}
163
228
164
- insertAccountParams := sqlc .InsertAccountParams {
229
+ insertAccountParams := sqlcmig6 .InsertAccountParams {
165
230
Type : int16 (account .Type ),
166
231
InitialBalanceMsat : int64 (account .InitialBalance ),
167
232
CurrentBalanceMsat : account .CurrentBalance ,
@@ -180,7 +245,7 @@ func migrateSingleAccountToSQL(ctx context.Context,
180
245
}
181
246
182
247
for hash := range account .Invoices {
183
- addInvoiceParams := sqlc .AddAccountInvoiceParams {
248
+ addInvoiceParams := sqlcmig6 .AddAccountInvoiceParams {
184
249
AccountID : sqlId ,
185
250
Hash : hash [:],
186
251
}
@@ -192,7 +257,7 @@ func migrateSingleAccountToSQL(ctx context.Context,
192
257
}
193
258
194
259
for hash , paymentEntry := range account .Payments {
195
- upsertPaymentParams := sqlc .UpsertAccountPaymentParams {
260
+ upsertPaymentParams := sqlcmig6 .UpsertAccountPaymentParams {
196
261
AccountID : sqlId ,
197
262
Hash : hash [:],
198
263
Status : int16 (paymentEntry .Status ),
@@ -211,7 +276,7 @@ func migrateSingleAccountToSQL(ctx context.Context,
211
276
// migrateAccountsIndicesToSQL runs the migration for the account indices from
212
277
// the KV database to the SQL database.
213
278
func migrateAccountsIndicesToSQL (ctx context.Context , kvStore kvdb.Backend ,
214
- tx SQLQueries ) error {
279
+ tx SQLMig6Queries ) error {
215
280
216
281
log .Infof ("Starting migration of accounts indices from KV to SQL" )
217
282
@@ -233,7 +298,7 @@ func migrateAccountsIndicesToSQL(ctx context.Context, kvStore kvdb.Backend,
233
298
settleIndexName , settleIndex )
234
299
}
235
300
236
- setAddIndexParams := sqlc .SetAccountIndexParams {
301
+ setAddIndexParams := sqlcmig6 .SetAccountIndexParams {
237
302
Name : addIndexName ,
238
303
Value : int64 (addIndex ),
239
304
}
@@ -243,7 +308,7 @@ func migrateAccountsIndicesToSQL(ctx context.Context, kvStore kvdb.Backend,
243
308
return err
244
309
}
245
310
246
- setSettleIndexParams := sqlc .SetAccountIndexParams {
311
+ setSettleIndexParams := sqlcmig6 .SetAccountIndexParams {
247
312
Name : settleIndexName ,
248
313
Value : int64 (settleIndex ),
249
314
}
0 commit comments