Skip to content

test: add an ergonomic way to access the test database for debugging#19371

Merged
SasSwart merged 6 commits into
mainfrom
jjs/test-db-dsn-log
Aug 15, 2025
Merged

test: add an ergonomic way to access the test database for debugging#19371
SasSwart merged 6 commits into
mainfrom
jjs/test-db-dsn-log

Conversation

@SasSwart
Copy link
Copy Markdown
Contributor

@SasSwart SasSwart commented Aug 15, 2025

Accessing the database during debugging currently requires either spinning up a separate PostgreSQL instance or inspecting memory to retrieve the DSN—both of which add unnecessary friction. While the test suite already provisions a database by default, connecting to it for manual inspection or debugging is not straightforward.

This change introduces a clearer and more accessible way to surface the DSN during debugging sessions, allowing developers to connect to the test database directly without relying on external infrastructure or ad hoc methods.

Expected Usage:

  1. Debug using dlv or the IDE.
  2. Step through line by line and determine that a query isn't doing what you'd expect
  3. No further insight to be gained at the Go level
  4. The next place to test is to connect directly to the database while it is in the exact state that the test has produced just before running the query
  5. Rerun the test with this option enabled and your breakpoint set right before the questionable query runs
  6. Connect to the database and inspect or troubleshoot as you need to

@SasSwart SasSwart requested review from hugodutka and mafredri August 15, 2025 07:26
@SasSwart SasSwart marked this pull request as ready for review August 15, 2025 07:29
Copy link
Copy Markdown
Member

@mafredri mafredri left a comment

Choose a reason for hiding this comment

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

I'd like to better understand how this is to be used. For instance, if you have a quick test I think it's unlikely you'll have time to connect to the database, right?

Why do you need to connect to the live database? There's also the dump database on failure option that can be enabled, which gives you the state of the database when it failed. That isn't sufficient for your use-case?

Is the purpose explicitly to allow outputting the DSN without modifying the test (i.e. not needing to add the option to output it and/or dump the db)?

Comment thread coderd/database/dbtestutil/postgres.go Outdated
Comment thread coderd/database/dbtestutil/postgres.go
Comment thread coderd/database/dbtestutil/postgres.go Outdated
Copy link
Copy Markdown
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.

What's the expected flow here? Let's say you have an existing test that fails. I would imagine it would make sense to log the DSN and then wait for the user to continue once they've successfully connected.

@SasSwart
Copy link
Copy Markdown
Contributor Author

Expected flow:

  1. Debug using dlv or the IDE.
  2. Step through line by line and determine that a query isn't doing what you'd expect
  3. No further insight to be gained at the Go level
  4. The next place to test is to connect directly to the database while it is in the exact state that the test has produced just before running the query
  5. Rerun the test with this option enabled and your breakpoint set right before the questionable query runs
  6. Connect to the database and inspect or troubleshoot as you need to

Dumping the database on test failure is useful, but still requires more friction than this in my opinion.

@SasSwart SasSwart requested review from johnstcn and mafredri August 15, 2025 08:37
@mafredri
Copy link
Copy Markdown
Member

Thanks @SasSwart, I think using dlv was the context I was missing, makes much more sense now 👍🏻.

(PS. I think your commit didn't get pushed, or GitHub hasn't updated yet ¯\_(ツ)_/¯)

Copy link
Copy Markdown
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.

Nice 👍

Comment thread coderd/database/dbtestutil/postgres.go Outdated
Comment thread coderd/database/dbtestutil/postgres.go Outdated
Copy link
Copy Markdown
Member

@mafredri mafredri left a comment

Choose a reason for hiding this comment

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

Thanks, great addition 👍🏻

@SasSwart SasSwart merged commit a9f607a into main Aug 15, 2025
26 checks passed
@SasSwart SasSwart deleted the jjs/test-db-dsn-log branch August 15, 2025 09:25
@github-actions github-actions Bot locked and limited conversation to collaborators Aug 15, 2025
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