Skip to content

Commit 5cee045

Browse files
committed
use a functional option instead
1 parent 68f5f07 commit 5cee045

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

coderd/database/dbtestutil/postgres.go

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,6 @@ 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-
// WillLogDSN returns true if the DSN should be shown during testing.
44-
// Set the CODER_TEST_LOG_PG_DSN environment variable to show the DSN.
45-
// This provides an ergonomic way to connect to test databases during
46-
// debugging without the need to spin up postgres manually.
47-
func WillLogDSN() bool {
48-
return os.Getenv("CODER_TEST_LOG_PG_DSN") != ""
49-
}
50-
5143
// These variables are global because all tests share them.
5244
var (
5345
connectionParamsInitOnce sync.Once
@@ -146,6 +138,7 @@ func initDefaultConnection(t TBSubset) error {
146138

147139
type OpenOptions struct {
148140
DBFrom *string
141+
LogDSN bool
149142
}
150143

151144
type OpenOption func(*OpenOptions)
@@ -158,6 +151,16 @@ func WithDBFrom(dbFrom string) OpenOption {
158151
}
159152
}
160153

154+
// WithLogDSN sets whether the DSN should be logged during testing.
155+
// Set the CODER_TEST_LOG_PG_DSN environment variable to show the DSN.
156+
// This provides an ergonomic way to connect to test databases during
157+
// debugging without the need to spin up postgres manually.
158+
func WithLogDSN(logDSN bool) OpenOption {
159+
return func(o *OpenOptions) {
160+
o.LogDSN = logDSN
161+
}
162+
}
163+
161164
// TBSubset is a subset of the testing.TB interface.
162165
// It allows to use dbtestutil.Open outside of tests.
163166
type TBSubset interface {
@@ -237,9 +240,8 @@ func Open(t TBSubset, opts ...OpenOption) (string, error) {
237240
}.DSN()
238241

239242
// Optionally log the DSN to help connect to the test database.
240-
if WillLogDSN() {
241-
t.Logf("Postgres test DSN: %s", dsn)
242-
_, _ = fmt.Fprintln(os.Stderr, "Postgres test DSN:", dsn)
243+
if openOptions.LogDSN {
244+
_, _ = fmt.Fprintf(os.Stderr, "Connect to this test database using: psql '%s'\n", dsn)
243245
}
244246
return dsn, nil
245247
}

0 commit comments

Comments
 (0)
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