Skip to content

Commit 706b22a

Browse files
authored
Merge pull request #10338 from ellemouton/graphSQLMigFreeze
graph/db: freeze the SQL migration code
2 parents 6fcdbbb + 2c9b47f commit 706b22a

27 files changed

+9271
-143
lines changed

config_builder.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ import (
3636
"github.com/lightningnetwork/lnd/fn/v2"
3737
"github.com/lightningnetwork/lnd/funding"
3838
graphdb "github.com/lightningnetwork/lnd/graph/db"
39+
graphdbmig1 "github.com/lightningnetwork/lnd/graph/db/migration1"
40+
graphmig1sqlc "github.com/lightningnetwork/lnd/graph/db/migration1/sqlc"
3941
"github.com/lightningnetwork/lnd/htlcswitch"
4042
"github.com/lightningnetwork/lnd/invoices"
4143
"github.com/lightningnetwork/lnd/keychain"
@@ -1134,13 +1136,14 @@ func (d *DefaultDatabaseBuilder) BuildDatabase(
11341136
}
11351137

11361138
graphMig := func(tx *sqlc.Queries) error {
1137-
cfg := &graphdb.SQLStoreConfig{
1139+
cfg := &graphdbmig1.SQLStoreConfig{
11381140
//nolint:ll
11391141
ChainHash: *d.cfg.ActiveNetParams.GenesisHash,
11401142
QueryCfg: queryCfg,
11411143
}
1142-
err := graphdb.MigrateGraphToSQL(
1143-
ctx, cfg, dbs.ChanStateDB.Backend, tx,
1144+
err := graphdbmig1.MigrateGraphToSQL(
1145+
ctx, cfg, dbs.ChanStateDB.Backend,
1146+
graphmig1sqlc.New(tx.GetTx()),
11441147
)
11451148
if err != nil {
11461149
return fmt.Errorf("failed to migrate "+

docs/release-notes/release-notes-0.21.0.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,14 @@
5454

5555
## Database
5656

57+
* Freeze the [graph SQL migration
58+
code](https://github.com/lightningnetwork/lnd/pull/10338) to prevent the
59+
need for maintenance as the sqlc code evolves.
60+
5761
## Code Health
5862

5963
## Tooling and Documentation
6064

6165
# Contributors (Alphabetical Order)
66+
67+
* Elle Mouton

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,9 @@ require (
202202
sigs.k8s.io/yaml v1.2.0 // indirect
203203
)
204204

205+
// TODO(elle): remove once the gossip V2 sqldb changes have been made.
206+
replace github.com/lightningnetwork/lnd/sqldb => ./sqldb
207+
205208
// This replace is for https://github.com/advisories/GHSA-25xm-hr59-7c27
206209
replace github.com/ulikunitz/xz => github.com/ulikunitz/xz v0.5.11
207210

go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -382,8 +382,6 @@ github.com/lightningnetwork/lnd/kvdb v1.4.16 h1:9BZgWdDfjmHRHLS97cz39bVuBAqMc4/p
382382
github.com/lightningnetwork/lnd/kvdb v1.4.16/go.mod h1:HW+bvwkxNaopkz3oIgBV6NEnV4jCEZCACFUcNg4xSjM=
383383
github.com/lightningnetwork/lnd/queue v1.1.1 h1:99ovBlpM9B0FRCGYJo6RSFDlt8/vOkQQZznVb18iNMI=
384384
github.com/lightningnetwork/lnd/queue v1.1.1/go.mod h1:7A6nC1Qrm32FHuhx/mi1cieAiBZo5O6l8IBIoQxvkz4=
385-
github.com/lightningnetwork/lnd/sqldb v1.0.11 h1:X8J3OvdIhJVniQG78Qsp3niErl1zdGMTPvzgiLMWOOo=
386-
github.com/lightningnetwork/lnd/sqldb v1.0.11/go.mod h1:oOdZ7vjmAUmI9He+aFHTunnxKVefHZAfJttZdz16hSg=
387385
github.com/lightningnetwork/lnd/ticker v1.1.1 h1:J/b6N2hibFtC7JLV77ULQp++QLtCwT6ijJlbdiZFbSM=
388386
github.com/lightningnetwork/lnd/ticker v1.1.1/go.mod h1:waPTRAAcwtu7Ji3+3k+u/xH5GHovTsCoSVpho0KDvdA=
389387
github.com/lightningnetwork/lnd/tlv v1.3.2 h1:MO4FCk7F4k5xPMqVZF6Nb/kOpxlwPrUQpYjmyKny5s0=

graph/db/benchmark_test.go

Lines changed: 0 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,12 @@ import (
66
"errors"
77
"fmt"
88
"net"
9-
"os"
109
"path"
1110
"sync"
1211
"testing"
1312
"time"
1413

1514
"github.com/btcsuite/btcd/chaincfg"
16-
"github.com/btcsuite/btclog/v2"
1715
"github.com/lightningnetwork/lnd/batch"
1816
"github.com/lightningnetwork/lnd/fn/v2"
1917
"github.com/lightningnetwork/lnd/graph/db/models"
@@ -430,137 +428,6 @@ func TestPopulateDBs(t *testing.T) {
430428
}
431429
}
432430

433-
// TestPopulateViaMigration is a helper test that can be used to populate a
434-
// local native SQL graph from a kvdbgraph using the migration logic.
435-
//
436-
// NOTE: the testPostgres variable can be set to true to test with a
437-
// postgres backend instead of the kvdb-sqlite backend.
438-
//
439-
// NOTE: you will need to set the following build tags in order to run this
440-
// test:
441-
//
442-
// test_native_sql
443-
// kvdb_sqlite // If your source is kvdb-sqlite
444-
// kvdb_postgres // If your source is kvdb-postgres
445-
//
446-
// NOTE: this is a helper test and is not run by default.
447-
func TestPopulateViaMigration(t *testing.T) {
448-
// ======= STEP 0 ===========
449-
// Comment out this SKipf line.
450-
t.Skipf("Skipping local helper test")
451-
452-
const (
453-
srcBBolt = "kvdb-bbolt"
454-
srcSQLite = "kvdb-sqlite"
455-
srcPostgres = "kvdb-postgres"
456-
)
457-
458-
// ======= STEP 1 ===========
459-
// Set your chosen SOURCE type by uncommenting the corresponding line
460-
// below. By default, a kvdb-sqlite source is chosen.
461-
srcDB := srcSQLite
462-
// srcDB := srcBBolt
463-
// srcDB := srcPostgres
464-
465-
// ======= STEP 2 ============
466-
// Set this variable to the correct genesis hash of the source
467-
// DB. By default, mainnet is assumed.
468-
chain := *chaincfg.MainNetParams.GenesisHash
469-
470-
// ======= STEP 3 (ignore if source is postgres) ==============
471-
// If your source destination is bbolt or sqlite, then set this to the
472-
// path where your source database can be found.
473-
const sourceDBPath = "testdata"
474-
475-
// ======= STEP 4 (only if source is bbolt!) ============
476-
// If your source destination is bbolt, then set this to the name of
477-
// the bbolt file that contains the channel graph data.
478-
const sourceBBoltName = "channel.db"
479-
480-
// ======= STEP 5 (only if source is sqlite!) ============
481-
// If your source destination is sqlite, then set this to the name of
482-
// the sqlite file that contains the channel graph data.
483-
const sourceSQLiteName = "channel.sqlite"
484-
485-
// ======= STEP 6 (only if source is postgres!) ============
486-
// Set the DNS of your kvdb postgres instance below. This should be the
487-
// same as what you have set in the config of the LND node that
488-
// populated the instance (ie, whatever your --db.postgres.dsn is set
489-
// to).
490-
const kvdbPostgresDNS = "postgres://user@host/db_name"
491-
492-
// ======== STEP 7 ========================
493-
// Finally, pick your destination DB! You can choose either SQLite or
494-
// Postgres.
495-
testSQLite := true
496-
497-
// ======== STEP 8 (only if destination is sqlite) ========
498-
// Set the path where you want to create the destination SQLite
499-
// database. This should be a directory that exists and is writable.
500-
const destSQLitePath = "testdata"
501-
502-
// ======== STEP 9 (only if destination is sqlite) ========
503-
// Pick a name for your destination SQLite database file.
504-
// NOTE: if you run this test again, delete the previously created
505-
// file first.
506-
const destSQLiteFile = "lnd-graph-test.sqlite"
507-
508-
// ======== STEP 10 (only if destination is postgres) ========
509-
// NB: this has some additional steps:
510-
// 1. First, connect to your destination postgres instance: example:
511-
// $ psql -U ellemouton -d postgres
512-
// 2. Now, create the test database:
513-
// CREATE DATABASE graphtest;
514-
// NOTE: if you restart this test for postgres, it helps to first drop
515-
// the new database & recreate it.
516-
// NOTE: the database name that you use above must be whatever you will
517-
// use in the DNS you set below.
518-
const sqlPostgresDNS = "postgres://user@host/graphtest"
519-
520-
// ======= YOUR WORK IS DONE =============
521-
522-
// Connect to source database.
523-
var srcKVDB kvdb.Backend
524-
switch srcDB {
525-
case srcBBolt:
526-
srcKVDB = kvdbBBolt(t, sourceDBPath, sourceBBoltName)
527-
case srcSQLite:
528-
srcKVDB = kvdbSqlite(t, sourceDBPath, sourceSQLiteName)
529-
case srcPostgres:
530-
srcKVDB = kvdbPostgres(t, kvdbPostgresDNS)
531-
default:
532-
t.Fatalf("Unsupported source database backend: %s", srcDB)
533-
}
534-
535-
// Connect to destination database.
536-
cfg := sqldb.DefaultSQLiteConfig()
537-
dstSQL := sqlSQLite(t, destSQLitePath, destSQLiteFile)
538-
if !testSQLite {
539-
cfg = sqldb.DefaultPostgresConfig()
540-
dstSQL = sqlPostgres(t, sqlPostgresDNS)
541-
}
542-
543-
// Set up a logger so we can see the migration progress.
544-
logger := btclog.NewDefaultHandler(os.Stdout)
545-
UseLogger(btclog.NewSLogger(logger))
546-
log.SetLevel(btclog.LevelDebug)
547-
548-
// Use the graph migration to populate the SQL graph from the
549-
// kvdb graph.
550-
ctx := t.Context()
551-
err := dstSQL.ExecTx(
552-
ctx, sqldb.WriteTxOpt(), func(queries SQLQueries) error {
553-
return MigrateGraphToSQL(
554-
ctx, &SQLStoreConfig{
555-
QueryCfg: cfg,
556-
ChainHash: chain,
557-
}, srcKVDB, queries,
558-
)
559-
}, func() {},
560-
)
561-
require.NoError(t, err)
562-
}
563-
564431
// syncGraph synchronizes the source graph with the destination graph by
565432
// copying all nodes and channels from the source to the destination.
566433
func syncGraph(t *testing.T, src, dest *ChannelGraph) {

0 commit comments

Comments
 (0)