mirror of
https://github.com/lightningnetwork/lnd.git
synced 2024-11-19 01:43:16 +01:00
sqldb: add helpers to create test DBs migrated up to a select version
This commit is contained in:
parent
5292c76e10
commit
ed36598504
@ -124,28 +124,28 @@ func (f *TestPgFixture) TearDown(t *testing.T) {
|
||||
require.NoError(t, err, "Could not purge resource")
|
||||
}
|
||||
|
||||
// randomDBName generates a random database name.
|
||||
func randomDBName(t *testing.T) string {
|
||||
randBytes := make([]byte, 8)
|
||||
_, err := rand.Read(randBytes)
|
||||
require.NoError(t, err)
|
||||
|
||||
return "test_" + hex.EncodeToString(randBytes)
|
||||
}
|
||||
|
||||
// NewTestPostgresDB is a helper function that creates a Postgres database for
|
||||
// testing using the given fixture.
|
||||
func NewTestPostgresDB(t *testing.T, fixture *TestPgFixture) *PostgresStore {
|
||||
t.Helper()
|
||||
|
||||
// Create random database name.
|
||||
randBytes := make([]byte, 8)
|
||||
_, err := rand.Read(randBytes)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
dbName := "test_" + hex.EncodeToString(randBytes)
|
||||
dbName := randomDBName(t)
|
||||
|
||||
t.Logf("Creating new Postgres DB '%s' for testing", dbName)
|
||||
|
||||
_, err = fixture.db.ExecContext(
|
||||
_, err := fixture.db.ExecContext(
|
||||
context.Background(), "CREATE DATABASE "+dbName,
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
require.NoError(t, err)
|
||||
|
||||
cfg := fixture.GetConfig(dbName)
|
||||
store, err := NewPostgresStore(cfg)
|
||||
@ -153,3 +153,30 @@ func NewTestPostgresDB(t *testing.T, fixture *TestPgFixture) *PostgresStore {
|
||||
|
||||
return store
|
||||
}
|
||||
|
||||
// NewTestPostgresDBWithVersion is a helper function that creates a Postgres
|
||||
// database for testing and migrates it to the given version.
|
||||
func NewTestPostgresDBWithVersion(t *testing.T, fixture *TestPgFixture,
|
||||
version uint) *PostgresStore {
|
||||
|
||||
t.Helper()
|
||||
|
||||
t.Logf("Creating new Postgres DB for testing, migrating to version %d",
|
||||
version)
|
||||
|
||||
dbName := randomDBName(t)
|
||||
_, err := fixture.db.ExecContext(
|
||||
context.Background(), "CREATE DATABASE "+dbName,
|
||||
)
|
||||
require.NoError(t, err)
|
||||
|
||||
storeCfg := fixture.GetConfig(dbName)
|
||||
storeCfg.SkipMigrations = true
|
||||
store, err := NewPostgresStore(storeCfg)
|
||||
require.NoError(t, err)
|
||||
|
||||
err = store.ExecuteMigrations(TargetVersion(version))
|
||||
require.NoError(t, err)
|
||||
|
||||
return store
|
||||
}
|
||||
|
@ -9,5 +9,21 @@ import (
|
||||
|
||||
// NewTestDB is a helper function that creates a Postgres database for testing.
|
||||
func NewTestDB(t *testing.T) *PostgresStore {
|
||||
return NewTestPostgresDB(t)
|
||||
pgFixture := NewTestPgFixture(t, DefaultPostgresFixtureLifetime)
|
||||
t.Cleanup(func() {
|
||||
pgFixture.TearDown(t)
|
||||
})
|
||||
|
||||
return NewTestPostgresDB(t, pgFixture)
|
||||
}
|
||||
|
||||
// NewTestDBWithVersion is a helper function that creates a Postgres database
|
||||
// for testing and migrates it to the given version.
|
||||
func NewTestDBWithVersion(t *testing.T, version uint) *PostgresStore {
|
||||
pgFixture := NewTestPgFixture(t, DefaultPostgresFixtureLifetime)
|
||||
t.Cleanup(func() {
|
||||
pgFixture.TearDown(t)
|
||||
})
|
||||
|
||||
return NewTestPostgresDBWithVersion(t, pgFixture, version)
|
||||
}
|
||||
|
@ -166,3 +166,29 @@ func NewTestSqliteDB(t *testing.T) *SqliteStore {
|
||||
|
||||
return sqlDB
|
||||
}
|
||||
|
||||
// NewTestSqliteDBWithVersion is a helper function that creates an SQLite
|
||||
// database for testing and migrates it to the given version.
|
||||
func NewTestSqliteDBWithVersion(t *testing.T, version uint) *SqliteStore {
|
||||
t.Helper()
|
||||
|
||||
t.Logf("Creating new SQLite DB for testing, migrating to version %d",
|
||||
version)
|
||||
|
||||
// TODO(roasbeef): if we pass :memory: for the file name, then we get
|
||||
// an in mem version to speed up tests
|
||||
dbFileName := filepath.Join(t.TempDir(), "tmp.db")
|
||||
sqlDB, err := NewSqliteStore(&SqliteConfig{
|
||||
SkipMigrations: true,
|
||||
}, dbFileName)
|
||||
require.NoError(t, err)
|
||||
|
||||
err = sqlDB.ExecuteMigrations(TargetVersion(version))
|
||||
require.NoError(t, err)
|
||||
|
||||
t.Cleanup(func() {
|
||||
require.NoError(t, sqlDB.DB.Close())
|
||||
})
|
||||
|
||||
return sqlDB
|
||||
}
|
||||
|
@ -11,3 +11,9 @@ import (
|
||||
func NewTestDB(t *testing.T) *SqliteStore {
|
||||
return NewTestSqliteDB(t)
|
||||
}
|
||||
|
||||
// NewTestDBWithVersion is a helper function that creates an SQLite database
|
||||
// for testing and migrates it to the given version.
|
||||
func NewTestDBWithVersion(t *testing.T, version uint) *SqliteStore {
|
||||
return NewTestSqliteDBWithVersion(t, version)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user