sqldb+invoices: create a re-usable comparion helper function

So that we can use it for comparisons in other migrations too.
This commit is contained in:
Elle Mouton committed 2025-07-07 18:41:46 +02:00
1 parent 5c09652e1a
commit 084563c5c0
4 files changed
+36 -29

No files matched your search

+2 -2
View File
@@ -2,11 +2,13 @@ module github.com/lightningnetwork/lnd/sqldb
require (
github.com/btcsuite/btclog/v2 v2.0.1-0.20250602222548-9967d19bb084
github.com/davecgh/go-spew v1.1.1
github.com/golang-migrate/migrate/v4 v4.17.0
github.com/jackc/pgconn v1.14.3
github.com/jackc/pgerrcode v0.0.0-20240316143900-6e2875d9b438
github.com/jackc/pgx/v5 v5.3.1
github.com/ory/dockertest/v3 v3.10.0
github.com/pmezard/go-difflib v1.0.0
github.com/stretchr/testify v1.9.0
golang.org/x/exp v0.0.0-20240325151524-a685a6edb6d8
modernc.org/sqlite v1.29.10
@@ -19,7 +21,6 @@ require (
github.com/btcsuite/btclog v0.0.0-20241003133417-09c4e92e319c // indirect
github.com/cenkalti/backoff/v4 v4.1.3 // indirect
github.com/containerd/continuity v0.3.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/docker/cli v20.10.17+incompatible // indirect
github.com/docker/docker v24.0.7+incompatible // indirect
github.com/docker/go-connections v0.4.0 // indirect
@@ -45,7 +46,6 @@ require (
github.com/opencontainers/image-spec v1.0.2 // indirect
github.com/opencontainers/runc v1.1.5 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
github.com/rogpeppe/go-internal v1.12.0 // indirect
github.com/sirupsen/logrus v1.9.2 // indirect
+30
View File
@@ -9,14 +9,17 @@ import (
"io"
"io/fs"
"net/http"
"reflect"
"strings"
"time"
"github.com/btcsuite/btclog/v2"
"github.com/davecgh/go-spew/spew"
"github.com/golang-migrate/migrate/v4"
"github.com/golang-migrate/migrate/v4/database"
"github.com/golang-migrate/migrate/v4/source/httpfs"
"github.com/lightningnetwork/lnd/sqldb/sqlc"
"github.com/pmezard/go-difflib/difflib"
)
var (
@@ -71,6 +74,11 @@ var (
// user if necessary.
},
}, migrationAdditions...)
// ErrMigrationMismatch is returned when a migrated record does not
// match the original record.
ErrMigrationMismatch = fmt.Errorf("migrated record does not match " +
"original record")
)
// MigrationConfig is a configuration struct that describes SQL migrations. Each
@@ -472,3 +480,25 @@ func ApplyMigrations(ctx context.Context, db *BaseDB,
return nil
}
// CompareRecords checks if the original and migrated objects are equal. If
// they are not, it returns an error with a unified diff of the two objects.
func CompareRecords(original, migrated any, identifier string) error {
if reflect.DeepEqual(original, migrated) {
return nil
}
diff := difflib.UnifiedDiff{
A: difflib.SplitLines(spew.Sdump(original)),
B: difflib.SplitLines(spew.Sdump(migrated)),
FromFile: "Expected",
FromDate: "",
ToFile: "Actual",
ToDate: "",
Context: 3,
}
diffText, _ := difflib.GetUnifiedDiffString(diff)
return fmt.Errorf("%w: %s.\n%v", ErrMigrationMismatch, identifier,
diffText)
}