mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-12-07 11:22:37 +01:00
sqldb: add helpers to create test DBs migrated up to a select version
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user