Skip to content

Commit 3e67545

Browse files
committed
tapdb: add func to create test db handle from an existing sqlite file
These new functions aid in investigating an existing tapd sqlite database file.
1 parent 7c0688e commit 3e67545

File tree

4 files changed

+40
-9
lines changed

4 files changed

+40
-9
lines changed

tapdb/sqlite.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -170,13 +170,21 @@ func (s *SqliteStore) ExecuteMigrations(target MigrationTarget) error {
170170
func NewTestSqliteDB(t *testing.T) *SqliteStore {
171171
t.Helper()
172172

173-
dbFileName := filepath.Join(t.TempDir(), "tmp.db")
174-
t.Logf("Creating new SQLite DB for testing: %s", dbFileName)
175-
176173
// TODO(roasbeef): if we pass :memory: for the file name, then we get
177174
// an in mem version to speed up tests
175+
dbPath := filepath.Join(t.TempDir(), "tmp.db")
176+
t.Logf("Creating new SQLite DB handle for testing: %s", dbPath)
177+
178+
return NewTestSqliteDbHandleFromPath(t, dbPath)
179+
}
180+
181+
// NewTestSqliteDbHandleFromPath is a helper function that creates a SQLite
182+
// database handle given a database file path.
183+
func NewTestSqliteDbHandleFromPath(t *testing.T, dbPath string) *SqliteStore {
184+
t.Helper()
185+
178186
sqlDB, err := NewSqliteStore(&SqliteConfig{
179-
DatabaseFileName: dbFileName,
187+
DatabaseFileName: dbPath,
180188
SkipMigrations: false,
181189
})
182190
require.NoError(t, err)

tapdb/sqlutils_test.go

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -221,11 +221,8 @@ func (d *DbHandler) AddRandomServerAddrs(t *testing.T,
221221
return addrs
222222
}
223223

224-
// NewDbHandle creates a new store and query handle to the test database.
225-
func NewDbHandle(t *testing.T) *DbHandler {
226-
// Create a new test database.
227-
db := NewTestDB(t)
228-
224+
// newDbHandleFromDb creates a new database store handle given a database store.
225+
func newDbHandleFromDb(db *BaseDB) *DbHandler {
229226
testClock := clock.NewTestClock(time.Now())
230227

231228
// Gain a handle to the pending (minting) universe federation store.
@@ -268,3 +265,17 @@ func NewDbHandle(t *testing.T) *DbHandler {
268265
DirectQuery: db,
269266
}
270267
}
268+
269+
// NewDbHandleFromPath creates a new database store handle given a database file
270+
// path.
271+
func NewDbHandleFromPath(t *testing.T, dbPath string) *DbHandler {
272+
db := NewTestDbHandleFromPath(t, dbPath)
273+
return newDbHandleFromDb(db.BaseDB)
274+
}
275+
276+
// NewDbHandle creates a new database store handle.
277+
func NewDbHandle(t *testing.T) *DbHandler {
278+
// Create a new test database with the default database file path.
279+
db := NewTestDB(t)
280+
return newDbHandleFromDb(db.BaseDB)
281+
}

tapdb/test_postgres.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ func NewTestDB(t *testing.T) *PostgresStore {
1111
return NewTestPostgresDB(t)
1212
}
1313

14+
// NewTestDbHandleFromPath is a helper function that creates a new handle to an
15+
// existing SQLite database for testing.
16+
func NewTestDbHandleFromPath(t *testing.T, dbPath string) *PostgresStore {
17+
return NewTestPostgresDB(t)
18+
}
19+
1420
// NewTestDBWithVersion is a helper function that creates a Postgres database
1521
// for testing and migrates it to the given version.
1622
func NewTestDBWithVersion(t *testing.T, version uint) *PostgresStore {

tapdb/test_sqlite.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ func NewTestDB(t *testing.T) *SqliteStore {
1111
return NewTestSqliteDB(t)
1212
}
1313

14+
// NewTestDbHandleFromPath is a helper function that creates a new handle to an
15+
// existing SQLite database for testing.
16+
func NewTestDbHandleFromPath(t *testing.T, dbPath string) *SqliteStore {
17+
return NewTestSqliteDbHandleFromPath(t, dbPath)
18+
}
19+
1420
// NewTestDBWithVersion is a helper function that creates an SQLite database for
1521
// testing and migrates it to the given version.
1622
func NewTestDBWithVersion(t *testing.T, version uint) *SqliteStore {

0 commit comments

Comments
 (0)