-
Notifications
You must be signed in to change notification settings - Fork 887
test(coderd/database/dbtestutil): allow access to *sql.DB
#10238
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
if o.returnSQLDB != nil { | ||
o.returnSQLDB(sqlDB) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What happens if dbtestutil.WillUsePostgres
is false? I could see this being a potential sharp edge. It would probably be a good idea to fail the test if this is defined and we're using the fakeDB.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch 👍🏻, I'll return an error.
coderd/database/dbtestutil/db.go
Outdated
@@ -49,6 +50,12 @@ func WithDumpOnFailure() Option { | |||
} | |||
} | |||
|
|||
func WithReturnSQLDB(f func(*sql.DB)) Option { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Non-blocker: Alternatively, we could extract a new function dbtestutil.NewSQLDB(t testing.TB, opts ...Option) (database.Store, database.Pubsub, *sql.DB)
? I feel that would be more immediately obvious and less annoying to use.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also a good idea, I'll do that instead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
60b1005
to
9e9aed4
Compare
9e9aed4
to
ee86b3a
Compare
opts = append(opts, withReturnSQLDB(func(db *sql.DB) { | ||
sqlDB = db | ||
})) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Gotta love functional options <3
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not opposed to this change. If you're already spinning up your own DB, you're likely doing something fairly specific.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As this is a helper/util, I don't think that it is harmful 👍
*sql.DB
This change may be controversial, but on occasion I find it useful. I needed this whilst benchmarking a database change.
Sample usage: