Skip to content

Commit cdedffd

Browse files
committed
tapdb: add NewTestDBWithVersion
1 parent 65176ff commit cdedffd

File tree

4 files changed

+63
-0
lines changed

4 files changed

+63
-0
lines changed

tapdb/postgres.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,3 +171,27 @@ func NewTestPostgresDB(t *testing.T) *PostgresStore {
171171

172172
return store
173173
}
174+
175+
// NewTestPostgresDBWithVersion is a helper function that creates a Postgres
176+
// database for testing and migrates it to the given version.
177+
func NewTestPostgresDBWithVersion(t *testing.T, version uint) *PostgresStore {
178+
t.Helper()
179+
180+
t.Logf("Creating new Postgres DB for testing, migrating to version %d",
181+
version)
182+
183+
sqlFixture := NewTestPgFixture(t, DefaultPostgresFixtureLifetime, true)
184+
storeCfg := sqlFixture.GetConfig()
185+
storeCfg.SkipMigrations = true
186+
store, err := NewPostgresStore(storeCfg)
187+
require.NoError(t, err)
188+
189+
err = store.ExecuteMigrations(TargetVersion(version))
190+
require.NoError(t, err)
191+
192+
t.Cleanup(func() {
193+
sqlFixture.TearDown(t)
194+
})
195+
196+
return store
197+
}

tapdb/sqlite.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,3 +187,30 @@ func NewTestSqliteDB(t *testing.T) *SqliteStore {
187187

188188
return sqlDB
189189
}
190+
191+
// NewTestSqliteDBWithVersion is a helper function that creates an SQLite
192+
// database for testing and migrates it to the given version.
193+
func NewTestSqliteDBWithVersion(t *testing.T, version uint) *SqliteStore {
194+
t.Helper()
195+
196+
t.Logf("Creating new SQLite DB for testing, migrating to version %d",
197+
version)
198+
199+
// TODO(roasbeef): if we pass :memory: for the file name, then we get
200+
// an in mem version to speed up tests
201+
dbFileName := filepath.Join(t.TempDir(), "tmp.db")
202+
sqlDB, err := NewSqliteStore(&SqliteConfig{
203+
DatabaseFileName: dbFileName,
204+
SkipMigrations: true,
205+
})
206+
require.NoError(t, err)
207+
208+
err = sqlDB.ExecuteMigrations(TargetVersion(version))
209+
require.NoError(t, err)
210+
211+
t.Cleanup(func() {
212+
require.NoError(t, sqlDB.DB.Close())
213+
})
214+
215+
return sqlDB
216+
}

tapdb/test_postgres.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,9 @@ import (
1010
func NewTestDB(t *testing.T) *PostgresStore {
1111
return NewTestPostgresDB(t)
1212
}
13+
14+
// NewTestDBWithVersion is a helper function that creates a Postgres database
15+
// for testing and migrates it to the given version.
16+
func NewTestDBWithVersion(t *testing.T, version uint) *PostgresStore {
17+
return NewTestPostgresDBWithVersion(t, version)
18+
}

tapdb/test_sqlite.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,9 @@ import (
1010
func NewTestDB(t *testing.T) *SqliteStore {
1111
return NewTestSqliteDB(t)
1212
}
13+
14+
// NewTestDBWithVersion is a helper function that creates an SQLite database for
15+
// testing and migrates it to the given version.
16+
func NewTestDBWithVersion(t *testing.T, version uint) *SqliteStore {
17+
return NewTestSqliteDBWithVersion(t, version)
18+
}

0 commit comments

Comments
 (0)