mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-11-10 06:07:16 +01:00
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:
2
go.mod
2
go.mod
@@ -140,7 +140,7 @@ require (
|
|||||||
github.com/opencontainers/image-spec v1.0.2 // indirect
|
github.com/opencontainers/image-spec v1.0.2 // indirect
|
||||||
github.com/opencontainers/runc v1.1.12 // indirect
|
github.com/opencontainers/runc v1.1.12 // indirect
|
||||||
github.com/ory/dockertest/v3 v3.10.0 // indirect
|
github.com/ory/dockertest/v3 v3.10.0 // indirect
|
||||||
github.com/pmezard/go-difflib v1.0.0
|
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||||
github.com/prometheus/client_model v0.2.0 // indirect
|
github.com/prometheus/client_model v0.2.0 // indirect
|
||||||
github.com/prometheus/common v0.26.0 // indirect
|
github.com/prometheus/common v0.26.0 // indirect
|
||||||
github.com/prometheus/procfs v0.6.0 // indirect
|
github.com/prometheus/procfs v0.6.0 // indirect
|
||||||
|
|||||||
@@ -6,17 +6,14 @@ import (
|
|||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"reflect"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/davecgh/go-spew/spew"
|
|
||||||
"github.com/lightningnetwork/lnd/graph/db/models"
|
"github.com/lightningnetwork/lnd/graph/db/models"
|
||||||
"github.com/lightningnetwork/lnd/kvdb"
|
"github.com/lightningnetwork/lnd/kvdb"
|
||||||
"github.com/lightningnetwork/lnd/lntypes"
|
"github.com/lightningnetwork/lnd/lntypes"
|
||||||
"github.com/lightningnetwork/lnd/sqldb"
|
"github.com/lightningnetwork/lnd/sqldb"
|
||||||
"github.com/lightningnetwork/lnd/sqldb/sqlc"
|
"github.com/lightningnetwork/lnd/sqldb/sqlc"
|
||||||
"github.com/pmezard/go-difflib/difflib"
|
|
||||||
"golang.org/x/time/rate"
|
"golang.org/x/time/rate"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -53,11 +50,6 @@ var (
|
|||||||
//
|
//
|
||||||
// addIndexNo => invoiceKey
|
// addIndexNo => invoiceKey
|
||||||
addIndexBucket = []byte("invoice-add-index")
|
addIndexBucket = []byte("invoice-add-index")
|
||||||
|
|
||||||
// ErrMigrationMismatch is returned when the migrated invoice does not
|
|
||||||
// match the original invoice.
|
|
||||||
ErrMigrationMismatch = fmt.Errorf("migrated invoice does not match " +
|
|
||||||
"original invoice")
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// createInvoiceHashIndex generates a hash index that contains payment hashes
|
// createInvoiceHashIndex generates a hash index that contains payment hashes
|
||||||
@@ -548,24 +540,9 @@ func migrateInvoices(ctx context.Context, tx *sqlc.Queries,
|
|||||||
// Override the add index before checking for equality.
|
// Override the add index before checking for equality.
|
||||||
migratedInvoice.AddIndex = invoice.AddIndex
|
migratedInvoice.AddIndex = invoice.AddIndex
|
||||||
|
|
||||||
if !reflect.DeepEqual(invoice, *migratedInvoice) {
|
err = sqldb.CompareRecords(invoice, *migratedInvoice, "invoice")
|
||||||
diff := difflib.UnifiedDiff{
|
if err != nil {
|
||||||
A: difflib.SplitLines(
|
return err
|
||||||
spew.Sdump(invoice),
|
|
||||||
),
|
|
||||||
B: difflib.SplitLines(
|
|
||||||
spew.Sdump(migratedInvoice),
|
|
||||||
),
|
|
||||||
FromFile: "Expected",
|
|
||||||
FromDate: "",
|
|
||||||
ToFile: "Actual",
|
|
||||||
ToDate: "",
|
|
||||||
Context: 3,
|
|
||||||
}
|
|
||||||
diffText, _ := difflib.GetUnifiedDiffString(diff)
|
|
||||||
|
|
||||||
return fmt.Errorf("%w: %v.\n%v", ErrMigrationMismatch,
|
|
||||||
paymentHash, diffText)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,11 +2,13 @@ module github.com/lightningnetwork/lnd/sqldb
|
|||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/btcsuite/btclog/v2 v2.0.1-0.20250602222548-9967d19bb084
|
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/golang-migrate/migrate/v4 v4.17.0
|
||||||
github.com/jackc/pgconn v1.14.3
|
github.com/jackc/pgconn v1.14.3
|
||||||
github.com/jackc/pgerrcode v0.0.0-20240316143900-6e2875d9b438
|
github.com/jackc/pgerrcode v0.0.0-20240316143900-6e2875d9b438
|
||||||
github.com/jackc/pgx/v5 v5.3.1
|
github.com/jackc/pgx/v5 v5.3.1
|
||||||
github.com/ory/dockertest/v3 v3.10.0
|
github.com/ory/dockertest/v3 v3.10.0
|
||||||
|
github.com/pmezard/go-difflib v1.0.0
|
||||||
github.com/stretchr/testify v1.9.0
|
github.com/stretchr/testify v1.9.0
|
||||||
golang.org/x/exp v0.0.0-20240325151524-a685a6edb6d8
|
golang.org/x/exp v0.0.0-20240325151524-a685a6edb6d8
|
||||||
modernc.org/sqlite v1.29.10
|
modernc.org/sqlite v1.29.10
|
||||||
@@ -19,7 +21,6 @@ require (
|
|||||||
github.com/btcsuite/btclog v0.0.0-20241003133417-09c4e92e319c // indirect
|
github.com/btcsuite/btclog v0.0.0-20241003133417-09c4e92e319c // indirect
|
||||||
github.com/cenkalti/backoff/v4 v4.1.3 // indirect
|
github.com/cenkalti/backoff/v4 v4.1.3 // indirect
|
||||||
github.com/containerd/continuity v0.3.0 // 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/cli v20.10.17+incompatible // indirect
|
||||||
github.com/docker/docker v24.0.7+incompatible // indirect
|
github.com/docker/docker v24.0.7+incompatible // indirect
|
||||||
github.com/docker/go-connections v0.4.0 // 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/image-spec v1.0.2 // indirect
|
||||||
github.com/opencontainers/runc v1.1.5 // indirect
|
github.com/opencontainers/runc v1.1.5 // indirect
|
||||||
github.com/pkg/errors v0.9.1 // 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/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
|
||||||
github.com/rogpeppe/go-internal v1.12.0 // indirect
|
github.com/rogpeppe/go-internal v1.12.0 // indirect
|
||||||
github.com/sirupsen/logrus v1.9.2 // indirect
|
github.com/sirupsen/logrus v1.9.2 // indirect
|
||||||
|
|||||||
@@ -9,14 +9,17 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"io/fs"
|
"io/fs"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"reflect"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/btcsuite/btclog/v2"
|
"github.com/btcsuite/btclog/v2"
|
||||||
|
"github.com/davecgh/go-spew/spew"
|
||||||
"github.com/golang-migrate/migrate/v4"
|
"github.com/golang-migrate/migrate/v4"
|
||||||
"github.com/golang-migrate/migrate/v4/database"
|
"github.com/golang-migrate/migrate/v4/database"
|
||||||
"github.com/golang-migrate/migrate/v4/source/httpfs"
|
"github.com/golang-migrate/migrate/v4/source/httpfs"
|
||||||
"github.com/lightningnetwork/lnd/sqldb/sqlc"
|
"github.com/lightningnetwork/lnd/sqldb/sqlc"
|
||||||
|
"github.com/pmezard/go-difflib/difflib"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@@ -71,6 +74,11 @@ var (
|
|||||||
// user if necessary.
|
// user if necessary.
|
||||||
},
|
},
|
||||||
}, migrationAdditions...)
|
}, 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
|
// MigrationConfig is a configuration struct that describes SQL migrations. Each
|
||||||
@@ -472,3 +480,25 @@ func ApplyMigrations(ctx context.Context, db *BaseDB,
|
|||||||
|
|
||||||
return nil
|
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)
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user