Skip to content

Commit 9d74ec4

Browse files
authored
Merge pull request #10163 from ellemouton/graphMigFlipSwitch
multi: switch on graph SQL migration
2 parents ab924be + 318f4aa commit 9d74ec4

File tree

7 files changed

+74
-97
lines changed

7 files changed

+74
-97
lines changed

config_builder.go

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ const (
7070
// invoiceMigration is the version of the migration that will be used to
7171
// migrate invoices from the kvdb to the sql database.
7272
invoiceMigration = 7
73+
74+
// graphMigration is the version number for the graph migration
75+
// that migrates the KV graph to the native SQL schema.
76+
graphMigration = 10
7377
)
7478

7579
// GrpcRegistrar is an interface that must be satisfied by an external subserver
@@ -1091,6 +1095,11 @@ func (d *DefaultDatabaseBuilder) BuildDatabase(
10911095
if d.cfg.DB.UseNativeSQL {
10921096
migrations := sqldb.GetMigrations()
10931097

1098+
queryCfg := &d.cfg.DB.Sqlite.QueryConfig
1099+
if d.cfg.DB.Backend == lncfg.PostgresBackend {
1100+
queryCfg = &d.cfg.DB.Postgres.QueryConfig
1101+
}
1102+
10941103
// If the user has not explicitly disabled the SQL invoice
10951104
// migration, attach the custom migration function to invoice
10961105
// migration (version 7). Even if this custom migration is
@@ -1115,17 +1124,42 @@ func (d *DefaultDatabaseBuilder) BuildDatabase(
11151124
d.logger.Debugf("Setting invoice bucket " +
11161125
"tombstone")
11171126

1118-
return dbs.ChanStateDB.SetInvoiceBucketTombstone() //nolint:ll
1127+
//nolint:ll
1128+
return dbs.ChanStateDB.SetInvoiceBucketTombstone()
1129+
}
1130+
1131+
graphMig := func(tx *sqlc.Queries) error {
1132+
cfg := &graphdb.SQLStoreConfig{
1133+
//nolint:ll
1134+
ChainHash: *d.cfg.ActiveNetParams.GenesisHash,
1135+
QueryCfg: queryCfg,
1136+
}
1137+
err := graphdb.MigrateGraphToSQL(
1138+
ctx, cfg, dbs.ChanStateDB.Backend, tx,
1139+
)
1140+
if err != nil {
1141+
return fmt.Errorf("failed to migrate "+
1142+
"graph to SQL: %w", err)
1143+
}
1144+
1145+
return nil
11191146
}
11201147

11211148
// Make sure we attach the custom migration function to
11221149
// the correct migration version.
11231150
for i := 0; i < len(migrations); i++ {
11241151
version := migrations[i].Version
1125-
if version == invoiceMigration {
1152+
switch version {
1153+
case invoiceMigration:
11261154
migrations[i].MigrationFn = invoiceMig
11271155

11281156
continue
1157+
case graphMigration:
1158+
migrations[i].MigrationFn = graphMig
1159+
1160+
continue
1161+
1162+
default:
11291163
}
11301164

11311165
migFn, ok := d.getSQLMigration(
@@ -1167,8 +1201,18 @@ func (d *DefaultDatabaseBuilder) BuildDatabase(
11671201

11681202
dbs.InvoiceDB = sqlInvoiceDB
11691203

1170-
graphStore, err = d.getGraphStore(
1171-
baseDB, databaseBackends.GraphDB, graphDBOptions...,
1204+
graphExecutor := sqldb.NewTransactionExecutor(
1205+
baseDB, func(tx *sql.Tx) graphdb.SQLQueries {
1206+
return baseDB.WithTx(tx)
1207+
},
1208+
)
1209+
1210+
graphStore, err = graphdb.NewSQLStore(
1211+
&graphdb.SQLStoreConfig{
1212+
ChainHash: *d.cfg.ActiveNetParams.GenesisHash,
1213+
QueryCfg: queryCfg,
1214+
},
1215+
graphExecutor, graphDBOptions...,
11721216
)
11731217
if err != nil {
11741218
err = fmt.Errorf("unable to get graph store: %w", err)

config_prod.go

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,14 @@ package lnd
55
import (
66
"context"
77

8-
graphdb "github.com/lightningnetwork/lnd/graph/db"
98
"github.com/lightningnetwork/lnd/kvdb"
10-
"github.com/lightningnetwork/lnd/sqldb"
119
"github.com/lightningnetwork/lnd/sqldb/sqlc"
1210
)
1311

1412
// RunTestSQLMigration is a build tag that indicates whether the test_native_sql
1513
// build tag is set.
1614
var RunTestSQLMigration = false
1715

18-
// getGraphStore returns a graphdb.V1Store backed by a graphdb.KVStore
19-
// implementation.
20-
func (d *DefaultDatabaseBuilder) getGraphStore(_ *sqldb.BaseDB,
21-
kvBackend kvdb.Backend,
22-
opts ...graphdb.StoreOptionModifier) (graphdb.V1Store, error) {
23-
24-
return graphdb.NewKVStore(kvBackend, opts...)
25-
}
26-
2716
// getSQLMigration returns a migration function for the given version.
2817
//
2918
// NOTE: this is a no-op for the production build since all migrations that are

config_test_native_sql.go

Lines changed: 6 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -4,79 +4,24 @@ package lnd
44

55
import (
66
"context"
7-
"database/sql"
8-
"fmt"
97

10-
graphdb "github.com/lightningnetwork/lnd/graph/db"
118
"github.com/lightningnetwork/lnd/kvdb"
12-
"github.com/lightningnetwork/lnd/lncfg"
13-
"github.com/lightningnetwork/lnd/sqldb"
149
"github.com/lightningnetwork/lnd/sqldb/sqlc"
1510
)
1611

1712
// RunTestSQLMigration is a build tag that indicates whether the test_native_sql
1813
// build tag is set.
1914
var RunTestSQLMigration = true
2015

21-
// getGraphStore returns a graphdb.V1Store backed by a graphdb.SQLStore
22-
// implementation.
23-
func (d *DefaultDatabaseBuilder) getGraphStore(baseDB *sqldb.BaseDB,
24-
_ kvdb.Backend, opts ...graphdb.StoreOptionModifier) (graphdb.V1Store,
25-
error) {
26-
27-
graphExecutor := sqldb.NewTransactionExecutor(
28-
baseDB, func(tx *sql.Tx) graphdb.SQLQueries {
29-
return baseDB.WithTx(tx)
30-
},
31-
)
32-
33-
queryConfig := d.cfg.DB.Sqlite.QueryConfig
34-
if d.cfg.DB.Backend == lncfg.PostgresBackend {
35-
queryConfig = d.cfg.DB.Postgres.QueryConfig
36-
}
37-
38-
return graphdb.NewSQLStore(
39-
&graphdb.SQLStoreConfig{
40-
ChainHash: *d.cfg.ActiveNetParams.GenesisHash,
41-
QueryCfg: &queryConfig,
42-
},
43-
graphExecutor, opts...,
44-
)
45-
}
46-
47-
// graphSQLMigration is the version number for the graph migration
48-
// that migrates the KV graph to the native SQL schema.
49-
const graphSQLMigration = 10
50-
5116
// getSQLMigration returns a migration function for the given version.
52-
func (d *DefaultDatabaseBuilder) getSQLMigration(ctx context.Context,
53-
version int, kvBackend kvdb.Backend) (func(tx *sqlc.Queries) error,
17+
func (d *DefaultDatabaseBuilder) getSQLMigration(_ context.Context,
18+
version int, _ kvdb.Backend) (func(tx *sqlc.Queries) error,
5419
bool) {
5520

56-
cfg := &graphdb.SQLStoreConfig{
57-
ChainHash: *d.cfg.ActiveNetParams.GenesisHash,
58-
QueryCfg: &d.cfg.DB.Sqlite.QueryConfig,
59-
}
60-
if d.cfg.DB.Backend == lncfg.PostgresBackend {
61-
cfg.QueryCfg = &d.cfg.DB.Postgres.QueryConfig
62-
}
63-
6421
switch version {
65-
case graphSQLMigration:
66-
return func(tx *sqlc.Queries) error {
67-
err := graphdb.MigrateGraphToSQL(
68-
ctx, cfg, kvBackend, tx,
69-
)
70-
if err != nil {
71-
return fmt.Errorf("failed to migrate graph "+
72-
"to SQL: %w", err)
73-
}
74-
75-
return nil
76-
}, true
22+
default:
23+
// No version was matched, so we return false to indicate that
24+
// no migration is known for the given version.
25+
return nil, false
7726
}
78-
79-
// No version was matched, so we return false to indicate that no
80-
// migration is known for the given version.
81-
return nil, false
8227
}

docs/release-notes/release-notes-0.20.0.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,11 @@ reader of a payment request.
248248
* Add missing [sql index](https://github.com/lightningnetwork/lnd/pull/10155)
249249
for settled invoices to increase query speed.
250250

251+
* [Migrate the KV graph store to native
252+
SQL](https://github.com/lightningnetwork/lnd/pull/10163). For this migration
253+
to take place, the db backend must already be either `postgres` or `sqlite`
254+
and the `--use-native-sql` flag must be set.
255+
251256
## Code Health
252257

253258
## Tooling and Documentation

itest/lnd_graph_migration_test.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"database/sql"
66
"net"
77

8-
"github.com/lightningnetwork/lnd"
98
graphdb "github.com/lightningnetwork/lnd/graph/db"
109
"github.com/lightningnetwork/lnd/lntest"
1110
"github.com/lightningnetwork/lnd/lntest/node"
@@ -17,10 +16,6 @@ import (
1716
// testGraphMigration tests that the graph migration from the old KV store to
1817
// the new native SQL store works as expected.
1918
func testGraphMigration(ht *lntest.HarnessTest) {
20-
if !lnd.RunTestSQLMigration {
21-
ht.Skip("not running with test_native_sql tag")
22-
}
23-
2419
ctx := ht.Context()
2520
alice := ht.NewNodeWithCoins("Alice", nil)
2621

sqldb/migrations.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,20 @@ var (
7878
Version: 8,
7979
SchemaVersion: 7,
8080
},
81+
{
82+
Name: "000008_graph",
83+
Version: 9,
84+
SchemaVersion: 8,
85+
},
86+
{
87+
Name: "kv_graph_migration",
88+
Version: 10,
89+
SchemaVersion: 8,
90+
// A migration function may be attached to this
91+
// migration to migrate KV graph to the native SQL
92+
// schema. This is optional and can be disabled by the
93+
// user if necessary.
94+
},
8195
}, migrationAdditions...)
8296

8397
// ErrMigrationMismatch is returned when a migrated record does not

sqldb/migrations_dev.go

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,4 @@
22

33
package sqldb
44

5-
var migrationAdditions = []MigrationConfig{
6-
{
7-
Name: "000008_graph",
8-
Version: 9,
9-
SchemaVersion: 8,
10-
},
11-
{
12-
Name: "kv_graph_migration",
13-
Version: 10,
14-
SchemaVersion: 8,
15-
// A migration function may be attached to this
16-
// migration to migrate KV graph to the native SQL
17-
// schema. This is optional and can be disabled by the
18-
// user if necessary.
19-
},
20-
}
5+
var migrationAdditions []MigrationConfig

0 commit comments

Comments
 (0)