Skip to content

Commit 25e6526

Browse files
committed
plug into itests behind feature flag
1 parent 949cc13 commit 25e6526

File tree

6 files changed

+132
-18
lines changed

6 files changed

+132
-18
lines changed

.github/workflows/main.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,10 +368,14 @@ jobs:
368368
args: backend=bitcoind dbbackend=sqlite
369369
- name: bitcoind-sqlite-nativesql
370370
args: backend=bitcoind dbbackend=sqlite nativesql=true
371+
- name: bitcoind-sqlite=nativesql-experiment
372+
args: backend=bitcoind dbbackend=sqlite nativesql=true tags=test_native_sql
371373
- name: bitcoind-postgres
372374
args: backend=bitcoind dbbackend=postgres
373375
- name: bitcoind-postgres-nativesql
374376
args: backend=bitcoind dbbackend=postgres nativesql=true
377+
- name: bitcoind-postgres-nativesql-experiment
378+
args: backend=bitcoind dbbackend=postgres nativesql=true tags=test_native_sql
375379
steps:
376380
- name: git checkout
377381
uses: actions/checkout@v4

config_builder.go

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1046,23 +1046,13 @@ func (d *DefaultDatabaseBuilder) BuildDatabase(
10461046
)
10471047
}
10481048

1049-
graphStore, err := graphdb.NewKVStore(
1049+
kvGraphStore, err := graphdb.NewKVStore(
10501050
databaseBackends.GraphDB, graphDBOptions...,
10511051
)
10521052
if err != nil {
10531053
return nil, nil, err
10541054
}
10551055

1056-
dbs.GraphDB, err = graphdb.NewChannelGraph(graphStore, chanGraphOpts...)
1057-
if err != nil {
1058-
cleanUp()
1059-
1060-
err = fmt.Errorf("unable to open graph DB: %w", err)
1061-
d.logger.Error(err)
1062-
1063-
return nil, nil, err
1064-
}
1065-
10661056
dbOptions := []channeldb.OptionModifier{
10671057
channeldb.OptionDryRunMigration(cfg.DryRunMigration),
10681058
channeldb.OptionKeepFailedPaymentAttempts(
@@ -1096,6 +1086,8 @@ func (d *DefaultDatabaseBuilder) BuildDatabase(
10961086
return nil, nil, err
10971087
}
10981088

1089+
var graphStore graphdb.V1Store
1090+
10991091
// Instantiate a native SQL store if the flag is set.
11001092
if d.cfg.DB.UseNativeSQL {
11011093
migrations := sqldb.GetMigrations()
@@ -1108,7 +1100,7 @@ func (d *DefaultDatabaseBuilder) BuildDatabase(
11081100
// migration's version (7), it will be skipped permanently,
11091101
// regardless of the flag.
11101102
if !d.cfg.DB.SkipNativeSQLMigration {
1111-
migrationFn := func(tx *sqlc.Queries) error {
1103+
invoiceMigFn := func(tx *sqlc.Queries) error {
11121104
err := invoices.MigrateInvoicesToSQL(
11131105
ctx, dbs.ChanStateDB.Backend,
11141106
dbs.ChanStateDB, tx,
@@ -1130,11 +1122,21 @@ func (d *DefaultDatabaseBuilder) BuildDatabase(
11301122
// Make sure we attach the custom migration function to
11311123
// the correct migration version.
11321124
for i := 0; i < len(migrations); i++ {
1133-
if migrations[i].Version != invoiceMigration {
1125+
version := migrations[i].Version
1126+
if version == invoiceMigration {
1127+
migrations[i].MigrationFn = invoiceMigFn
1128+
1129+
continue
1130+
}
1131+
1132+
migFn, ok := getSQLMigration(
1133+
ctx, version, kvGraphStore,
1134+
)
1135+
if !ok {
11341136
continue
11351137
}
11361138

1137-
migrations[i].MigrationFn = migrationFn
1139+
migrations[i].MigrationFn = migFn
11381140
}
11391141
}
11401142

@@ -1154,17 +1156,27 @@ func (d *DefaultDatabaseBuilder) BuildDatabase(
11541156
// the base DB and transaction executor for the native SQL
11551157
// invoice store.
11561158
baseDB := dbs.NativeSQLStore.GetBaseDB()
1157-
executor := sqldb.NewTransactionExecutor(
1159+
invoiceExecutor := sqldb.NewTransactionExecutor(
11581160
baseDB, func(tx *sql.Tx) invoices.SQLInvoiceQueries {
11591161
return baseDB.WithTx(tx)
11601162
},
11611163
)
11621164

11631165
sqlInvoiceDB := invoices.NewSQLStore(
1164-
executor, clock.NewDefaultClock(),
1166+
invoiceExecutor, clock.NewDefaultClock(),
11651167
)
11661168

11671169
dbs.InvoiceDB = sqlInvoiceDB
1170+
1171+
graphStore, err = d.getGraphStore(
1172+
baseDB, kvGraphStore, graphDBOptions...,
1173+
)
1174+
if err != nil {
1175+
err = fmt.Errorf("unable to get graph store: %w", err)
1176+
d.logger.Error(err)
1177+
1178+
return nil, nil, err
1179+
}
11681180
} else {
11691181
// Check if the invoice bucket tombstone is set. If it is, we
11701182
// need to return and ask the user switch back to using the
@@ -1186,6 +1198,18 @@ func (d *DefaultDatabaseBuilder) BuildDatabase(
11861198
}
11871199

11881200
dbs.InvoiceDB = dbs.ChanStateDB
1201+
1202+
graphStore = kvGraphStore
1203+
}
1204+
1205+
dbs.GraphDB, err = graphdb.NewChannelGraph(graphStore, chanGraphOpts...)
1206+
if err != nil {
1207+
cleanUp()
1208+
1209+
err = fmt.Errorf("unable to open channel graph: %w", err)
1210+
d.logger.Error(err)
1211+
1212+
return nil, nil, err
11891213
}
11901214

11911215
// Wrap the watchtower client DB and make sure we clean up.

config_prod.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//go:build !test_native_sql
2+
3+
package lnd
4+
5+
import (
6+
"context"
7+
8+
graphdb "github.com/lightningnetwork/lnd/graph/db"
9+
"github.com/lightningnetwork/lnd/sqldb"
10+
"github.com/lightningnetwork/lnd/sqldb/sqlc"
11+
)
12+
13+
func getSQLMigration(ctx context.Context, version int,
14+
kvGraphStore *graphdb.KVStore) (func(tx *sqlc.Queries) error, bool) {
15+
16+
return nil, false
17+
}
18+
19+
func (d *DefaultDatabaseBuilder) getGraphStore(_ *sqldb.BaseDB,
20+
kvGraphStore *graphdb.KVStore,
21+
_ ...graphdb.StoreOptionModifier) (graphdb.V1Store, error) {
22+
23+
return kvGraphStore, nil
24+
}

config_test_native_sql.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
//go:build test_native_sql
2+
3+
package lnd
4+
5+
import (
6+
"context"
7+
"database/sql"
8+
"fmt"
9+
10+
graphdb "github.com/lightningnetwork/lnd/graph/db"
11+
"github.com/lightningnetwork/lnd/sqldb"
12+
"github.com/lightningnetwork/lnd/sqldb/sqlc"
13+
)
14+
15+
const graphSQLMigration = 9
16+
17+
func getSQLMigration(ctx context.Context, version int,
18+
kvGraphStore *graphdb.KVStore) (func(tx *sqlc.Queries) error, bool) {
19+
20+
if version != graphSQLMigration {
21+
return nil, false
22+
}
23+
24+
return func(tx *sqlc.Queries) error {
25+
err := graphdb.MigrateGraphToSQL(
26+
ctx, kvGraphStore, tx,
27+
)
28+
if err != nil {
29+
return fmt.Errorf("failed to migrate "+
30+
"graph to SQL: %w", err)
31+
}
32+
33+
return nil
34+
}, true
35+
}
36+
37+
func (d *DefaultDatabaseBuilder) getGraphStore(baseDB *sqldb.BaseDB,
38+
_ *graphdb.KVStore,
39+
opts ...graphdb.StoreOptionModifier) (graphdb.V1Store, error) {
40+
41+
graphExecutor := sqldb.NewTransactionExecutor(
42+
baseDB, func(tx *sql.Tx) graphdb.SQLQueries {
43+
return baseDB.WithTx(tx)
44+
},
45+
)
46+
47+
return graphdb.NewSQLStore(
48+
&graphdb.SQLStoreConfig{
49+
ChainHash: *d.cfg.ActiveNetParams.GenesisHash,
50+
},
51+
graphExecutor, opts...,
52+
)
53+
}

sqldb/migrations_dev.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//go:build test_db_postgres || test_db_sqlite
1+
//go:build test_db_postgres || test_db_sqlite || test_native_sql
22

33
package sqldb
44

@@ -8,4 +8,13 @@ var migrationAdditions = []MigrationConfig{
88
Version: 8,
99
SchemaVersion: 7,
1010
},
11+
{
12+
Name: "kv_graph_migration",
13+
Version: 9,
14+
SchemaVersion: 7,
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+
},
1120
}

sqldb/migrations_prod.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//go:build !test_db_postgres && !test_db_sqlite
1+
//go:build !test_db_postgres && !test_db_sqlite && !test_native_sql
22

33
package sqldb
44

0 commit comments

Comments
 (0)