Skip to content

Commit c36359b

Browse files
migrationstreams: introduce migrationstreams pkg
The upcoming kvdb to sql code migration will be added to as part of the `sqldb/v2` migration stream. However, since the kvdb to sql migration will need to use the migration functions present in the `accounts`, `firewalldb`, and `session` packages, the migration will need to referernce those packages. That would lead to a circular dependency though if the migration stream was defined in the `db` package, as those packages need to import the `db` package. To avoid this, we introduce a new `migrationstreams` package that contains the migration streams, and ensure that the `db` package doesn't import the `migrationstreams` package.
1 parent 32f2457 commit c36359b

File tree

5 files changed

+40
-5
lines changed

5 files changed

+40
-5
lines changed

config_dev.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
"github.com/lightninglabs/lightning-terminal/accounts"
1010
"github.com/lightninglabs/lightning-terminal/db"
11+
"github.com/lightninglabs/lightning-terminal/db/migrationstreams"
1112
"github.com/lightninglabs/lightning-terminal/db/sqlc"
1213
"github.com/lightninglabs/lightning-terminal/firewalldb"
1314
"github.com/lightninglabs/lightning-terminal/session"
@@ -113,7 +114,7 @@ func NewStores(cfg *Config, clock clock.Clock) (*stores, error) {
113114

114115
if !cfg.Sqlite.SkipMigrations {
115116
err = sqldb.ApplyAllMigrations(
116-
sqlStore, db.LitdMigrationStreams,
117+
sqlStore, migrationstreams.LitdMigrationStreams,
117118
)
118119
if err != nil {
119120
return stores, fmt.Errorf("error applying "+
@@ -155,7 +156,7 @@ func NewStores(cfg *Config, clock clock.Clock) (*stores, error) {
155156

156157
if !cfg.Postgres.SkipMigrations {
157158
err = sqldb.ApplyAllMigrations(
158-
sqlStore, db.LitdMigrationStreams,
159+
sqlStore, migrationstreams.LitdMigrationStreams,
159160
)
160161
if err != nil {
161162
return stores, fmt.Errorf("error applying "+

db/migrations.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ const (
1717

1818
// MakeTestMigrationStreams creates the migration streams for the unit test
1919
// environment.
20+
//
21+
// NOTE: This function is not located in the migrationstreams package to avoid
22+
// cyclic dependencies.
2023
func MakeTestMigrationStreams() []sqldb.MigrationStream {
2124
migStream := sqldb.MigrationStream{
2225
MigrateTableName: pgx.DefaultMigrationsTable,

db/migrationstreams/log.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package migrationstreams
2+
3+
import (
4+
"github.com/btcsuite/btclog/v2"
5+
"github.com/lightningnetwork/lnd/build"
6+
)
7+
8+
const Subsystem = "MIGS"
9+
10+
// log is a logger that is initialized with no output filters. This
11+
// means the package will not perform any logging by default until the caller
12+
// requests it.
13+
var log btclog.Logger
14+
15+
// The default amount of logging is none.
16+
func init() {
17+
UseLogger(build.NewSubLogger(Subsystem, nil))
18+
}
19+
20+
// UseLogger uses a specified Logger to output package logging info.
21+
// This should be used in preference to SetLogWriter if the caller is also
22+
// using btclog.
23+
func UseLogger(logger btclog.Logger) {
24+
log = logger
25+
}

db/sql_migrations.go renamed to db/migrationstreams/sql_migrations.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
1-
package db
1+
package migrationstreams
22

33
import (
44
"github.com/golang-migrate/migrate/v4"
55
"github.com/golang-migrate/migrate/v4/database/pgx/v5"
6+
"github.com/lightninglabs/lightning-terminal/db"
67
"github.com/lightningnetwork/lnd/sqldb/v2"
78
)
89

910
var (
1011
LitdMigrationStream = sqldb.MigrationStream{
1112
MigrateTableName: pgx.DefaultMigrationsTable,
1213
SQLFileDirectory: "sqlc/migrations",
13-
Schemas: SqlSchemas,
14+
Schemas: db.SqlSchemas,
1415

1516
// LatestMigrationVersion is the latest migration version of the
1617
// database. This is used to implement downgrade protection for
1718
// the daemon.
1819
//
1920
// NOTE: This MUST be updated when a new migration is added.
20-
LatestMigrationVersion: LatestMigrationVersion,
21+
LatestMigrationVersion: db.LatestMigrationVersion,
2122

2223
MakePostMigrationChecks: func(
2324
db *sqldb.BaseDB) (map[uint]migrate.PostStepCallback,

log.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/lightninglabs/lightning-terminal/accounts"
99
"github.com/lightninglabs/lightning-terminal/autopilotserver"
1010
"github.com/lightninglabs/lightning-terminal/db"
11+
"github.com/lightninglabs/lightning-terminal/db/migrationstreams"
1112
"github.com/lightninglabs/lightning-terminal/firewall"
1213
"github.com/lightninglabs/lightning-terminal/firewalldb"
1314
mid "github.com/lightninglabs/lightning-terminal/rpcmiddleware"
@@ -91,6 +92,10 @@ func SetupLoggers(root *build.SubLoggerManager, intercept signal.Interceptor) {
9192
root, subservers.Subsystem, intercept, subservers.UseLogger,
9293
)
9394
lnd.AddSubLogger(root, db.Subsystem, intercept, db.UseLogger)
95+
lnd.AddSubLogger(
96+
root, migrationstreams.Subsystem, intercept,
97+
migrationstreams.UseLogger,
98+
)
9499

95100
// Add daemon loggers to lnd's root logger.
96101
faraday.SetupLoggers(root, intercept)

0 commit comments

Comments
 (0)