multi: introduce migration stream for unit tests

In upcoming commits, we will introduce a new migration stream package
that will need to reference the db package, as well as the accounts,
session and firewalldb package in future commits. To avoid circular
dependencies, we therefore introduce a new migration stream that unit
tests can use, in order to avoid having to import the new migration
stream package.
This commit is contained in:
Viktor Torstensson 2025-07-24 02:39:36 +02:00
parent a5f727279f
commit f77ae5dcf2
No known key found for this signature in database
GPG key ID: 961CC8259AE675D4
5 changed files with 49 additions and 7 deletions

View file

@ -18,7 +18,8 @@ var ErrDBClosed = errors.New("database is closed")
// NewTestDB is a helper function that creates an SQLStore database for testing.
func NewTestDB(t *testing.T, clock clock.Clock) Store {
return createStore(
t, sqldb.NewTestSqliteDB(t, db.LitdMigrationStreams).BaseDB,
t,
sqldb.NewTestSqliteDB(t, db.MakeTestMigrationStreams()).BaseDB,
clock,
)
}
@ -28,7 +29,9 @@ func NewTestDB(t *testing.T, clock clock.Clock) Store {
func NewTestDBFromPath(t *testing.T, dbPath string,
clock clock.Clock) Store {
tDb := sqldb.NewTestSqliteDBFromPath(t, dbPath, db.LitdMigrationStreams)
tDb := sqldb.NewTestSqliteDBFromPath(
t, dbPath, db.MakeTestMigrationStreams(),
)
return createStore(t, tDb.BaseDB, clock)
}

View file

@ -1,5 +1,11 @@
package db
import (
"github.com/golang-migrate/migrate/v4"
"github.com/golang-migrate/migrate/v4/database/pgx/v5"
"github.com/lightningnetwork/lnd/sqldb/v2"
)
const (
// LatestMigrationVersion is the latest migration version of the
// database. This is used to implement downgrade protection for the
@ -8,3 +14,28 @@ const (
// NOTE: This MUST be updated when a new migration is added.
LatestMigrationVersion = 5
)
// MakeTestMigrationStreams creates the migration streams for the unit test
// environment.
func MakeTestMigrationStreams() []sqldb.MigrationStream {
migStream := sqldb.MigrationStream{
TrackingTableName: pgx.DefaultMigrationsTable,
SQLFileDirectory: "sqlc/migrations",
SQLFiles: 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,
MakeProgrammaticMigrations: func(db *sqldb.BaseDB) (
map[uint]migrate.ProgrammaticMigrEntry, error) {
return make(map[uint]migrate.ProgrammaticMigrEntry), nil
},
}
return []sqldb.MigrationStream{migStream}
}

View file

@ -68,5 +68,7 @@ func NewTestPostgresDB(t *testing.T) *sqldb.PostgresStore {
sqlFixture.TearDown(t)
})
return sqldb.NewTestPostgresDB(t, sqlFixture, LitdMigrationStreams)
return sqldb.NewTestPostgresDB(
t, sqlFixture, MakeTestMigrationStreams(),
)
}

View file

@ -16,7 +16,8 @@ var isSqlite = true
// NewTestDB is a helper function that creates an BBolt database for testing.
func NewTestDB(t *testing.T, clock clock.Clock) FirewallDBs {
return createStore(
t, sqldb.NewTestSqliteDB(t, db.LitdMigrationStreams).BaseDB,
t,
sqldb.NewTestSqliteDB(t, db.MakeTestMigrationStreams()).BaseDB,
clock,
)
}
@ -26,7 +27,9 @@ func NewTestDB(t *testing.T, clock clock.Clock) FirewallDBs {
func NewTestDBFromPath(t *testing.T, dbPath string,
clock clock.Clock) FirewallDBs {
tDb := sqldb.NewTestSqliteDBFromPath(t, dbPath, db.LitdMigrationStreams)
tDb := sqldb.NewTestSqliteDBFromPath(
t, dbPath, db.MakeTestMigrationStreams(),
)
return createStore(t, tDb.BaseDB, clock)
}

View file

@ -18,7 +18,8 @@ var ErrDBClosed = errors.New("database is closed")
// NewTestDB is a helper function that creates an SQLStore database for testing.
func NewTestDB(t *testing.T, clock clock.Clock) Store {
return createStore(
t, sqldb.NewTestSqliteDB(t, db.LitdMigrationStreams).BaseDB,
t,
sqldb.NewTestSqliteDB(t, db.MakeTestMigrationStreams()).BaseDB,
clock,
)
}
@ -28,7 +29,9 @@ func NewTestDB(t *testing.T, clock clock.Clock) Store {
func NewTestDBFromPath(t *testing.T, dbPath string,
clock clock.Clock) Store {
tDb := sqldb.NewTestSqliteDBFromPath(t, dbPath, db.LitdMigrationStreams)
tDb := sqldb.NewTestSqliteDBFromPath(
t, dbPath, db.MakeTestMigrationStreams(),
)
return createStore(t, tDb.BaseDB, clock)
}