Show HN: Pgtestdb, fast Postgres dbs for Golang tests (github.com)

4 points by peterldowns ↗ HN
Hi HN, I'd like to get your feedback on pgtestdb, a new library for helping you write more useful database-backed tests.

Many applications rely heavily on their database — and why shouldn't they! But writing tests that use the database is always a bit awkward, or in the worst case, very slow. Sometimes teams even mock their database out for an in-memory one, which works but only as long as you're not relying on specific features of your database.

Postgres is a fantastic database, and at my last job we relied heavily on its views, functions, data types, etc. A lot of our application logic was tied up with it. But writing tests against it was slow. Naively, each test would have to:

- Create a new database

- Run our migrations on it

- Then run the test

- Then tear down the database

pgtestdb makes this all a lot faster with two major strategies:

- Use a ram-backed or tmpfs-backed server that drops all data consistency guarantees. This isn't an issue for tests at all, and makes them much faster.

- Use template databases to only ever have to run the migrations once, when they change, and then fork that template each time a test asks for a new database.

Check out the docs on the Github page for more information but I hope you find this useful. I've written adaptors for most common migration frameworks so hopefully you can try it out without much issue.

Sincerely, Peter

1 comment

[ 3.4 ms ] story [ 5.7 ms ] thread
I should add that while this only works with Postgres, there's no reason this only has to work with Golang. I'm planning on releasing implementations for Typescript and Python over the next few months. If anyone would like to help out, send me an email (in my bio) or send a PR/note on Github.