payments/db: split migration queries into dedicated SQLMigrationQueries interface

This commit is contained in:
ziggie committed 2026-02-25 18:52:34 +01:00
1 parent a2c36d1667
commit cf3610abb9
5 files changed
+30 -18

No files matched your search

@@ -67,8 +67,14 @@ func TestMigrationWithExternalDB(t *testing.T) {
// Run migration in a transaction. // Run migration in a transaction.
err := sqlStore.db.ExecTx( err := sqlStore.db.ExecTx(
ctx, sqldb.WriteTxOpt(), func(tx SQLQueries) error { ctx, sqldb.WriteTxOpt(), func(tx SQLQueries) error {
migTx, ok := tx.(SQLMigrationQueries)
if !ok {
return fmt.Errorf("db does not " +
"implement SQLMigrationQueries")
}
return MigratePaymentsKVToSQL( return MigratePaymentsKVToSQL(
ctx, kvBackend, tx, &SQLStoreConfig{ ctx, kvBackend, migTx, &SQLStoreConfig{
QueryCfg: sqlStore.cfg.QueryCfg, QueryCfg: sqlStore.cfg.QueryCfg,
}, },
) )
@@ -29,7 +29,7 @@ type migratedPaymentRef struct {
// with their SQL counterparts. If a structural mismatch is detected, a full // with their SQL counterparts. If a structural mismatch is detected, a full
// deep comparison is performed to produce a detailed diff for debugging. // deep comparison is performed to produce a detailed diff for debugging.
func validateMigratedPaymentBatch(ctx context.Context, func validateMigratedPaymentBatch(ctx context.Context,
kvBackend kvdb.Backend, sqlDB SQLQueries, kvBackend kvdb.Backend, sqlDB SQLMigrationQueries,
cfg *SQLStoreConfig, batch []migratedPaymentRef) error { cfg *SQLStoreConfig, batch []migratedPaymentRef) error {
if len(batch) == 0 { if len(batch) == 0 {
+4 -4
View File
@@ -37,7 +37,7 @@ type MigrationStats struct {
// migrated data in batches. Callers are responsible for executing this within // migrated data in batches. Callers are responsible for executing this within
// a single SQL transaction if atomicity is required. // a single SQL transaction if atomicity is required.
func MigratePaymentsKVToSQL(ctx context.Context, kvBackend kvdb.Backend, func MigratePaymentsKVToSQL(ctx context.Context, kvBackend kvdb.Backend,
sqlDB SQLQueries, cfg *SQLStoreConfig) error { sqlDB SQLMigrationQueries, cfg *SQLStoreConfig) error {
if cfg == nil { if cfg == nil {
return fmt.Errorf("missing SQL store config for migration") return fmt.Errorf("missing SQL store config for migration")
@@ -272,7 +272,7 @@ func normalizeTimeForSQL(t time.Time) time.Time {
// migratePayment migrates a single payment from KV to SQL. // migratePayment migrates a single payment from KV to SQL.
func migratePayment(ctx context.Context, payment *MPPayment, hash lntypes.Hash, func migratePayment(ctx context.Context, payment *MPPayment, hash lntypes.Hash,
sqlDB SQLQueries, stats *MigrationStats) (int64, error) { sqlDB SQLMigrationQueries, stats *MigrationStats) (int64, error) {
// Update migration stats based on payment status. // Update migration stats based on payment status.
switch payment.Status { switch payment.Status {
@@ -602,7 +602,7 @@ func migrateRouteHop(ctx context.Context,
// migrateDuplicatePayments migrates duplicate payments into the dedicated // migrateDuplicatePayments migrates duplicate payments into the dedicated
// payment_duplicates table. // payment_duplicates table.
func migrateDuplicatePayments(ctx context.Context, dupBucket kvdb.RBucket, func migrateDuplicatePayments(ctx context.Context, dupBucket kvdb.RBucket,
hash [32]byte, primaryPaymentID int64, sqlDB SQLQueries, hash [32]byte, primaryPaymentID int64, sqlDB SQLMigrationQueries,
stats *MigrationStats) error { stats *MigrationStats) error {
duplicateCount := 0 duplicateCount := 0
@@ -660,7 +660,7 @@ func migrateDuplicatePayments(ctx context.Context, dupBucket kvdb.RBucket,
// given payment hash into payment_duplicates. // given payment hash into payment_duplicates.
func migrateSingleDuplicatePayment(ctx context.Context, dupBucket kvdb.RBucket, func migrateSingleDuplicatePayment(ctx context.Context, dupBucket kvdb.RBucket,
hash [32]byte, primaryPaymentID int64, duplicateSeq uint64, hash [32]byte, primaryPaymentID int64, duplicateSeq uint64,
sqlDB SQLQueries) error { sqlDB SQLMigrationQueries) error {
creationData := dupBucket.Get(duplicatePaymentCreationInfoKey) creationData := dupBucket.Get(duplicatePaymentCreationInfoKey)
if creationData == nil { if creationData == nil {
+9 -6
View File
@@ -88,13 +88,16 @@ type SQLQueries interface {
// DeleteFailedAttempts removes all failed HTLCs from the db for a // DeleteFailedAttempts removes all failed HTLCs from the db for a
// given payment. // given payment.
DeleteFailedAttempts(ctx context.Context, paymentID int64) error DeleteFailedAttempts(ctx context.Context, paymentID int64) error
}
/* // SQLMigrationQueries extends SQLQueries with the additional queries needed
Migration specific queries. // for the one-time migration from KV to SQL. Keeping them in a separate
// interface makes it clear which code paths are migration-only and prevents
These queries are used ONLY for the one-time migration from KV // the regular store from accidentally depending on them.
to SQL. //
*/ //nolint:ll
type SQLMigrationQueries interface {
SQLQueries
// FetchPaymentsByIDsMig is a migration-only batch fetch that returns // FetchPaymentsByIDsMig is a migration-only batch fetch that returns
// payment data along with HTLC attempt counts for structural // payment data along with HTLC attempt counts for structural
+9 -6
View File
@@ -89,13 +89,16 @@ type SQLQueries interface {
// DeleteFailedAttempts removes all failed HTLCs from the db for a // DeleteFailedAttempts removes all failed HTLCs from the db for a
// given payment. // given payment.
DeleteFailedAttempts(ctx context.Context, paymentID int64) error DeleteFailedAttempts(ctx context.Context, paymentID int64) error
}
/* // SQLMigrationQueries extends SQLQueries with the additional queries needed
Migration specific queries. // for the one-time migration from KV to SQL. Keeping them in a separate
// interface makes it clear which code paths are migration-only and prevents
These queries are used ONLY for the one-time migration from KV // the regular store from accidentally depending on them.
to SQL. //
*/ //nolint:ll
type SQLMigrationQueries interface {
SQLQueries
// FetchPaymentsByIDsMig is a migration-only batch fetch that returns // FetchPaymentsByIDsMig is a migration-only batch fetch that returns
// payment data along with HTLC attempt counts for structural // payment data along with HTLC attempt counts for structural