Skip to content

test: add an ergonomic way to access the test database for debugging #19371

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Aug 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions coderd/database/dbtestutil/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ func initDefaultConnection(t TBSubset) error {

type OpenOptions struct {
DBFrom *string
LogDSN bool
}

type OpenOption func(*OpenOptions)
Expand All @@ -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)
Expand Down Expand Up @@ -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
}

Expand Down
4 changes: 4 additions & 0 deletions coderd/database/gen/dump/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy