@@ -40,14 +40,6 @@ func (p ConnectionParams) DSN() string {
40
40
return fmt .Sprintf ("postgres://%s:%s@%s:%s/%s?sslmode=disable" , p .Username , p .Password , p .Host , p .Port , p .DBName )
41
41
}
42
42
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
-
51
43
// These variables are global because all tests share them.
52
44
var (
53
45
connectionParamsInitOnce sync.Once
@@ -146,6 +138,7 @@ func initDefaultConnection(t TBSubset) error {
146
138
147
139
type OpenOptions struct {
148
140
DBFrom * string
141
+ LogDSN bool
149
142
}
150
143
151
144
type OpenOption func (* OpenOptions )
@@ -158,6 +151,16 @@ func WithDBFrom(dbFrom string) OpenOption {
158
151
}
159
152
}
160
153
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
+
161
164
// TBSubset is a subset of the testing.TB interface.
162
165
// It allows to use dbtestutil.Open outside of tests.
163
166
type TBSubset interface {
@@ -237,9 +240,8 @@ func Open(t TBSubset, opts ...OpenOption) (string, error) {
237
240
}.DSN ()
238
241
239
242
// 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 )
243
245
}
244
246
return dsn , nil
245
247
}
0 commit comments