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

Commit5cee045

Browse files
committed
use a functional option instead
1 parent68f5f07 commit5cee045

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
returnfmt.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-
funcWillLogDSN()bool {
48-
returnos.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
typeOpenOptionsstruct {
148140
DBFrom*string
141+
LogDSNbool
149142
}
150143

151144
typeOpenOptionfunc(*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+
funcWithLogDSN(logDSNbool)OpenOption {
159+
returnfunc(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
typeTBSubsetinterface {
@@ -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-
ifWillLogDSN() {
241-
t.Logf("Postgres test DSN: %s",dsn)
242-
_,_=fmt.Fprintln(os.Stderr,"Postgres test DSN:",dsn)
243+
ifopenOptions.LogDSN {
244+
_,_=fmt.Fprintf(os.Stderr,"Connect to this test database using: psql '%s'\n",dsn)
243245
}
244246
returndsn,nil
245247
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp