From 495222cf22e0fb20eb9114c9577d6c1ef9689416 Mon Sep 17 00:00:00 2001 From: Sas Swart Date: Fri, 15 Aug 2025 07:20:16 +0000 Subject: [PATCH 1/6] test: provide an ergonomic way to access the test database for debugging --- coderd/database/dbtestutil/postgres.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/coderd/database/dbtestutil/postgres.go b/coderd/database/dbtestutil/postgres.go index e5aa4b14de83b..3ac1fe172d018 100644 --- a/coderd/database/dbtestutil/postgres.go +++ b/coderd/database/dbtestutil/postgres.go @@ -40,6 +40,10 @@ func (p ConnectionParams) DSN() string { return fmt.Sprintf("postgres://%s:%s@%s:%s/%s?sslmode=disable", p.Username, p.Password, p.Host, p.Port, p.DBName) } +func WillLogDSN() bool { + return os.Getenv("CODER_TEST_LOG_PG_DSN") != "" +} + // These variables are global because all tests share them. var ( connectionParamsInitOnce sync.Once @@ -227,6 +231,12 @@ 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 WillLogDSN() { + t.Logf("Postgres test DSN: %s", dsn) + _, _ = fmt.Fprintln(os.Stderr, "Postgres test DSN:", dsn) + } return dsn, nil } From 68f5f075ded78bd7dc915cccca0e92cdf0b82967 Mon Sep 17 00:00:00 2001 From: Sas Swart Date: Fri, 15 Aug 2025 07:24:03 +0000 Subject: [PATCH 2/6] comment --- coderd/database/dbtestutil/postgres.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/coderd/database/dbtestutil/postgres.go b/coderd/database/dbtestutil/postgres.go index 3ac1fe172d018..9828aa818f476 100644 --- a/coderd/database/dbtestutil/postgres.go +++ b/coderd/database/dbtestutil/postgres.go @@ -40,6 +40,10 @@ func (p ConnectionParams) DSN() string { return fmt.Sprintf("postgres://%s:%s@%s:%s/%s?sslmode=disable", p.Username, p.Password, p.Host, p.Port, p.DBName) } +// WillLogDSN returns true if the DSN should be shown during testing. +// Set the CODER_TEST_LOG_PG_DSN environment variable to show the DSN. +// This provides an ergonomic way to connect to test databases during +// debugging without the need to spin up postgres manually. func WillLogDSN() bool { return os.Getenv("CODER_TEST_LOG_PG_DSN") != "" } From 5cee04598d42aaf955e16232b208a55d2f9ddead Mon Sep 17 00:00:00 2001 From: Sas Swart Date: Fri, 15 Aug 2025 08:40:29 +0000 Subject: [PATCH 3/6] use a functional option instead --- coderd/database/dbtestutil/postgres.go | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/coderd/database/dbtestutil/postgres.go b/coderd/database/dbtestutil/postgres.go index 9828aa818f476..5ea47b032f27d 100644 --- a/coderd/database/dbtestutil/postgres.go +++ b/coderd/database/dbtestutil/postgres.go @@ -40,14 +40,6 @@ func (p ConnectionParams) DSN() string { return fmt.Sprintf("postgres://%s:%s@%s:%s/%s?sslmode=disable", p.Username, p.Password, p.Host, p.Port, p.DBName) } -// WillLogDSN returns true if the DSN should be shown during testing. -// Set the CODER_TEST_LOG_PG_DSN environment variable to show the DSN. -// This provides an ergonomic way to connect to test databases during -// debugging without the need to spin up postgres manually. -func WillLogDSN() bool { - return os.Getenv("CODER_TEST_LOG_PG_DSN") != "" -} - // These variables are global because all tests share them. var ( connectionParamsInitOnce sync.Once @@ -146,6 +138,7 @@ func initDefaultConnection(t TBSubset) error { type OpenOptions struct { DBFrom *string + LogDSN bool } type OpenOption func(*OpenOptions) @@ -158,6 +151,16 @@ func WithDBFrom(dbFrom string) OpenOption { } } +// WithLogDSN sets whether the DSN should be logged during testing. +// Set the CODER_TEST_LOG_PG_DSN environment variable to show the DSN. +// This provides an ergonomic way to connect to test databases during +// debugging without the need to spin up postgres manually. +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 { @@ -237,9 +240,8 @@ func Open(t TBSubset, opts ...OpenOption) (string, error) { }.DSN() // Optionally log the DSN to help connect to the test database. - if WillLogDSN() { - t.Logf("Postgres test DSN: %s", dsn) - _, _ = fmt.Fprintln(os.Stderr, "Postgres test DSN:", dsn) + if openOptions.LogDSN { + _, _ = fmt.Fprintf(os.Stderr, "Connect to this test database using: psql '%s'\n", dsn) } return dsn, nil } From 0a3190e21e254fa95aa01909dee84dc8bb84e5d3 Mon Sep 17 00:00:00 2001 From: Sas Swart Date: Fri, 15 Aug 2025 09:00:30 +0000 Subject: [PATCH 4/6] update comment --- coderd/database/dbtestutil/postgres.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/coderd/database/dbtestutil/postgres.go b/coderd/database/dbtestutil/postgres.go index 5ea47b032f27d..412851a36cee6 100644 --- a/coderd/database/dbtestutil/postgres.go +++ b/coderd/database/dbtestutil/postgres.go @@ -152,9 +152,7 @@ func WithDBFrom(dbFrom string) OpenOption { } // WithLogDSN sets whether the DSN should be logged during testing. -// Set the CODER_TEST_LOG_PG_DSN environment variable to show the DSN. -// This provides an ergonomic way to connect to test databases during -// debugging without the need to spin up postgres manually. +// This provides an ergonomic way to connect to test databases during debugging. func WithLogDSN(logDSN bool) OpenOption { return func(o *OpenOptions) { o.LogDSN = logDSN From 6276b00536d6c69a92a1fed684d7dff05fa15266 Mon Sep 17 00:00:00 2001 From: Sas Swart Date: Fri, 15 Aug 2025 09:03:07 +0000 Subject: [PATCH 5/6] update DSN logging message --- coderd/database/dbtestutil/postgres.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/coderd/database/dbtestutil/postgres.go b/coderd/database/dbtestutil/postgres.go index 412851a36cee6..1ab80569dedb1 100644 --- a/coderd/database/dbtestutil/postgres.go +++ b/coderd/database/dbtestutil/postgres.go @@ -162,6 +162,7 @@ func WithLogDSN(logDSN bool) OpenOption { // 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) @@ -239,7 +240,7 @@ func Open(t TBSubset, opts ...OpenOption) (string, error) { // Optionally log the DSN to help connect to the test database. if openOptions.LogDSN { - _, _ = fmt.Fprintf(os.Stderr, "Connect to this test database using: psql '%s'\n", dsn) + _, _ = fmt.Fprintf(os.Stderr, "Connect to the database for %s using: psql '%s'\n", t.Name(), dsn) } return dsn, nil } From ed07826f1827e793724b58d3ca7ce7607565baf8 Mon Sep 17 00:00:00 2001 From: Sas Swart Date: Fri, 15 Aug 2025 09:11:26 +0000 Subject: [PATCH 6/6] lint --- coderd/database/gen/dump/main.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/coderd/database/gen/dump/main.go b/coderd/database/gen/dump/main.go index f99b69bdaef93..1d84339eecce9 100644 --- a/coderd/database/gen/dump/main.go +++ b/coderd/database/gen/dump/main.go @@ -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) } 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