• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

lightningnetwork / lnd / 16466354971

23 Jul 2025 09:05AM UTC coverage: 57.54% (-9.7%) from 67.201%
16466354971

Pull #9455

github

web-flow
Merge f914ae23c into 90e211684
Pull Request #9455: discovery+lnwire: add support for DNS host name in NodeAnnouncement msg

151 of 291 new or added lines in 7 files covered. (51.89%)

28441 existing lines in 456 files now uncovered.

98864 of 171817 relevant lines covered (57.54%)

1.79 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/invoices/sql_migration.go
1
package invoices
2

3
import (
4
        "bytes"
5
        "context"
6
        "encoding/binary"
7
        "errors"
8
        "fmt"
9
        "strconv"
10
        "time"
11

12
        "github.com/lightningnetwork/lnd/graph/db/models"
13
        "github.com/lightningnetwork/lnd/kvdb"
14
        "github.com/lightningnetwork/lnd/lntypes"
15
        "github.com/lightningnetwork/lnd/sqldb"
16
        "github.com/lightningnetwork/lnd/sqldb/sqlc"
17
        "golang.org/x/time/rate"
18
)
19

20
var (
21
        // invoiceBucket is the name of the bucket within the database that
22
        // stores all data related to invoices no matter their final state.
23
        // Within the invoice bucket, each invoice is keyed by its invoice ID
24
        // which is a monotonically increasing uint32.
25
        invoiceBucket = []byte("invoices")
26

27
        // invoiceIndexBucket  is the name of the sub-bucket within the
28
        // invoiceBucket which indexes all invoices by their payment hash. The
29
        // payment hash is the sha256 of the invoice's payment preimage. This
30
        // index is used to detect duplicates, and also to provide a fast path
31
        // for looking up incoming HTLCs to determine if we're able to settle
32
        // them fully.
33
        //
34
        // maps: payHash => invoiceKey
35
        invoiceIndexBucket = []byte("paymenthashes")
36

37
        // numInvoicesKey is the name of key which houses the auto-incrementing
38
        // invoice ID which is essentially used as a primary key. With each
39
        // invoice inserted, the primary key is incremented by one. This key is
40
        // stored within the invoiceIndexBucket. Within the invoiceBucket
41
        // invoices are uniquely identified by the invoice ID.
42
        numInvoicesKey = []byte("nik")
43

44
        // addIndexBucket is an index bucket that we'll use to create a
45
        // monotonically increasing set of add indexes. Each time we add a new
46
        // invoice, this sequence number will be incremented and then populated
47
        // within the new invoice.
48
        //
49
        // In addition to this sequence number, we map:
50
        //
51
        //   addIndexNo => invoiceKey
52
        addIndexBucket = []byte("invoice-add-index")
53
)
54

55
// createInvoiceHashIndex generates a hash index that contains payment hashes
56
// for each invoice in the database. Retrieving the payment hash for certain
57
// invoices, such as those created for spontaneous AMP payments, can be
58
// challenging because the hash is not directly derivable from the invoice's
59
// parameters and is stored separately in the `paymenthashes` bucket. This
60
// bucket maps payment hashes to invoice keys, but for migration purposes, we
61
// need the ability to query in the reverse direction. This function establishes
62
// a new index in the SQL database that maps each invoice key to its
63
// corresponding payment hash.
64
func createInvoiceHashIndex(ctx context.Context, db kvdb.Backend,
UNCOV
65
        tx *sqlc.Queries) error {
×
UNCOV
66

×
UNCOV
67
        return db.View(func(kvTx kvdb.RTx) error {
×
UNCOV
68
                invoices := kvTx.ReadBucket(invoiceBucket)
×
UNCOV
69
                if invoices == nil {
×
70
                        return ErrNoInvoicesCreated
×
71
                }
×
72

UNCOV
73
                invoiceIndex := invoices.NestedReadBucket(
×
UNCOV
74
                        invoiceIndexBucket,
×
UNCOV
75
                )
×
UNCOV
76
                if invoiceIndex == nil {
×
UNCOV
77
                        return ErrNoInvoicesCreated
×
UNCOV
78
                }
×
79

80
                addIndex := invoices.NestedReadBucket(addIndexBucket)
×
81
                if addIndex == nil {
×
82
                        return ErrNoInvoicesCreated
×
83
                }
×
84

85
                // First, iterate over all elements in the add index bucket and
86
                // insert the add index value for the corresponding invoice key
87
                // in the payment_hashes table.
88
                err := addIndex.ForEach(func(k, v []byte) error {
×
89
                        // The key is the add index, and the value is
×
90
                        // the invoice key.
×
91
                        addIndexNo := binary.BigEndian.Uint64(k)
×
92
                        invoiceKey := binary.BigEndian.Uint32(v)
×
93

×
94
                        return tx.InsertKVInvoiceKeyAndAddIndex(ctx,
×
95
                                sqlc.InsertKVInvoiceKeyAndAddIndexParams{
×
96
                                        ID:       int64(invoiceKey),
×
97
                                        AddIndex: int64(addIndexNo),
×
98
                                },
×
99
                        )
×
100
                })
×
101
                if err != nil {
×
102
                        return err
×
103
                }
×
104

105
                // Next, iterate over all hashes in the invoice index bucket and
106
                // set the hash to the corresponding the invoice key in the
107
                // payment_hashes table.
108
                return invoiceIndex.ForEach(func(k, v []byte) error {
×
109
                        // Skip the special numInvoicesKey as that does
×
110
                        // not point to a valid invoice.
×
111
                        if bytes.Equal(k, numInvoicesKey) {
×
112
                                return nil
×
113
                        }
×
114

115
                        // The key is the payment hash, and the value
116
                        // is the invoice key.
117
                        if len(k) != lntypes.HashSize {
×
118
                                return fmt.Errorf("invalid payment "+
×
119
                                        "hash length: expected %v, "+
×
120
                                        "got %v", lntypes.HashSize,
×
121
                                        len(k))
×
122
                        }
×
123

124
                        invoiceKey := binary.BigEndian.Uint32(v)
×
125

×
126
                        return tx.SetKVInvoicePaymentHash(ctx,
×
127
                                sqlc.SetKVInvoicePaymentHashParams{
×
128
                                        ID:   int64(invoiceKey),
×
129
                                        Hash: k,
×
130
                                },
×
131
                        )
×
132
                })
UNCOV
133
        }, func() {})
×
134
}
135

136
// toInsertMigratedInvoiceParams creates the parameters for inserting a migrated
137
// invoice into the SQL database. The parameters are derived from the original
138
// invoice insert parameters.
139
func toInsertMigratedInvoiceParams(
UNCOV
140
        params sqlc.InsertInvoiceParams) sqlc.InsertMigratedInvoiceParams {
×
UNCOV
141

×
UNCOV
142
        return sqlc.InsertMigratedInvoiceParams{
×
UNCOV
143
                Hash:               params.Hash,
×
UNCOV
144
                Preimage:           params.Preimage,
×
UNCOV
145
                Memo:               params.Memo,
×
UNCOV
146
                AmountMsat:         params.AmountMsat,
×
UNCOV
147
                CltvDelta:          params.CltvDelta,
×
UNCOV
148
                Expiry:             params.Expiry,
×
UNCOV
149
                PaymentAddr:        params.PaymentAddr,
×
UNCOV
150
                PaymentRequest:     params.PaymentRequest,
×
UNCOV
151
                PaymentRequestHash: params.PaymentRequestHash,
×
UNCOV
152
                State:              params.State,
×
UNCOV
153
                AmountPaidMsat:     params.AmountPaidMsat,
×
UNCOV
154
                IsAmp:              params.IsAmp,
×
UNCOV
155
                IsHodl:             params.IsHodl,
×
UNCOV
156
                IsKeysend:          params.IsKeysend,
×
UNCOV
157
                CreatedAt:          params.CreatedAt,
×
UNCOV
158
        }
×
UNCOV
159
}
×
160

161
// MigrateSingleInvoice migrates a single invoice to the new SQL schema. Note
162
// that perfect equality between the old and new schemas is not achievable, as
163
// the invoice's add index cannot be mapped directly to its ID due to SQL’s
164
// auto-incrementing primary key. The ID returned from the insert will instead
165
// serve as the add index in the new schema.
166
func MigrateSingleInvoice(ctx context.Context, tx SQLInvoiceQueries,
UNCOV
167
        invoice *Invoice, paymentHash lntypes.Hash) error {
×
UNCOV
168

×
UNCOV
169
        insertInvoiceParams, err := makeInsertInvoiceParams(
×
UNCOV
170
                invoice, paymentHash,
×
UNCOV
171
        )
×
UNCOV
172
        if err != nil {
×
173
                return err
×
174
        }
×
175

176
        // Convert the insert invoice parameters to the migrated invoice insert
177
        // parameters.
UNCOV
178
        insertMigratedInvoiceParams := toInsertMigratedInvoiceParams(
×
UNCOV
179
                insertInvoiceParams,
×
UNCOV
180
        )
×
UNCOV
181

×
UNCOV
182
        // If the invoice is settled, we'll also set the timestamp and the index
×
UNCOV
183
        // at which it was settled.
×
UNCOV
184
        if invoice.State == ContractSettled {
×
UNCOV
185
                if invoice.SettleIndex == 0 {
×
186
                        return fmt.Errorf("settled invoice %s missing settle "+
×
187
                                "index", paymentHash)
×
188
                }
×
189

UNCOV
190
                if invoice.SettleDate.IsZero() {
×
191
                        return fmt.Errorf("settled invoice %s missing settle "+
×
192
                                "date", paymentHash)
×
193
                }
×
194

UNCOV
195
                insertMigratedInvoiceParams.SettleIndex = sqldb.SQLInt64(
×
UNCOV
196
                        invoice.SettleIndex,
×
UNCOV
197
                )
×
UNCOV
198
                insertMigratedInvoiceParams.SettledAt = sqldb.SQLTime(
×
UNCOV
199
                        invoice.SettleDate.UTC(),
×
UNCOV
200
                )
×
201
        }
202

203
        // First we need to insert the invoice itself so we can use the "add
204
        // index" which in this case is the auto incrementing primary key that
205
        // is returned from the insert.
UNCOV
206
        invoiceID, err := tx.InsertMigratedInvoice(
×
UNCOV
207
                ctx, insertMigratedInvoiceParams,
×
UNCOV
208
        )
×
UNCOV
209
        if err != nil {
×
210
                return fmt.Errorf("unable to insert invoice: %w", err)
×
211
        }
×
212

213
        // Insert the invoice's features.
UNCOV
214
        for feature := range invoice.Terms.Features.Features() {
×
UNCOV
215
                params := sqlc.InsertInvoiceFeatureParams{
×
UNCOV
216
                        InvoiceID: invoiceID,
×
UNCOV
217
                        Feature:   int32(feature),
×
UNCOV
218
                }
×
UNCOV
219

×
UNCOV
220
                err := tx.InsertInvoiceFeature(ctx, params)
×
UNCOV
221
                if err != nil {
×
222
                        return fmt.Errorf("unable to insert invoice "+
×
223
                                "feature(%v): %w", feature, err)
×
224
                }
×
225
        }
226

UNCOV
227
        sqlHtlcIDs := make(map[models.CircuitKey]int64)
×
UNCOV
228

×
UNCOV
229
        // Now insert the HTLCs of the invoice. We'll also keep track of the SQL
×
UNCOV
230
        // ID of each HTLC so we can use it when inserting the AMP sub invoices.
×
UNCOV
231
        for circuitKey, htlc := range invoice.Htlcs {
×
UNCOV
232
                htlcParams := sqlc.InsertInvoiceHTLCParams{
×
UNCOV
233
                        HtlcID: int64(circuitKey.HtlcID),
×
UNCOV
234
                        ChanID: strconv.FormatUint(
×
UNCOV
235
                                circuitKey.ChanID.ToUint64(), 10,
×
UNCOV
236
                        ),
×
UNCOV
237
                        AmountMsat:   int64(htlc.Amt),
×
UNCOV
238
                        AcceptHeight: int32(htlc.AcceptHeight),
×
UNCOV
239
                        AcceptTime:   htlc.AcceptTime.UTC(),
×
UNCOV
240
                        ExpiryHeight: int32(htlc.Expiry),
×
UNCOV
241
                        State:        int16(htlc.State),
×
UNCOV
242
                        InvoiceID:    invoiceID,
×
UNCOV
243
                }
×
UNCOV
244

×
UNCOV
245
                // Leave the MPP amount as NULL if the MPP total amount is zero.
×
UNCOV
246
                if htlc.MppTotalAmt != 0 {
×
UNCOV
247
                        htlcParams.TotalMppMsat = sqldb.SQLInt64(
×
UNCOV
248
                                int64(htlc.MppTotalAmt),
×
UNCOV
249
                        )
×
UNCOV
250
                }
×
251

252
                // Leave the resolve time as NULL if the HTLC is not resolved.
UNCOV
253
                if !htlc.ResolveTime.IsZero() {
×
UNCOV
254
                        htlcParams.ResolveTime = sqldb.SQLTime(
×
UNCOV
255
                                htlc.ResolveTime.UTC(),
×
UNCOV
256
                        )
×
UNCOV
257
                }
×
258

UNCOV
259
                sqlID, err := tx.InsertInvoiceHTLC(ctx, htlcParams)
×
UNCOV
260
                if err != nil {
×
261
                        return fmt.Errorf("unable to insert invoice htlc: %w",
×
262
                                err)
×
263
                }
×
264

UNCOV
265
                sqlHtlcIDs[circuitKey] = sqlID
×
UNCOV
266

×
UNCOV
267
                // Store custom records.
×
UNCOV
268
                for key, value := range htlc.CustomRecords {
×
UNCOV
269
                        err = tx.InsertInvoiceHTLCCustomRecord(
×
UNCOV
270
                                ctx, sqlc.InsertInvoiceHTLCCustomRecordParams{
×
UNCOV
271
                                        Key:    int64(key),
×
UNCOV
272
                                        Value:  value,
×
UNCOV
273
                                        HtlcID: sqlID,
×
UNCOV
274
                                },
×
UNCOV
275
                        )
×
UNCOV
276
                        if err != nil {
×
277
                                return err
×
278
                        }
×
279
                }
280
        }
281

UNCOV
282
        if !invoice.IsAMP() {
×
UNCOV
283
                return nil
×
UNCOV
284
        }
×
285

UNCOV
286
        for setID, ampState := range invoice.AMPState {
×
UNCOV
287
                // Find the earliest HTLC of the AMP invoice, which will
×
UNCOV
288
                // be used as the creation date of this sub invoice.
×
UNCOV
289
                var createdAt time.Time
×
UNCOV
290
                for circuitKey := range ampState.InvoiceKeys {
×
UNCOV
291
                        htlc := invoice.Htlcs[circuitKey]
×
UNCOV
292
                        if createdAt.IsZero() {
×
UNCOV
293
                                createdAt = htlc.AcceptTime.UTC()
×
UNCOV
294
                                continue
×
295
                        }
296

UNCOV
297
                        if createdAt.After(htlc.AcceptTime) {
×
UNCOV
298
                                createdAt = htlc.AcceptTime.UTC()
×
UNCOV
299
                        }
×
300
                }
301

UNCOV
302
                params := sqlc.InsertAMPSubInvoiceParams{
×
UNCOV
303
                        SetID:     setID[:],
×
UNCOV
304
                        State:     int16(ampState.State),
×
UNCOV
305
                        CreatedAt: createdAt,
×
UNCOV
306
                        InvoiceID: invoiceID,
×
UNCOV
307
                }
×
UNCOV
308

×
UNCOV
309
                if ampState.SettleIndex != 0 {
×
UNCOV
310
                        if ampState.SettleDate.IsZero() {
×
311
                                return fmt.Errorf("settled AMP sub invoice %x "+
×
312
                                        "missing settle date", setID)
×
313
                        }
×
314

UNCOV
315
                        params.SettledAt = sqldb.SQLTime(
×
UNCOV
316
                                ampState.SettleDate.UTC(),
×
UNCOV
317
                        )
×
UNCOV
318

×
UNCOV
319
                        params.SettleIndex = sqldb.SQLInt64(
×
UNCOV
320
                                ampState.SettleIndex,
×
UNCOV
321
                        )
×
322
                }
323

UNCOV
324
                err := tx.InsertAMPSubInvoice(ctx, params)
×
UNCOV
325
                if err != nil {
×
326
                        return fmt.Errorf("unable to insert AMP sub invoice: "+
×
327
                                "%w", err)
×
328
                }
×
329

330
                // Now we can add the AMP HTLCs to the database.
UNCOV
331
                for circuitKey := range ampState.InvoiceKeys {
×
UNCOV
332
                        htlc := invoice.Htlcs[circuitKey]
×
UNCOV
333
                        rootShare := htlc.AMP.Record.RootShare()
×
UNCOV
334

×
UNCOV
335
                        sqlHtlcID, ok := sqlHtlcIDs[circuitKey]
×
UNCOV
336
                        if !ok {
×
337
                                return fmt.Errorf("missing htlc for AMP htlc: "+
×
338
                                        "%v", circuitKey)
×
339
                        }
×
340

UNCOV
341
                        params := sqlc.InsertAMPSubInvoiceHTLCParams{
×
UNCOV
342
                                InvoiceID:  invoiceID,
×
UNCOV
343
                                SetID:      setID[:],
×
UNCOV
344
                                HtlcID:     sqlHtlcID,
×
UNCOV
345
                                RootShare:  rootShare[:],
×
UNCOV
346
                                ChildIndex: int64(htlc.AMP.Record.ChildIndex()),
×
UNCOV
347
                                Hash:       htlc.AMP.Hash[:],
×
UNCOV
348
                        }
×
UNCOV
349

×
UNCOV
350
                        if htlc.AMP.Preimage != nil {
×
UNCOV
351
                                params.Preimage = htlc.AMP.Preimage[:]
×
UNCOV
352
                        }
×
353

UNCOV
354
                        err = tx.InsertAMPSubInvoiceHTLC(ctx, params)
×
UNCOV
355
                        if err != nil {
×
356
                                return fmt.Errorf("unable to insert AMP sub "+
×
357
                                        "invoice: %w", err)
×
358
                        }
×
359
                }
360
        }
361

UNCOV
362
        return nil
×
363
}
364

365
// OverrideInvoiceTimeZone overrides the time zone of the invoice to the local
366
// time zone and chops off the nanosecond part for comparison. This is needed
367
// because KV database stores times as-is which as an unwanted side effect would
368
// fail migration due to time comparison expecting both the original and
369
// migrated invoices to be in the same local time zone and in microsecond
370
// precision. Note that PostgreSQL stores times in microsecond precision while
371
// SQLite can store times in nanosecond precision if using TEXT storage class.
UNCOV
372
func OverrideInvoiceTimeZone(invoice *Invoice) {
×
UNCOV
373
        fixTime := func(t time.Time) time.Time {
×
UNCOV
374
                return t.In(time.Local).Truncate(time.Microsecond)
×
UNCOV
375
        }
×
376

UNCOV
377
        invoice.CreationDate = fixTime(invoice.CreationDate)
×
UNCOV
378

×
UNCOV
379
        if !invoice.SettleDate.IsZero() {
×
UNCOV
380
                invoice.SettleDate = fixTime(invoice.SettleDate)
×
UNCOV
381
        }
×
382

UNCOV
383
        if invoice.IsAMP() {
×
UNCOV
384
                for setID, ampState := range invoice.AMPState {
×
UNCOV
385
                        if ampState.SettleDate.IsZero() {
×
UNCOV
386
                                continue
×
387
                        }
388

UNCOV
389
                        ampState.SettleDate = fixTime(ampState.SettleDate)
×
UNCOV
390
                        invoice.AMPState[setID] = ampState
×
391
                }
392
        }
393

UNCOV
394
        for _, htlc := range invoice.Htlcs {
×
UNCOV
395
                if !htlc.AcceptTime.IsZero() {
×
UNCOV
396
                        htlc.AcceptTime = fixTime(htlc.AcceptTime)
×
UNCOV
397
                }
×
398

UNCOV
399
                if !htlc.ResolveTime.IsZero() {
×
UNCOV
400
                        htlc.ResolveTime = fixTime(htlc.ResolveTime)
×
UNCOV
401
                }
×
402
        }
403
}
404

405
// MigrateInvoicesToSQL runs the migration of all invoices from the KV database
406
// to the SQL database. The migration is done in a single transaction to ensure
407
// that all invoices are migrated or none at all. This function can be run
408
// multiple times without causing any issues as it will check if the migration
409
// has already been performed.
410
func MigrateInvoicesToSQL(ctx context.Context, db kvdb.Backend,
UNCOV
411
        kvStore InvoiceDB, tx *sqlc.Queries, batchSize int) error {
×
UNCOV
412

×
UNCOV
413
        log.Infof("Starting migration of invoices from KV to SQL")
×
UNCOV
414

×
UNCOV
415
        offset := uint64(0)
×
UNCOV
416
        t0 := time.Now()
×
UNCOV
417

×
UNCOV
418
        // Create the hash index which we will use to look up invoice
×
UNCOV
419
        // payment hashes by their add index during migration.
×
UNCOV
420
        err := createInvoiceHashIndex(ctx, db, tx)
×
UNCOV
421
        if err != nil && !errors.Is(err, ErrNoInvoicesCreated) {
×
422
                log.Errorf("Unable to create invoice hash index: %v",
×
423
                        err)
×
424

×
425
                return err
×
426
        }
×
UNCOV
427
        log.Debugf("Created SQL invoice hash index in %v", time.Since(t0))
×
UNCOV
428

×
UNCOV
429
        s := rate.Sometimes{
×
UNCOV
430
                Interval: 30 * time.Second,
×
UNCOV
431
        }
×
UNCOV
432

×
UNCOV
433
        t0 = time.Now()
×
UNCOV
434
        chunk := 0
×
UNCOV
435
        total := 0
×
UNCOV
436

×
UNCOV
437
        // Now we can start migrating the invoices. We'll do this in
×
UNCOV
438
        // batches to reduce memory usage.
×
UNCOV
439
        for {
×
UNCOV
440
                query := InvoiceQuery{
×
UNCOV
441
                        IndexOffset:    offset,
×
UNCOV
442
                        NumMaxInvoices: uint64(batchSize),
×
UNCOV
443
                }
×
UNCOV
444

×
UNCOV
445
                queryResult, err := kvStore.QueryInvoices(ctx, query)
×
UNCOV
446
                if err != nil && !errors.Is(err, ErrNoInvoicesCreated) {
×
447
                        return fmt.Errorf("unable to query invoices: %w", err)
×
448
                }
×
449

UNCOV
450
                if len(queryResult.Invoices) == 0 {
×
UNCOV
451
                        log.Infof("All invoices migrated. Total: %d", total)
×
UNCOV
452
                        break
×
453
                }
454

455
                err = migrateInvoices(ctx, tx, queryResult.Invoices)
×
456
                if err != nil {
×
457
                        return err
×
458
                }
×
459

460
                offset = queryResult.LastIndexOffset
×
461
                resultCnt := len(queryResult.Invoices)
×
462
                total += resultCnt
×
463
                chunk += resultCnt
×
464

×
465
                s.Do(func() {
×
466
                        elapsed := time.Since(t0).Seconds()
×
467
                        ratePerSec := float64(chunk) / elapsed
×
468
                        log.Debugf("Migrated %d invoices (%.2f invoices/sec)",
×
469
                                total, ratePerSec)
×
470

×
471
                        t0 = time.Now()
×
472
                        chunk = 0
×
473
                })
×
474
        }
475

476
        // Clean up the hash index as it's no longer needed.
UNCOV
477
        err = tx.ClearKVInvoiceHashIndex(ctx)
×
UNCOV
478
        if err != nil {
×
479
                return fmt.Errorf("unable to clear invoice hash "+
×
480
                        "index: %w", err)
×
481
        }
×
482

UNCOV
483
        log.Infof("Migration of %d invoices from KV to SQL completed", total)
×
UNCOV
484

×
UNCOV
485
        return nil
×
486
}
487

488
func migrateInvoices(ctx context.Context, tx *sqlc.Queries,
489
        invoices []Invoice) error {
×
490

×
491
        for i, invoice := range invoices {
×
492
                var paymentHash lntypes.Hash
×
493
                if invoice.Terms.PaymentPreimage != nil {
×
494
                        paymentHash = invoice.Terms.PaymentPreimage.Hash()
×
495
                } else {
×
496
                        paymentHashBytes, err :=
×
497
                                tx.GetKVInvoicePaymentHashByAddIndex(
×
498
                                        ctx, int64(invoice.AddIndex),
×
499
                                )
×
500
                        if err != nil {
×
501
                                // This would be an unexpected inconsistency
×
502
                                // in the kv database. We can't do much here
×
503
                                // so we'll notify the user and continue.
×
504
                                log.Warnf("Cannot migrate invoice, unable to "+
×
505
                                        "fetch payment hash (add_index=%v): %v",
×
506
                                        invoice.AddIndex, err)
×
507

×
508
                                continue
×
509
                        }
510

511
                        copy(paymentHash[:], paymentHashBytes)
×
512
                }
513

514
                err := MigrateSingleInvoice(ctx, tx, &invoices[i], paymentHash)
×
515
                if err != nil {
×
516
                        return fmt.Errorf("unable to migrate invoice(%v): %w",
×
517
                                paymentHash, err)
×
518
                }
×
519

520
                migratedInvoice, err := fetchInvoice(
×
521
                        ctx, tx, InvoiceRefByHash(paymentHash),
×
522
                )
×
523
                if err != nil {
×
524
                        return fmt.Errorf("unable to fetch migrated "+
×
525
                                "invoice(%v): %w", paymentHash, err)
×
526
                }
×
527

528
                // Override the time zone for comparison. Note that we need to
529
                // override both invoices as the original invoice is coming from
530
                // KV database, it was stored as a binary serialized Go
531
                // time.Time value which has nanosecond precision but might have
532
                // been created in a different time zone. The migrated invoice
533
                // is stored in SQL in UTC and selected in the local time zone,
534
                // however in PostgreSQL it has microsecond precision while in
535
                // SQLite it has nanosecond precision if using TEXT storage
536
                // class.
537
                OverrideInvoiceTimeZone(&invoice)
×
538
                OverrideInvoiceTimeZone(migratedInvoice)
×
539

×
540
                // Override the add index before checking for equality.
×
541
                migratedInvoice.AddIndex = invoice.AddIndex
×
542

×
543
                err = sqldb.CompareRecords(invoice, *migratedInvoice, "invoice")
×
544
                if err != nil {
×
545
                        return err
×
546
                }
×
547
        }
548

549
        return nil
×
550
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc