Skip to content

Commit a7e1e49

Browse files
committed
plug into itests behind feature flag
1 parent ad28b35 commit a7e1e49

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(
@@ -1098,6 +1088,8 @@ func (d *DefaultDatabaseBuilder) BuildDatabase(
10981088
return nil, nil, err
10991089
}
11001090

1091+
var graphStore graphdb.V1Store
1092+
11011093
// Instantiate a native SQL store if the flag is set.
11021094
if d.cfg.DB.UseNativeSQL {
11031095
migrations := sqldb.GetMigrations()
@@ -1110,7 +1102,7 @@ func (d *DefaultDatabaseBuilder) BuildDatabase(
11101102
// migration's version (7), it will be skipped permanently,
11111103
// regardless of the flag.
11121104
if !d.cfg.DB.SkipNativeSQLMigration {
1113-
migrationFn := func(tx *sqlc.Queries) error {
1105+
invoiceMigFn := func(tx *sqlc.Queries) error {
11141106
err := invoices.MigrateInvoicesToSQL(
11151107
ctx, dbs.ChanStateDB.Backend,
11161108
dbs.ChanStateDB, tx,
@@ -1132,11 +1124,21 @@ func (d *DefaultDatabaseBuilder) BuildDatabase(
11321124
// Make sure we attach the custom migration function to
11331125
// the correct migration version.
11341126
for i := 0; i < len(migrations); i++ {
1135-
if migrations[i].Version != invoiceMigration {
1127+
version := migrations[i].Version
1128+
if version == invoiceMigration {
1129+
migrations[i].MigrationFn = invoiceMigFn
1130+
1131+
continue
1132+
}
1133+
1134+
migFn, ok := getSQLMigration(
1135+
ctx, version, kvGraphStore,
1136+
)
1137+
if !ok {
11361138
continue
11371139
}
11381140

1139-
migrations[i].MigrationFn = migrationFn
1141+
migrations[i].MigrationFn = migFn
11401142
}
11411143
}
11421144

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

11651167
sqlInvoiceDB := invoices.NewSQLStore(
1166-
executor, clock.NewDefaultClock(),
1168+
invoiceExecutor, clock.NewDefaultClock(),
11671169
)
11681170

11691171
dbs.InvoiceDB = sqlInvoiceDB
1172+
1173+
graphStore, err = d.getGraphStore(
1174+
baseDB, kvGraphStore, graphDBOptions...,
1175+
)
1176+
if err != nil {
1177+
err = fmt.Errorf("unable to get graph store: %w", err)
1178+
d.logger.Error(err)
1179+
1180+
return nil, nil, err
1181+
}
11701182
} else {
11711183
// Check if the invoice bucket tombstone is set. If it is, we
11721184
// need to return and ask the user switch back to using the
@@ -1188,6 +1200,18 @@ func (d *DefaultDatabaseBuilder) BuildDatabase(
11881200
}
11891201

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

11931217
// 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)