Skip to content

Commit 495222c

Browse files
committed
test: provide an ergonomic way to access the test database for debugging
1 parent d7bdb3c commit 495222c

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

coderd/database/dbtestutil/postgres.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ func (p ConnectionParams) DSN() string {
4040
return fmt.Sprintf("postgres://%s:%s@%s:%s/%s?sslmode=disable", p.Username, p.Password, p.Host, p.Port, p.DBName)
4141
}
4242

43+
func WillLogDSN() bool {
44+
return os.Getenv("CODER_TEST_LOG_PG_DSN") != ""
45+
}
46+
4347
// These variables are global because all tests share them.
4448
var (
4549
connectionParamsInitOnce sync.Once
@@ -227,6 +231,12 @@ func Open(t TBSubset, opts ...OpenOption) (string, error) {
227231
Port: port,
228232
DBName: dbName,
229233
}.DSN()
234+
235+
// Optionally log the DSN to help connect to the test database.
236+
if WillLogDSN() {
237+
t.Logf("Postgres test DSN: %s", dsn)
238+
_, _ = fmt.Fprintln(os.Stderr, "Postgres test DSN:", dsn)
239+
}
230240
return dsn, nil
231241
}
232242

0 commit comments

Comments
 (0)