payments/db: split migration queries into dedicated SQLMigrationQueries interface
What changed, and why it matters
This commit is a straightforward internal code cleanup in LND's payment database code. It splits migration-only database query methods out of the general SQLQueries interface into a new SQLMigrationQueries interface. There is no security bug being fixed here; it is a refactoring change to make the codebase clearer and prevent accidental use of migration-only queries in normal operation.
No security action required. Treat as normal code-quality refactoring.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The change refactors the SQLQueries interface in payments/db/migration1/sql_store.go and payments/db/sql_store.go by extracting migration-specific methods (FetchPaymentsByIDsMig, FetchPaymentIDsMig, InsertPayment, etc.) into a new SQLMigrationQueries interface that embeds SQLQueries. Corresponding call sites in migration code, validation code, and tests are updated to accept SQLMigrationQueries instead of SQLQueries. This is purely an interface-segregation refactor with no functional or security behavior change.
Changed components
payments/db/migration1/sql_store.gopayments/db/sql_store.gopayments/db/migration1/sql_migration.gopayments/db/migration1/migration_validation.gopayments/db/migration1/migration_external_test.goInspect captured patch +30 / −18
diff --git a/payments/db/migration1/migration_external_test.go b/payments/db/migration1/migration_external_test.go
index a5ddc42..85f8289 100644
--- a/payments/db/migration1/migration_external_test.go
+++ b/payments/db/migration1/migration_external_test.go
@@ -67,8 +67,14 @@ func TestMigrationWithExternalDB(t *testing.T) {
// Run migration in a transaction.
err := sqlStore.db.ExecTx(
ctx, sqldb.WriteTxOpt(), func(tx SQLQueries) error {
+ migTx, ok := tx.(SQLMigrationQueries)
+ if !ok {
+ return fmt.Errorf("db does not " +
+ "implement SQLMigrationQueries")
+ }
+
return MigratePaymentsKVToSQL(
- ctx, kvBackend, tx, &SQLStoreConfig{
+ ctx, kvBackend, migTx, &SQLStoreConfig{
QueryCfg: sqlStore.cfg.QueryCfg,
},
)
diff --git a/payments/db/migration1/migration_validation.go b/payments/db/migration1/migration_validation.go
index e6f8bdd..b2192e9 100644
--- a/payments/db/migration1/migration_validation.go
+++ b/payments/db/migration1/migration_validation.go
@@ -29,7 +29,7 @@ type migratedPaymentRef struct {
// with their SQL counterparts. If a structural mismatch is detected, a full
// deep comparison is performed to produce a detailed diff for debugging.
func validateMigratedPaymentBatch(ctx context.Context,
- kvBackend kvdb.Backend, sqlDB SQLQueries,
+ kvBackend kvdb.Backend, sqlDB SQLMigrationQueries,
cfg *SQLStoreConfig, batch []migratedPaymentRef) error {
if len(batch) == 0 {
diff --git a/payments/db/migration1/sql_migration.go b/payments/db/migration1/sql_migration.go
index 626b25a..b208117 100644
--- a/payments/db/migration1/sql_migration.go
+++ b/payments/db/migration1/sql_migration.go
@@ -37,7 +37,7 @@ type MigrationStats struct {
// migrated data in batches. Callers are responsible for executing this within
// a single SQL transaction if atomicity is required.
func MigratePaymentsKVToSQL(ctx context.Context, kvBackend kvdb.Backend,
- sqlDB SQLQueries, cfg *SQLStoreConfig) error {
+ sqlDB SQLMigrationQueries, cfg *SQLStoreConfig) error {
if cfg == nil {
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.
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.
switch payment.Status {
@@ -602,7 +602,7 @@ func migrateRouteHop(ctx context.Context,
// migrateDuplicatePayments migrates duplicate payments into the dedicated
// payment_duplicates table.
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 {
duplicateCount := 0
@@ -660,7 +660,7 @@ func migrateDuplicatePayments(ctx context.Context, dupBucket kvdb.RBucket,
// given payment hash into payment_duplicates.
func migrateSingleDuplicatePayment(ctx context.Context, dupBucket kvdb.RBucket,
hash [32]byte, primaryPaymentID int64, duplicateSeq uint64,
- sqlDB SQLQueries) error {
+ sqlDB SQLMigrationQueries) error {
creationData := dupBucket.Get(duplicatePaymentCreationInfoKey)
if creationData == nil {
diff --git a/payments/db/migration1/sql_store.go b/payments/db/migration1/sql_store.go
index 0fe4aa1..2a2c977 100644
--- a/payments/db/migration1/sql_store.go
+++ b/payments/db/migration1/sql_store.go
@@ -88,13 +88,16 @@ type SQLQueries interface {
// DeleteFailedAttempts removes all failed HTLCs from the db for a
// given payment.
DeleteFailedAttempts(ctx context.Context, paymentID int64) error
+}
- /*
- Migration specific queries.
-
- These queries are used ONLY for the one-time migration from KV
- to SQL.
- */
+// SQLMigrationQueries extends SQLQueries with the additional queries needed
+// 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
+// the regular store from accidentally depending on them.
+//
+//nolint:ll
+type SQLMigrationQueries interface {
+ SQLQueries
// FetchPaymentsByIDsMig is a migration-only batch fetch that returns
// payment data along with HTLC attempt counts for structural
diff --git a/payments/db/sql_store.go b/payments/db/sql_store.go
index 2c8dde9..1c8ffb5 100644
--- a/payments/db/sql_store.go
+++ b/payments/db/sql_store.go
@@ -89,13 +89,16 @@ type SQLQueries interface {
// DeleteFailedAttempts removes all failed HTLCs from the db for a
// given payment.
DeleteFailedAttempts(ctx context.Context, paymentID int64) error
+}
- /*
- Migration specific queries.
-
- These queries are used ONLY for the one-time migration from KV
- to SQL.
- */
+// SQLMigrationQueries extends SQLQueries with the additional queries needed
+// 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
+// the regular store from accidentally depending on them.
+//
+//nolint:ll
+type SQLMigrationQueries interface {
+ SQLQueries
// FetchPaymentsByIDsMig is a migration-only batch fetch that returns
// payment data along with HTLC attempt counts for structural
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.