From a9f607afd85a761f7ac82efb06d98003a52c4885 Mon Sep 17 00:00:00 2001 From: Sas Swart Date: Fri, 15 Aug 2025 11:25:20 +0200 Subject: [PATCH] test: add an ergonomic way to access the test database for debugging (#19371) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Accessing the database during debugging currently requires either spinning up a separate PostgreSQL instance or inspecting memory to retrieve the DSN—both of which add unnecessary friction. While the test suite already provisions a database by default, connecting to it for manual inspection or debugging is not straightforward. This change introduces a clearer and more accessible way to surface the DSN during debugging sessions, allowing developers to connect to the test database directly without relying on external infrastructure or ad hoc methods. Expected Usage: 1. Debug using dlv or the IDE. 2. Step through line by line and determine that a query isn't doing what you'd expect 3. No further insight to be gained at the Go level 4. The next place to test is to connect directly to the database while it is in the exact state that the test has produced just before running the query 5. Rerun the test with this option enabled and your breakpoint set right before the questionable query runs 6. Connect to the database and inspect or troubleshoot as you need to --- coderd/database/dbtestutil/postgres.go | 15 +++++++++++++++ coderd/database/gen/dump/main.go | 4 ++++ 2 files changed, 19 insertions(+) diff --git a/coderd/database/dbtestutil/postgres.go b/coderd/database/dbtestutil/postgres.go index e5aa4b14de..1ab80569de 100644 --- a/coderd/database/dbtestutil/postgres.go +++ b/coderd/database/dbtestutil/postgres.go @@ -138,6 +138,7 @@ func initDefaultConnection(t TBSubset) error { type OpenOptions struct { DBFrom *string + LogDSN bool } type OpenOption func(*OpenOptions) @@ -150,9 +151,18 @@ func WithDBFrom(dbFrom string) OpenOption { } } +// WithLogDSN sets whether the DSN should be logged during testing. +// This provides an ergonomic way to connect to test databases during debugging. +func WithLogDSN(logDSN bool) OpenOption { + return func(o *OpenOptions) { + o.LogDSN = logDSN + } +} + // TBSubset is a subset of the testing.TB interface. // It allows to use dbtestutil.Open outside of tests. type TBSubset interface { + Name() string Cleanup(func()) Helper() Logf(format string, args ...any) @@ -227,6 +237,11 @@ func Open(t TBSubset, opts ...OpenOption) (string, error) { Port: port, DBName: dbName, }.DSN() + + // Optionally log the DSN to help connect to the test database. + if openOptions.LogDSN { + _, _ = fmt.Fprintf(os.Stderr, "Connect to the database for %s using: psql '%s'\n", t.Name(), dsn) + } return dsn, nil } diff --git a/coderd/database/gen/dump/main.go b/coderd/database/gen/dump/main.go index f99b69bdae..1d84339eec 100644 --- a/coderd/database/gen/dump/main.go +++ b/coderd/database/gen/dump/main.go @@ -19,6 +19,10 @@ type mockTB struct { cleanup []func() } +func (*mockTB) Name() string { + return "mockTB" +} + func (t *mockTB) Cleanup(f func()) { t.cleanup = append(t.cleanup, f) }