Skip to content

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

Merged
merged 3 commits into from
Oct 12, 2023

Conversation

mafredri
Copy link
Member

@mafredri mafredri commented Oct 12, 2023

This change may be controversial, but on occasion I find it useful. I needed this whilst benchmarking a database change.

Sample usage:

db, pubsub, sqlDB := dbtestutil.NewDBWithSQLDB(b)
client := coderdtest.New(b, &coderdtest.Options{
	IncludeProvisionerDaemon: true,
	Database:                 db,
	Pubsub:                   pubsub,
})

_, _ = sqlDB.Exec("...")

@mafredri mafredri self-assigned this Oct 12, 2023
@mafredri mafredri requested review from johnstcn and mtojek October 12, 2023 10:08
Comment on lines +98 to +100
if o.returnSQLDB != nil {
o.returnSQLDB(sqlDB)
}
Copy link
Member

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.

Copy link
Member Author

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.

@@ -49,6 +50,12 @@ func WithDumpOnFailure() Option {
}
}

func WithReturnSQLDB(f func(*sql.DB)) Option {
Copy link
Member

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.

Copy link
Member Author

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.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mafredri mafredri force-pushed the mafredri/dbtestutil-expose-sqldb branch from 60b1005 to 9e9aed4 Compare October 12, 2023 10:20
@mafredri mafredri force-pushed the mafredri/dbtestutil-expose-sqldb branch from 9e9aed4 to ee86b3a Compare October 12, 2023 10:20
Comment on lines +67 to +69
opts = append(opts, withReturnSQLDB(func(db *sql.DB) {
sqlDB = db
}))
Copy link
Member

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

Copy link
Member

@johnstcn johnstcn left a 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.

Copy link
Member

@mtojek mtojek left a 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 👍

@mafredri mafredri changed the title test(coderd/database/dbtestutil): allow access to *sql.DB test(coderd/database/dbtestutil): allow access to *sql.DB Oct 12, 2023
@mafredri mafredri merged commit 782c22a into main Oct 12, 2023
@mafredri mafredri deleted the mafredri/dbtestutil-expose-sqldb branch October 12, 2023 12:07
@github-actions github-actions bot locked and limited conversation to collaborators Oct 12, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants