Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commita9f607a

Browse files
authored
test: add an ergonomic way to access the test database for debugging (#19371)
Accessing the database during debugging currently requires eitherspinning up a separate PostgreSQL instance or inspecting memory toretrieve the DSN—both of which add unnecessary friction. While the testsuite already provisions a database by default, connecting to it formanual inspection or debugging is not straightforward.This change introduces a clearer and more accessible way to surface theDSN during debugging sessions, allowing developers to connect to thetest database directly without relying on external infrastructure or adhoc methods.Expected Usage:1. Debug using dlv or the IDE.2. Step through line by line and determine that a query isn't doing whatyou'd expect3. No further insight to be gained at the Go level4. The next place to test is to connect directly to the database whileit is in the exact state that the test has produced just before runningthe query5. Rerun the test with this option enabled and your breakpoint set rightbefore the questionable query runs6. Connect to the database and inspect or troubleshoot as you need to
1 parentd7bdb3c commita9f607a

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

‎coderd/database/dbtestutil/postgres.go‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ func initDefaultConnection(t TBSubset) error {
138138

139139
typeOpenOptionsstruct {
140140
DBFrom*string
141+
LogDSNbool
141142
}
142143

143144
typeOpenOptionfunc(*OpenOptions)
@@ -150,9 +151,18 @@ func WithDBFrom(dbFrom string) OpenOption {
150151
}
151152
}
152153

154+
// WithLogDSN sets whether the DSN should be logged during testing.
155+
// This provides an ergonomic way to connect to test databases during debugging.
156+
funcWithLogDSN(logDSNbool)OpenOption {
157+
returnfunc(o*OpenOptions) {
158+
o.LogDSN=logDSN
159+
}
160+
}
161+
153162
// TBSubset is a subset of the testing.TB interface.
154163
// It allows to use dbtestutil.Open outside of tests.
155164
typeTBSubsetinterface {
165+
Name()string
156166
Cleanup(func())
157167
Helper()
158168
Logf(formatstring,args...any)
@@ -227,6 +237,11 @@ func Open(t TBSubset, opts ...OpenOption) (string, error) {
227237
Port:port,
228238
DBName:dbName,
229239
}.DSN()
240+
241+
// Optionally log the DSN to help connect to the test database.
242+
ifopenOptions.LogDSN {
243+
_,_=fmt.Fprintf(os.Stderr,"Connect to the database for %s using: psql '%s'\n",t.Name(),dsn)
244+
}
230245
returndsn,nil
231246
}
232247

‎coderd/database/gen/dump/main.go‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ type mockTB struct {
1919
cleanup []func()
2020
}
2121

22+
func (*mockTB)Name()string {
23+
return"mockTB"
24+
}
25+
2226
func (t*mockTB)Cleanup(ffunc()) {
2327
t.cleanup=append(t.cleanup,f)
2428
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp