Skip to content

Commit 60b1005

Browse files
committed
use separate initializer
1 parent 866d3d9 commit 60b1005

File tree

1 file changed

+16
-4
lines changed
  • coderd/database/dbtestutil

1 file changed

+16
-4
lines changed

coderd/database/dbtestutil/db.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,22 +50,34 @@ func WithDumpOnFailure() Option {
5050
}
5151
}
5252

53-
func WithReturnSQLDB(f func(*sql.DB)) Option {
53+
func withReturnSQLDB(f func(*sql.DB)) Option {
5454
return func(o *options) {
5555
o.returnSQLDB = f
5656
}
5757
}
5858

59+
func NewDBWithSQLDB(t testing.TB, opts ...Option) (database.Store, pubsub.Pubsub, *sql.DB) {
60+
t.Helper()
61+
62+
if !WillUsePostgres() {
63+
t.Fatalf("cannot use WithReturnSQLDB without PostgreSQL, consider adding `if !dbtestutil.WillUsePostgres() { t.Skip() }` to this test")
64+
}
65+
66+
var sqlDB *sql.DB
67+
opts = append(opts, withReturnSQLDB(func(db *sql.DB) {
68+
sqlDB = db
69+
}))
70+
db, ps := NewDB(t, opts...)
71+
return db, ps, sqlDB
72+
}
73+
5974
func NewDB(t testing.TB, opts ...Option) (database.Store, pubsub.Pubsub) {
6075
t.Helper()
6176

6277
var o options
6378
for _, opt := range opts {
6479
opt(&o)
6580
}
66-
if o.returnSQLDB && !WillUsePostgres() {
67-
t.Fatalf("cannot use WithReturnSQLDB without PostgreSQL, consider adding `if !dbtestutil.WillUsePostgres() { t.Skip() }` to this test")
68-
}
6981

7082
db := dbfake.New()
7183
ps := pubsub.NewInMemory()

0 commit comments

Comments
 (0)