migstreams: introduce migstreams package

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 `migstreams` package that
contains the migration streams, and ensure that the `db` package doesn't
import the `migstreams` package.
This commit is contained in:
Viktor Tigerström 2025-07-11 20:05:45 +02:00 committed by Viktor Torstensson
parent 08b91b09ef
commit 0d96fd8dad
No known key found for this signature in database
GPG key ID: 961CC8259AE675D4
5 changed files with 40 additions and 5 deletions

View file

@ -8,6 +8,7 @@ import (
"github.com/lightninglabs/lightning-terminal/accounts"
"github.com/lightninglabs/lightning-terminal/db"
"github.com/lightninglabs/lightning-terminal/db/migstreams"
"github.com/lightninglabs/lightning-terminal/db/sqlc"
"github.com/lightninglabs/lightning-terminal/firewalldb"
"github.com/lightninglabs/lightning-terminal/session"
@ -113,7 +114,7 @@ func NewStores(cfg *Config, clock clock.Clock) (*stores, error) {
if !cfg.Sqlite.SkipMigrations {
err = sqldb.ApplyAllMigrations(
sqlStore, db.LitdMigrationStreams,
sqlStore, migstreams.LitdMigrationStreams,
)
if err != nil {
return stores, fmt.Errorf("error applying "+
@ -155,7 +156,7 @@ func NewStores(cfg *Config, clock clock.Clock) (*stores, error) {
if !cfg.Postgres.SkipMigrations {
err = sqldb.ApplyAllMigrations(
sqlStore, db.LitdMigrationStreams,
sqlStore, migstreams.LitdMigrationStreams,
)
if err != nil {
return stores, fmt.Errorf("error applying "+

View file

@ -17,6 +17,9 @@ const (
// MakeTestMigrationStreams creates the migration streams for the unit test
// environment.
//
// NOTE: This function is not located in the migstreams package to avoid
// cyclic dependencies.
func MakeTestMigrationStreams() []sqldb.MigrationStream {
migStream := sqldb.MigrationStream{
TrackingTableName: pgx.DefaultMigrationsTable,

25
db/migstreams/log.go Normal file
View file

@ -0,0 +1,25 @@
package migstreams
import (
"github.com/btcsuite/btclog/v2"
"github.com/lightningnetwork/lnd/build"
)
const Subsystem = "MIGS"
// log is a logger that is initialized with no output filters. This
// means the package will not perform any logging by default until the caller
// requests it.
var log btclog.Logger
// The default amount of logging is none.
func init() {
UseLogger(build.NewSubLogger(Subsystem, nil))
}
// UseLogger uses a specified Logger to output package logging info.
// This should be used in preference to SetLogWriter if the caller is also
// using btclog.
func UseLogger(logger btclog.Logger) {
log = logger
}

View file

@ -1,8 +1,9 @@
package db
package migstreams
import (
"github.com/golang-migrate/migrate/v4"
"github.com/golang-migrate/migrate/v4/database/pgx/v5"
"github.com/lightninglabs/lightning-terminal/db"
"github.com/lightningnetwork/lnd/sqldb/v2"
)
@ -12,14 +13,14 @@ var (
LitdMigrationStream = sqldb.MigrationStream{
TrackingTableName: pgx.DefaultMigrationsTable,
SQLFileDirectory: "sqlc/migrations",
SQLFiles: SqlSchemas,
SQLFiles: db.SqlSchemas,
// LatestMigrationVersion is the latest migration version of the
// database. This is used to implement downgrade protection for
// the daemon.
//
// NOTE: This MUST be updated when a new migration is added.
LatestMigrationVersion: LatestMigrationVersion,
LatestMigrationVersion: db.LatestMigrationVersion,
MakeProgrammaticMigrations: func(db *sqldb.BaseDB) (
map[uint]migrate.ProgrammaticMigrEntry, error) {

5
log.go
View file

@ -8,6 +8,7 @@ import (
"github.com/lightninglabs/lightning-terminal/accounts"
"github.com/lightninglabs/lightning-terminal/autopilotserver"
"github.com/lightninglabs/lightning-terminal/db"
"github.com/lightninglabs/lightning-terminal/db/migstreams"
"github.com/lightninglabs/lightning-terminal/firewall"
"github.com/lightninglabs/lightning-terminal/firewalldb"
mid "github.com/lightninglabs/lightning-terminal/rpcmiddleware"
@ -93,6 +94,10 @@ func SetupLoggers(root *build.SubLoggerManager, intercept signal.Interceptor) {
)
lnd.AddSubLogger(root, db.Subsystem, intercept, db.UseLogger)
lnd.AddSubLogger(root, sqldb.Subsystem, intercept, sqldb.UseLogger)
lnd.AddSubLogger(
root, migstreams.Subsystem, intercept,
migstreams.UseLogger,
)
// Add daemon loggers to lnd's root logger.
faraday.SetupLoggers(root, intercept)