From 902611d86a6d2ce2102df3b61f14c042fdc55f98 Mon Sep 17 00:00:00 2001 From: Elle Mouton Date: Fri, 11 Jul 2025 13:35:00 +0200 Subject: [PATCH] lnd: plug in graph SQL migration into dev build This commit plugs in the graph kvdb-to-sql migration for builds containing the `test_native_sql` tag. This will allow us to perform local tests and write itests for the migration without exposing it to the production release build. --- config_builder.go | 17 ++++++++++++++--- config_prod.go | 16 ++++++++++++++++ config_test_native_sql.go | 33 +++++++++++++++++++++++++++++++++ sqldb/migrations_dev.go | 9 +++++++++ 4 files changed, 72 insertions(+), 3 deletions(-) diff --git a/config_builder.go b/config_builder.go index e47b29b7d..3c8b122a8 100644 --- a/config_builder.go +++ b/config_builder.go @@ -1097,7 +1097,7 @@ func (d *DefaultDatabaseBuilder) BuildDatabase( // migration's version (7), it will be skipped permanently, // regardless of the flag. if !d.cfg.DB.SkipNativeSQLMigration { - migrationFn := func(tx *sqlc.Queries) error { + invoiceMig := func(tx *sqlc.Queries) error { err := invoices.MigrateInvoicesToSQL( ctx, dbs.ChanStateDB.Backend, dbs.ChanStateDB, tx, @@ -1119,11 +1119,22 @@ func (d *DefaultDatabaseBuilder) BuildDatabase( // Make sure we attach the custom migration function to // the correct migration version. for i := 0; i < len(migrations); i++ { - if migrations[i].Version != invoiceMigration { + version := migrations[i].Version + if version == invoiceMigration { + migrations[i].MigrationFn = invoiceMig + continue } - migrations[i].MigrationFn = migrationFn + migFn, ok := getSQLMigration( + ctx, version, dbs.ChanStateDB.Backend, + *d.cfg.ActiveNetParams.GenesisHash, + ) + if !ok { + continue + } + + migrations[i].MigrationFn = migFn } } diff --git a/config_prod.go b/config_prod.go index b480c2bb8..769d85468 100644 --- a/config_prod.go +++ b/config_prod.go @@ -3,9 +3,13 @@ package lnd import ( + "context" + + "github.com/btcsuite/btcd/chaincfg/chainhash" graphdb "github.com/lightningnetwork/lnd/graph/db" "github.com/lightningnetwork/lnd/kvdb" "github.com/lightningnetwork/lnd/sqldb" + "github.com/lightningnetwork/lnd/sqldb/sqlc" ) // getGraphStore returns a graphdb.V1Store backed by a graphdb.KVStore @@ -16,3 +20,15 @@ func (d *DefaultDatabaseBuilder) getGraphStore(_ *sqldb.BaseDB, return graphdb.NewKVStore(kvBackend, opts...) } + +// getSQLMigration returns a migration function for the given version. +// +// NOTE: this is a no-op for the production build since all migrations that are +// in production will also be in development builds, and so they are not +// defined behind a build tag. +func getSQLMigration(ctx context.Context, version int, + kvBackend kvdb.Backend, + chain chainhash.Hash) (func(tx *sqlc.Queries) error, bool) { + + return nil, false +} diff --git a/config_test_native_sql.go b/config_test_native_sql.go index 4a9469deb..dee5985a1 100644 --- a/config_test_native_sql.go +++ b/config_test_native_sql.go @@ -3,11 +3,15 @@ package lnd import ( + "context" "database/sql" + "fmt" + "github.com/btcsuite/btcd/chaincfg/chainhash" graphdb "github.com/lightningnetwork/lnd/graph/db" "github.com/lightningnetwork/lnd/kvdb" "github.com/lightningnetwork/lnd/sqldb" + "github.com/lightningnetwork/lnd/sqldb/sqlc" ) // getGraphStore returns a graphdb.V1Store backed by a graphdb.SQLStore @@ -29,3 +33,32 @@ func (d *DefaultDatabaseBuilder) getGraphStore(baseDB *sqldb.BaseDB, graphExecutor, opts..., ) } + +// graphSQLMigration is the version number for the graph migration +// that migrates the KV graph to the native SQL schema. +const graphSQLMigration = 9 + +// getSQLMigration returns a migration function for the given version. +func getSQLMigration(ctx context.Context, version int, + kvBackend kvdb.Backend, + chain chainhash.Hash) (func(tx *sqlc.Queries) error, bool) { + + switch version { + case graphSQLMigration: + return func(tx *sqlc.Queries) error { + err := graphdb.MigrateGraphToSQL( + ctx, kvBackend, tx, chain, + ) + if err != nil { + return fmt.Errorf("failed to migrate graph "+ + "to SQL: %w", err) + } + + return nil + }, true + } + + // No version was matched, so we return false to indicate that no + // migration is known for the given version. + return nil, false +} diff --git a/sqldb/migrations_dev.go b/sqldb/migrations_dev.go index b8721f927..38501b58b 100644 --- a/sqldb/migrations_dev.go +++ b/sqldb/migrations_dev.go @@ -8,4 +8,13 @@ var migrationAdditions = []MigrationConfig{ Version: 8, SchemaVersion: 7, }, + { + Name: "kv_graph_migration", + Version: 9, + SchemaVersion: 7, + // A migration function may be attached to this + // migration to migrate KV graph to the native SQL + // schema. This is optional and can be disabled by the + // user if necessary. + }, }