Skip to content

Commit 084563c

Browse files
committed
sqldb+invoices: create a re-usable comparion helper function
So that we can use it for comparisons in other migrations too.
1 parent 5c09652 commit 084563c

File tree

4 files changed

+36
-29
lines changed

4 files changed

+36
-29
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ require (
140140
github.com/opencontainers/image-spec v1.0.2 // indirect
141141
github.com/opencontainers/runc v1.1.12 // indirect
142142
github.com/ory/dockertest/v3 v3.10.0 // indirect
143-
github.com/pmezard/go-difflib v1.0.0
143+
github.com/pmezard/go-difflib v1.0.0 // indirect
144144
github.com/prometheus/client_model v0.2.0 // indirect
145145
github.com/prometheus/common v0.26.0 // indirect
146146
github.com/prometheus/procfs v0.6.0 // indirect

invoices/sql_migration.go

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,14 @@ import (
66
"encoding/binary"
77
"errors"
88
"fmt"
9-
"reflect"
109
"strconv"
1110
"time"
1211

13-
"github.com/davecgh/go-spew/spew"
1412
"github.com/lightningnetwork/lnd/graph/db/models"
1513
"github.com/lightningnetwork/lnd/kvdb"
1614
"github.com/lightningnetwork/lnd/lntypes"
1715
"github.com/lightningnetwork/lnd/sqldb"
1816
"github.com/lightningnetwork/lnd/sqldb/sqlc"
19-
"github.com/pmezard/go-difflib/difflib"
2017
"golang.org/x/time/rate"
2118
)
2219

@@ -53,11 +50,6 @@ var (
5350
//
5451
// addIndexNo => invoiceKey
5552
addIndexBucket = []byte("invoice-add-index")
56-
57-
// ErrMigrationMismatch is returned when the migrated invoice does not
58-
// match the original invoice.
59-
ErrMigrationMismatch = fmt.Errorf("migrated invoice does not match " +
60-
"original invoice")
6153
)
6254

6355
// createInvoiceHashIndex generates a hash index that contains payment hashes
@@ -548,24 +540,9 @@ func migrateInvoices(ctx context.Context, tx *sqlc.Queries,
548540
// Override the add index before checking for equality.
549541
migratedInvoice.AddIndex = invoice.AddIndex
550542

551-
if !reflect.DeepEqual(invoice, *migratedInvoice) {
552-
diff := difflib.UnifiedDiff{
553-
A: difflib.SplitLines(
554-
spew.Sdump(invoice),
555-
),
556-
B: difflib.SplitLines(
557-
spew.Sdump(migratedInvoice),
558-
),
559-
FromFile: "Expected",
560-
FromDate: "",
561-
ToFile: "Actual",
562-
ToDate: "",
563-
Context: 3,
564-
}
565-
diffText, _ := difflib.GetUnifiedDiffString(diff)
566-
567-
return fmt.Errorf("%w: %v.\n%v", ErrMigrationMismatch,
568-
paymentHash, diffText)
543+
err = sqldb.CompareRecords(invoice, *migratedInvoice, "invoice")
544+
if err != nil {
545+
return err
569546
}
570547
}
571548

sqldb/go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@ module github.com/lightningnetwork/lnd/sqldb
22

33
require (
44
github.com/btcsuite/btclog/v2 v2.0.1-0.20250602222548-9967d19bb084
5+
github.com/davecgh/go-spew v1.1.1
56
github.com/golang-migrate/migrate/v4 v4.17.0
67
github.com/jackc/pgconn v1.14.3
78
github.com/jackc/pgerrcode v0.0.0-20240316143900-6e2875d9b438
89
github.com/jackc/pgx/v5 v5.3.1
910
github.com/ory/dockertest/v3 v3.10.0
11+
github.com/pmezard/go-difflib v1.0.0
1012
github.com/stretchr/testify v1.9.0
1113
golang.org/x/exp v0.0.0-20240325151524-a685a6edb6d8
1214
modernc.org/sqlite v1.29.10
@@ -19,7 +21,6 @@ require (
1921
github.com/btcsuite/btclog v0.0.0-20241003133417-09c4e92e319c // indirect
2022
github.com/cenkalti/backoff/v4 v4.1.3 // indirect
2123
github.com/containerd/continuity v0.3.0 // indirect
22-
github.com/davecgh/go-spew v1.1.1 // indirect
2324
github.com/docker/cli v20.10.17+incompatible // indirect
2425
github.com/docker/docker v24.0.7+incompatible // indirect
2526
github.com/docker/go-connections v0.4.0 // indirect
@@ -45,7 +46,6 @@ require (
4546
github.com/opencontainers/image-spec v1.0.2 // indirect
4647
github.com/opencontainers/runc v1.1.5 // indirect
4748
github.com/pkg/errors v0.9.1 // indirect
48-
github.com/pmezard/go-difflib v1.0.0 // indirect
4949
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
5050
github.com/rogpeppe/go-internal v1.12.0 // indirect
5151
github.com/sirupsen/logrus v1.9.2 // indirect

sqldb/migrations.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,17 @@ import (
99
"io"
1010
"io/fs"
1111
"net/http"
12+
"reflect"
1213
"strings"
1314
"time"
1415

1516
"github.com/btcsuite/btclog/v2"
17+
"github.com/davecgh/go-spew/spew"
1618
"github.com/golang-migrate/migrate/v4"
1719
"github.com/golang-migrate/migrate/v4/database"
1820
"github.com/golang-migrate/migrate/v4/source/httpfs"
1921
"github.com/lightningnetwork/lnd/sqldb/sqlc"
22+
"github.com/pmezard/go-difflib/difflib"
2023
)
2124

2225
var (
@@ -71,6 +74,11 @@ var (
7174
// user if necessary.
7275
},
7376
}, migrationAdditions...)
77+
78+
// ErrMigrationMismatch is returned when a migrated record does not
79+
// match the original record.
80+
ErrMigrationMismatch = fmt.Errorf("migrated record does not match " +
81+
"original record")
7482
)
7583

7684
// MigrationConfig is a configuration struct that describes SQL migrations. Each
@@ -472,3 +480,25 @@ func ApplyMigrations(ctx context.Context, db *BaseDB,
472480

473481
return nil
474482
}
483+
484+
// CompareRecords checks if the original and migrated objects are equal. If
485+
// they are not, it returns an error with a unified diff of the two objects.
486+
func CompareRecords(original, migrated any, identifier string) error {
487+
if reflect.DeepEqual(original, migrated) {
488+
return nil
489+
}
490+
491+
diff := difflib.UnifiedDiff{
492+
A: difflib.SplitLines(spew.Sdump(original)),
493+
B: difflib.SplitLines(spew.Sdump(migrated)),
494+
FromFile: "Expected",
495+
FromDate: "",
496+
ToFile: "Actual",
497+
ToDate: "",
498+
Context: 3,
499+
}
500+
diffText, _ := difflib.GetUnifiedDiffString(diff)
501+
502+
return fmt.Errorf("%w: %s.\n%v", ErrMigrationMismatch, identifier,
503+
diffText)
504+
}

0 commit comments

Comments
 (0)