rss twitter gitlab github linkedin linkedin instagram
Go Tool: (exploring) sqlc (Part 1)
Dec 10, 2020

Last month I attended GopherCon 2020 (virtually, like everyone else 😄), and just like the first time I attended I was able to get answers to some of the problems I was trying to solve.

This time it was related to speeding up our development (and testing!) workflow when using PostgreSQL as a persistent datastore. For context, we have been successfully using database/sql and jmoiron/sqlx, and both can get the work done but I’m always trying to find better tools to improve what we have. I’ve been doing this investigation for a few months already, and I did find some packages and tools, but because of the amount of options I haven’t really had time to try them all. In the end what I’m looking for is tools/packages that:

  • Integrate nicely with PostgreSQL, and
  • Avoid too much magic (reflect-based ORMs are out of the picture).

With all of that being said… I got answers to those questions from Johan Brandhorst-Satzkorn, specifically the talk A Journey to Postgres Productivity with Go, during this talk Johan mentions the following 5 tools/packages:

  • jackc/pgx PostgreSQL driver and toolkit for Go

    This driver has been on my radar for months already, I actually used it for the blog series Implementing Complex Pipelines in Go, the cool thing about pgx is that it also implements the interface types defined in database/sql so replacing what we have (which uses lib/pq) should be, in theory, not that difficult.

  • golang-migrate/migrate Database migrations. CLI and Golang library.

    We have been using this package for a while already, simple yet powerful, it satisfies our needs.

  • kyleconroy/sqlc Generate type safe Go from SQL

    This is one of the tools I found during my investigation, it made a great first impression because it integrates nicely with PostgreSQL and allows you to interact with your schema via type-safe Go, however it does lack some dynamism we sometimes need.

  • Masterminds/squirrel Fluent SQL generation for golang

    Another package I found while exploring alternatives, it seemed nice but a tiny bit magic; but it does answer the question I had after reading about sqlc, this one provides the dynamism we need.

  • ory/dockertest Write better integration tests! Dockertest helps you boot up ephermal docker images for your Go tests with minimal work

    I believe I heard about this one previously but I never had the chance of using it before, however just recently I started adding it to our projects and it seems to work nicely. It does remove that extra boilerplate in GitlabCI for launching the services required for running the tests. More importantly it defines the actual service requirements in code, any team member can refer to the actual code to run locally a docker container if needed.

In the end the important thing about those 5 tools/packages is how they integrate and how improve (or not) the workflow we currently have; specifically about sqlc I found it really easy to integrate, no real concerns or complains so far. Making a decision about adding a new step to the process or adding a new tool always takes time so although I like what I’ve seen so far about sqlc I’m still exploring how it integrates with all the other needs we have, specifically instrumentation (via NewRelic) and error details, so far the current version still uses, by default, a combination of database/sql and lib/pq so there’s that as well.

Final thoughts

I will continue exploring sqlc as well as the other tools/packages mentioned above, another thing I’m also investigating is something similar to factory_bot for speeding up the default values required when testing PostgreSQL-related methods.

More to come.

Back to posts