Skip to content

Commit 32f2457

Browse files
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.
1 parent 65ac971 commit 32f2457

File tree

5 files changed

+48
-7
lines changed

5 files changed

+48
-7
lines changed

accounts/test_sqlite.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ var ErrDBClosed = errors.New("database is closed")
1818
// NewTestDB is a helper function that creates an SQLStore database for testing.
1919
func NewTestDB(t *testing.T, clock clock.Clock) Store {
2020
return createStore(
21-
t, sqldb.NewTestSqliteDB(t, db.LitdMigrationStreams).BaseDB,
21+
t,
22+
sqldb.NewTestSqliteDB(t, db.MakeTestMigrationStreams()).BaseDB,
2223
clock,
2324
)
2425
}
@@ -28,7 +29,9 @@ func NewTestDB(t *testing.T, clock clock.Clock) Store {
2829
func NewTestDBFromPath(t *testing.T, dbPath string,
2930
clock clock.Clock) Store {
3031

31-
tDb := sqldb.NewTestSqliteDBFromPath(t, dbPath, db.LitdMigrationStreams)
32+
tDb := sqldb.NewTestSqliteDBFromPath(
33+
t, dbPath, db.MakeTestMigrationStreams(),
34+
)
3235

3336
return createStore(t, tDb.BaseDB, clock)
3437
}

db/migrations.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
package db
22

3+
import (
4+
"github.com/golang-migrate/migrate/v4"
5+
"github.com/golang-migrate/migrate/v4/database/pgx/v5"
6+
"github.com/lightningnetwork/lnd/sqldb/v2"
7+
)
8+
39
const (
410
// LatestMigrationVersion is the latest migration version of the
511
// database. This is used to implement downgrade protection for the
@@ -8,3 +14,29 @@ const (
814
// NOTE: This MUST be updated when a new migration is added.
915
LatestMigrationVersion = 5
1016
)
17+
18+
// MakeTestMigrationStreams creates the migration streams for the unit test
19+
// environment.
20+
func MakeTestMigrationStreams() []sqldb.MigrationStream {
21+
migStream := sqldb.MigrationStream{
22+
MigrateTableName: pgx.DefaultMigrationsTable,
23+
SQLFileDirectory: "sqlc/migrations",
24+
Schemas: SqlSchemas,
25+
26+
// LatestMigrationVersion is the latest migration version of the
27+
// database. This is used to implement downgrade protection for
28+
// the daemon.
29+
//
30+
// NOTE: This MUST be updated when a new migration is added.
31+
LatestMigrationVersion: LatestMigrationVersion,
32+
33+
MakePostMigrationChecks: func(
34+
db *sqldb.BaseDB) (map[uint]migrate.PostStepCallback,
35+
error) {
36+
37+
return make(map[uint]migrate.PostStepCallback), nil
38+
},
39+
}
40+
41+
return []sqldb.MigrationStream{migStream}
42+
}

db/postgres.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,5 +68,5 @@ func NewTestPostgresDB(t *testing.T) *sqldb.PostgresStore {
6868
sqlFixture.TearDown(t)
6969
})
7070

71-
return sqldb.NewTestPostgresDB(t, sqlFixture, LitdMigrationStreams)
71+
return sqldb.NewTestPostgresDB(t, sqlFixture, MakeTestMigrationStreams())
7272
}

firewalldb/test_sqlite.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ import (
1313
// NewTestDB is a helper function that creates an BBolt database for testing.
1414
func NewTestDB(t *testing.T, clock clock.Clock) FirewallDBs {
1515
return createStore(
16-
t, sqldb.NewTestSqliteDB(t, db.LitdMigrationStreams).BaseDB,
16+
t,
17+
sqldb.NewTestSqliteDB(t, db.MakeTestMigrationStreams()).BaseDB,
1718
clock,
1819
)
1920
}
@@ -23,7 +24,9 @@ func NewTestDB(t *testing.T, clock clock.Clock) FirewallDBs {
2324
func NewTestDBFromPath(t *testing.T, dbPath string,
2425
clock clock.Clock) FirewallDBs {
2526

26-
tDb := sqldb.NewTestSqliteDBFromPath(t, dbPath, db.LitdMigrationStreams)
27+
tDb := sqldb.NewTestSqliteDBFromPath(
28+
t, dbPath, db.MakeTestMigrationStreams(),
29+
)
2730

2831
return createStore(t, tDb.BaseDB, clock)
2932
}

session/test_sqlite.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ var ErrDBClosed = errors.New("database is closed")
1818
// NewTestDB is a helper function that creates an SQLStore database for testing.
1919
func NewTestDB(t *testing.T, clock clock.Clock) Store {
2020
return createStore(
21-
t, sqldb.NewTestSqliteDB(t, db.LitdMigrationStreams).BaseDB,
21+
t,
22+
sqldb.NewTestSqliteDB(t, db.MakeTestMigrationStreams()).BaseDB,
2223
clock,
2324
)
2425
}
@@ -28,7 +29,9 @@ func NewTestDB(t *testing.T, clock clock.Clock) Store {
2829
func NewTestDBFromPath(t *testing.T, dbPath string,
2930
clock clock.Clock) Store {
3031

31-
tDb := sqldb.NewTestSqliteDBFromPath(t, dbPath, db.LitdMigrationStreams)
32+
tDb := sqldb.NewTestSqliteDBFromPath(
33+
t, dbPath, db.MakeTestMigrationStreams(),
34+
)
3235

3336
return createStore(t, tDb.BaseDB, clock)
3437
}

0 commit comments

Comments
 (0)