Skip to content

Commit a12d714

Browse files
multi: add dev kvdb to sql code migration
Add the necessary code to trigger the kvdb to sql code migration in dev builds.
1 parent a0be3bb commit a12d714

9 files changed

+173
-16
lines changed

config_dev.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
package terminal
44

55
import (
6+
"context"
67
"fmt"
78
"path/filepath"
89

@@ -87,7 +88,9 @@ func defaultDevConfig() *DevConfig {
8788
}
8889

8990
// NewStores creates a new stores instance based on the chosen database backend.
90-
func NewStores(cfg *Config, clock clock.Clock) (*stores, error) {
91+
func NewStores(ctx context.Context, cfg *Config,
92+
clock clock.Clock) (*stores, error) {
93+
9194
var (
9295
networkDir = filepath.Join(cfg.LitDir, cfg.Network)
9396
stores = &stores{
@@ -114,7 +117,10 @@ func NewStores(cfg *Config, clock clock.Clock) (*stores, error) {
114117

115118
if !cfg.Sqlite.SkipMigrations {
116119
err = sqldb.ApplyAllMigrations(
117-
sqlStore, migrationstreams.LitdMigrationStreams,
120+
sqlStore,
121+
migrationstreams.MakeMigrationStreams(
122+
ctx, cfg.MacaroonPath, clock,
123+
),
118124
)
119125
if err != nil {
120126
return stores, fmt.Errorf("error applying "+
@@ -156,7 +162,10 @@ func NewStores(cfg *Config, clock clock.Clock) (*stores, error) {
156162

157163
if !cfg.Postgres.SkipMigrations {
158164
err = sqldb.ApplyAllMigrations(
159-
sqlStore, migrationstreams.LitdMigrationStreams,
165+
sqlStore,
166+
migrationstreams.MakeMigrationStreams(
167+
ctx, cfg.MacaroonPath, clock,
168+
),
160169
)
161170
if err != nil {
162171
return stores, fmt.Errorf("error applying "+

config_prod.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
package terminal
44

55
import (
6+
"context"
67
"fmt"
78
"path/filepath"
89

@@ -29,7 +30,9 @@ func (c *DevConfig) Validate(_, _ string) error {
2930

3031
// NewStores creates a new instance of the stores struct using the default Bolt
3132
// backend since in production, this is currently the only backend supported.
32-
func NewStores(cfg *Config, clock clock.Clock) (*stores, error) {
33+
func NewStores(_ context.Context, cfg *Config,
34+
clock clock.Clock) (*stores, error) {
35+
3336
networkDir := filepath.Join(cfg.LitDir, cfg.Network)
3437

3538
stores := &stores{

db/migrations.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ const (
2828
// environment.
2929
//
3030
// NOTE: This function is not located in the migrationstreams package to avoid
31-
// cyclic dependencies.
31+
// cyclic dependencies. This test migration stream does not run the kvdb to sql
32+
// migration, as we already have separate unit tests which tests the migration.
3233
func MakeTestMigrationStreams() []sqldb.MigrationStream {
3334
migStream := sqldb.MigrationStream{
3435
MigrateTableName: pgx.DefaultMigrationsTable,
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
//go:build dev
2+
3+
package migrationstreams
4+
5+
import (
6+
"context"
7+
"database/sql"
8+
"fmt"
9+
"path/filepath"
10+
"time"
11+
12+
"github.com/golang-migrate/migrate/v4"
13+
"github.com/golang-migrate/migrate/v4/database"
14+
"github.com/lightninglabs/lightning-terminal/accounts"
15+
"github.com/lightninglabs/lightning-terminal/db/sqlcmig6"
16+
"github.com/lightninglabs/lightning-terminal/firewalldb"
17+
"github.com/lightninglabs/lightning-terminal/session"
18+
"github.com/lightningnetwork/lnd/clock"
19+
"github.com/lightningnetwork/lnd/sqldb/v2"
20+
)
21+
22+
// MakePostStepCallbacksMig6 turns the post migration checks into a map of post
23+
// step callbacks that can be used with the migrate package. The keys of the map
24+
// are the migration versions, and the values are the callbacks that will be
25+
// executed after the migration with the corresponding version is applied.
26+
func MakePostStepCallbacksMig6(ctx context.Context, db *sqldb.BaseDB,
27+
macPath string, clock clock.Clock,
28+
migVersion uint) migrate.PostStepCallback {
29+
30+
mig6queries := sqlcmig6.NewForType(db, db.BackendType)
31+
mig6executor := sqldb.NewTransactionExecutor(
32+
db, func(tx *sql.Tx) *sqlcmig6.Queries {
33+
return mig6queries.WithTx(tx)
34+
},
35+
)
36+
37+
return func(_ *migrate.Migration, _ database.Driver) error {
38+
// We ignore the actual driver that's being returned here, since
39+
// we use migrate.NewWithInstance() to create the migration
40+
// instance from our already instantiated database backend that
41+
// is also passed into this function.
42+
return mig6executor.ExecTx(
43+
ctx, sqldb.NewWriteTx(),
44+
func(q6 *sqlcmig6.Queries) error {
45+
log.Infof("Running post migration callback "+
46+
"for migration version %d", migVersion)
47+
48+
return kvdbToSqlMigrationCallback(
49+
ctx, macPath, db, clock, q6,
50+
)
51+
}, sqldb.NoOpReset,
52+
)
53+
}
54+
}
55+
56+
func kvdbToSqlMigrationCallback(ctx context.Context, macPath string,
57+
_ *sqldb.BaseDB, clock clock.Clock, q *sqlcmig6.Queries) error {
58+
59+
start := time.Now()
60+
log.Infof("Starting KVDB to SQL migration for all stores")
61+
62+
accountStore, err := accounts.NewBoltStore(
63+
filepath.Dir(macPath), accounts.DBFilename, clock,
64+
)
65+
if err != nil {
66+
return err
67+
}
68+
69+
err = accounts.MigrateAccountStoreToSQL(ctx, accountStore.DB, q)
70+
if err != nil {
71+
return fmt.Errorf("error migrating account store to "+
72+
"SQL: %v", err)
73+
}
74+
75+
sessionStore, err := session.NewDB(
76+
filepath.Dir(macPath), session.DBFilename,
77+
clock, accountStore,
78+
)
79+
if err != nil {
80+
return err
81+
}
82+
83+
err = session.MigrateSessionStoreToSQL(ctx, sessionStore.DB, q)
84+
if err != nil {
85+
return fmt.Errorf("error migrating session store to "+
86+
"SQL: %v", err)
87+
}
88+
89+
firewallStore, err := firewalldb.NewBoltDB(
90+
filepath.Dir(macPath), firewalldb.DBFilename,
91+
sessionStore, accountStore, clock,
92+
)
93+
if err != nil {
94+
return err
95+
}
96+
97+
err = firewalldb.MigrateFirewallDBToSQL(ctx, firewallStore.DB, q)
98+
if err != nil {
99+
return fmt.Errorf("error migrating firewalldb store "+
100+
"to SQL: %v", err)
101+
}
102+
103+
log.Infof("Succesfully migrated all KVDB stores to SQL in: %v",
104+
time.Since(start))
105+
106+
return nil
107+
}

db/migrationstreams/sql_migrations.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,20 @@
33
package migrationstreams
44

55
import (
6+
"context"
67
"github.com/golang-migrate/migrate/v4"
78
"github.com/golang-migrate/migrate/v4/database/pgx/v5"
89
"github.com/lightninglabs/lightning-terminal/db"
10+
"github.com/lightningnetwork/lnd/clock"
911
"github.com/lightningnetwork/lnd/sqldb/v2"
1012
)
1113

12-
var (
13-
LitdMigrationStream = sqldb.MigrationStream{
14+
// MakeMigrationStreams creates the migration streams for production
15+
// environments.
16+
func MakeMigrationStreams(_ context.Context, _ string,
17+
_ clock.Clock) []sqldb.MigrationStream {
18+
19+
migStream := sqldb.MigrationStream{
1420
MigrateTableName: pgx.DefaultMigrationsTable,
1521
SQLFileDirectory: "sqlc/migrations",
1622
Schemas: db.SqlSchemas,
@@ -29,5 +35,6 @@ var (
2935
return make(map[uint]migrate.PostStepCallback), nil
3036
},
3137
}
32-
LitdMigrationStreams = []sqldb.MigrationStream{LitdMigrationStream}
33-
)
38+
39+
return []sqldb.MigrationStream{migStream}
40+
}

db/migrationstreams/sql_migrations_dev.go

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,30 @@
33
package migrationstreams
44

55
import (
6+
"context"
67
"github.com/golang-migrate/migrate/v4"
78
"github.com/golang-migrate/migrate/v4/database/pgx/v5"
89
"github.com/lightninglabs/lightning-terminal/db"
10+
"github.com/lightningnetwork/lnd/clock"
911
"github.com/lightningnetwork/lnd/sqldb/v2"
1012
)
1113

12-
var (
14+
const (
15+
// KVDBtoSQLMigVersion is the version of the migration that migrates the
16+
// kvdb to the sql database.
17+
//
18+
// TODO: When this the kvdb to sql migration goes live into prod, this
19+
// should be moved to non dev db/migrations.go file, and this constant
20+
// value should be updated to reflect the real migration number.
21+
KVDBtoSQLMigVersion = 1
22+
)
23+
24+
// MakeMigrationStreams creates the migration streams for the dev environments.
25+
func MakeMigrationStreams(ctx context.Context, macPath string,
26+
clock clock.Clock) []sqldb.MigrationStream {
27+
1328
// Create the prod migration stream.
14-
migStream = sqldb.MigrationStream{
29+
migStream := sqldb.MigrationStream{
1530
MigrateTableName: pgx.DefaultMigrationsTable,
1631
SQLFileDirectory: "sqlc/migrations",
1732
Schemas: db.SqlSchemas,
@@ -32,7 +47,7 @@ var (
3247
}
3348

3449
// Create the dev migration stream.
35-
migStreamDev = sqldb.MigrationStream{
50+
migStreamDev := sqldb.MigrationStream{
3651
MigrateTableName: pgx.DefaultMigrationsTable + "_dev",
3752
SQLFileDirectory: "sqlc/migrations_dev",
3853
Schemas: db.SqlSchemas,
@@ -48,8 +63,23 @@ var (
4863
db *sqldb.BaseDB) (map[uint]migrate.PostStepCallback,
4964
error) {
5065

51-
return make(map[uint]migrate.PostStepCallback), nil
66+
// Any Callbacks added to this map will be executed when
67+
// after the dev migration number for the uint key in
68+
// the map has been applied. If no entry exists for a
69+
// given uint, then no callback will be executed for
70+
// that migration number. This is useful for adding a
71+
// code migration step as a callback to be run
72+
// after a specific migration of a given number has been
73+
// applied.
74+
res := make(map[uint]migrate.PostStepCallback)
75+
76+
res[KVDBtoSQLMigVersion] = MakePostStepCallbacksMig6(
77+
ctx, db, macPath, clock, KVDBtoSQLMigVersion,
78+
)
79+
80+
return res, nil
5281
},
5382
}
54-
LitdMigrationStreams = []sqldb.MigrationStream{migStream, migStreamDev}
55-
)
83+
84+
return []sqldb.MigrationStream{migStream, migStreamDev}
85+
}

terminal.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,7 @@ func (g *LightningTerminal) start(ctx context.Context) error {
447447
return fmt.Errorf("could not create network directory: %v", err)
448448
}
449449

450-
g.stores, err = NewStores(g.cfg, clock.NewDefaultClock())
450+
g.stores, err = NewStores(ctx, g.cfg, clock.NewDefaultClock())
451451
if err != nil {
452452
return fmt.Errorf("could not create stores: %v", err)
453453
}

0 commit comments

Comments
 (0)