multi: switch on graph SQL migration

In this commit, the graph SQL migration is added to the production
build.
This commit is contained in:
Elle Mouton
2025-08-15 13:41:36 +02:00
parent 3751d7a6b6
commit 9ce14371e1
6 changed files with 69 additions and 97 deletions

View File

@@ -70,6 +70,10 @@ const (
// invoiceMigration is the version of the migration that will be used to // invoiceMigration is the version of the migration that will be used to
// migrate invoices from the kvdb to the sql database. // migrate invoices from the kvdb to the sql database.
invoiceMigration = 7 invoiceMigration = 7
// graphMigration is the version number for the graph migration
// that migrates the KV graph to the native SQL schema.
graphMigration = 10
) )
// GrpcRegistrar is an interface that must be satisfied by an external subserver // GrpcRegistrar is an interface that must be satisfied by an external subserver
@@ -1091,6 +1095,11 @@ func (d *DefaultDatabaseBuilder) BuildDatabase(
if d.cfg.DB.UseNativeSQL { if d.cfg.DB.UseNativeSQL {
migrations := sqldb.GetMigrations() migrations := sqldb.GetMigrations()
queryCfg := &d.cfg.DB.Sqlite.QueryConfig
if d.cfg.DB.Backend == lncfg.PostgresBackend {
queryCfg = &d.cfg.DB.Postgres.QueryConfig
}
// If the user has not explicitly disabled the SQL invoice // If the user has not explicitly disabled the SQL invoice
// migration, attach the custom migration function to invoice // migration, attach the custom migration function to invoice
// migration (version 7). Even if this custom migration is // migration (version 7). Even if this custom migration is
@@ -1115,17 +1124,42 @@ func (d *DefaultDatabaseBuilder) BuildDatabase(
d.logger.Debugf("Setting invoice bucket " + d.logger.Debugf("Setting invoice bucket " +
"tombstone") "tombstone")
return dbs.ChanStateDB.SetInvoiceBucketTombstone() //nolint:ll //nolint:ll
return dbs.ChanStateDB.SetInvoiceBucketTombstone()
}
graphMig := func(tx *sqlc.Queries) error {
cfg := &graphdb.SQLStoreConfig{
//nolint:ll
ChainHash: *d.cfg.ActiveNetParams.GenesisHash,
QueryCfg: queryCfg,
}
err := graphdb.MigrateGraphToSQL(
ctx, cfg, dbs.ChanStateDB.Backend, tx,
)
if err != nil {
return fmt.Errorf("failed to migrate "+
"graph to SQL: %w", err)
}
return nil
} }
// Make sure we attach the custom migration function to // Make sure we attach the custom migration function to
// the correct migration version. // the correct migration version.
for i := 0; i < len(migrations); i++ { for i := 0; i < len(migrations); i++ {
version := migrations[i].Version version := migrations[i].Version
if version == invoiceMigration { switch version {
case invoiceMigration:
migrations[i].MigrationFn = invoiceMig migrations[i].MigrationFn = invoiceMig
continue continue
case graphMigration:
migrations[i].MigrationFn = graphMig
continue
default:
} }
migFn, ok := d.getSQLMigration( migFn, ok := d.getSQLMigration(
@@ -1167,8 +1201,18 @@ func (d *DefaultDatabaseBuilder) BuildDatabase(
dbs.InvoiceDB = sqlInvoiceDB dbs.InvoiceDB = sqlInvoiceDB
graphStore, err = d.getGraphStore( graphExecutor := sqldb.NewTransactionExecutor(
baseDB, databaseBackends.GraphDB, graphDBOptions..., baseDB, func(tx *sql.Tx) graphdb.SQLQueries {
return baseDB.WithTx(tx)
},
)
graphStore, err = graphdb.NewSQLStore(
&graphdb.SQLStoreConfig{
ChainHash: *d.cfg.ActiveNetParams.GenesisHash,
QueryCfg: queryCfg,
},
graphExecutor, graphDBOptions...,
) )
if err != nil { if err != nil {
err = fmt.Errorf("unable to get graph store: %w", err) err = fmt.Errorf("unable to get graph store: %w", err)

View File

@@ -5,9 +5,7 @@ package lnd
import ( import (
"context" "context"
graphdb "github.com/lightningnetwork/lnd/graph/db"
"github.com/lightningnetwork/lnd/kvdb" "github.com/lightningnetwork/lnd/kvdb"
"github.com/lightningnetwork/lnd/sqldb"
"github.com/lightningnetwork/lnd/sqldb/sqlc" "github.com/lightningnetwork/lnd/sqldb/sqlc"
) )
@@ -15,15 +13,6 @@ import (
// build tag is set. // build tag is set.
var RunTestSQLMigration = false var RunTestSQLMigration = false
// getGraphStore returns a graphdb.V1Store backed by a graphdb.KVStore
// implementation.
func (d *DefaultDatabaseBuilder) getGraphStore(_ *sqldb.BaseDB,
kvBackend kvdb.Backend,
opts ...graphdb.StoreOptionModifier) (graphdb.V1Store, error) {
return graphdb.NewKVStore(kvBackend, opts...)
}
// getSQLMigration returns a migration function for the given version. // getSQLMigration returns a migration function for the given version.
// //
// NOTE: this is a no-op for the production build since all migrations that are // NOTE: this is a no-op for the production build since all migrations that are

View File

@@ -4,13 +4,8 @@ package lnd
import ( import (
"context" "context"
"database/sql"
"fmt"
graphdb "github.com/lightningnetwork/lnd/graph/db"
"github.com/lightningnetwork/lnd/kvdb" "github.com/lightningnetwork/lnd/kvdb"
"github.com/lightningnetwork/lnd/lncfg"
"github.com/lightningnetwork/lnd/sqldb"
"github.com/lightningnetwork/lnd/sqldb/sqlc" "github.com/lightningnetwork/lnd/sqldb/sqlc"
) )
@@ -18,65 +13,15 @@ import (
// build tag is set. // build tag is set.
var RunTestSQLMigration = true var RunTestSQLMigration = true
// getGraphStore returns a graphdb.V1Store backed by a graphdb.SQLStore
// implementation.
func (d *DefaultDatabaseBuilder) getGraphStore(baseDB *sqldb.BaseDB,
_ kvdb.Backend, opts ...graphdb.StoreOptionModifier) (graphdb.V1Store,
error) {
graphExecutor := sqldb.NewTransactionExecutor(
baseDB, func(tx *sql.Tx) graphdb.SQLQueries {
return baseDB.WithTx(tx)
},
)
queryConfig := d.cfg.DB.Sqlite.QueryConfig
if d.cfg.DB.Backend == lncfg.PostgresBackend {
queryConfig = d.cfg.DB.Postgres.QueryConfig
}
return graphdb.NewSQLStore(
&graphdb.SQLStoreConfig{
ChainHash: *d.cfg.ActiveNetParams.GenesisHash,
QueryCfg: &queryConfig,
},
graphExecutor, opts...,
)
}
// graphSQLMigration is the version number for the graph migration
// that migrates the KV graph to the native SQL schema.
const graphSQLMigration = 10
// getSQLMigration returns a migration function for the given version. // getSQLMigration returns a migration function for the given version.
func (d *DefaultDatabaseBuilder) getSQLMigration(ctx context.Context, func (d *DefaultDatabaseBuilder) getSQLMigration(_ context.Context,
version int, kvBackend kvdb.Backend) (func(tx *sqlc.Queries) error, version int, _ kvdb.Backend) (func(tx *sqlc.Queries) error,
bool) { bool) {
cfg := &graphdb.SQLStoreConfig{
ChainHash: *d.cfg.ActiveNetParams.GenesisHash,
QueryCfg: &d.cfg.DB.Sqlite.QueryConfig,
}
if d.cfg.DB.Backend == lncfg.PostgresBackend {
cfg.QueryCfg = &d.cfg.DB.Postgres.QueryConfig
}
switch version { switch version {
case graphSQLMigration: default:
return func(tx *sqlc.Queries) error { // No version was matched, so we return false to indicate that
err := graphdb.MigrateGraphToSQL( // no migration is known for the given version.
ctx, cfg, kvBackend, tx,
)
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 return nil, false
}
} }

View File

@@ -5,7 +5,6 @@ import (
"database/sql" "database/sql"
"net" "net"
"github.com/lightningnetwork/lnd"
graphdb "github.com/lightningnetwork/lnd/graph/db" graphdb "github.com/lightningnetwork/lnd/graph/db"
"github.com/lightningnetwork/lnd/lntest" "github.com/lightningnetwork/lnd/lntest"
"github.com/lightningnetwork/lnd/lntest/node" "github.com/lightningnetwork/lnd/lntest/node"
@@ -17,10 +16,6 @@ import (
// testGraphMigration tests that the graph migration from the old KV store to // testGraphMigration tests that the graph migration from the old KV store to
// the new native SQL store works as expected. // the new native SQL store works as expected.
func testGraphMigration(ht *lntest.HarnessTest) { func testGraphMigration(ht *lntest.HarnessTest) {
if !lnd.RunTestSQLMigration {
ht.Skip("not running with test_native_sql tag")
}
ctx := ht.Context() ctx := ht.Context()
alice := ht.NewNodeWithCoins("Alice", nil) alice := ht.NewNodeWithCoins("Alice", nil)

View File

@@ -78,6 +78,20 @@ var (
Version: 8, Version: 8,
SchemaVersion: 7, SchemaVersion: 7,
}, },
{
Name: "000008_graph",
Version: 9,
SchemaVersion: 8,
},
{
Name: "kv_graph_migration",
Version: 10,
SchemaVersion: 8,
// 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.
},
}, migrationAdditions...) }, migrationAdditions...)
// ErrMigrationMismatch is returned when a migrated record does not // ErrMigrationMismatch is returned when a migrated record does not

View File

@@ -2,19 +2,4 @@
package sqldb package sqldb
var migrationAdditions = []MigrationConfig{ var migrationAdditions []MigrationConfig
{
Name: "000008_graph",
Version: 9,
SchemaVersion: 8,
},
{
Name: "kv_graph_migration",
Version: 10,
SchemaVersion: 8,
// 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.
},
}