Service oriented golang template with relational database. The goal of this template is to provide an initial setup for DB migrations and code generation, using Docker, Postgres, and a task runner.
- Docker
- sqlc: Generate go code from
sql
queries - go-migrate: Manage DB migrations
- Task: Task Runner
- dbml2sql: Generate sql from dbml
- pgx: Postgres driver
This template does not include the go module file. To create a new one and add dependencies run:
go mod init <package>
go mod tidy
go get ./...
cmd/
: app entry pointsdb/
:migrations/
: SQL migrations filesqueries/
: SQL query files used bysqlc
pkg/
: app sourcesdb/
: code generated bysqlc
Docker compose can be used to setup a db instance. Use
docker-compose up -d
Note: migrations will run once the postgres container is ready using docker-compose depend-on
The schema.dbml file is the db schema source of truth.
It is used to generate the schema.sql
and initial migration.
DB driver is pgx
but it can be changed in the sqlc configuration.
List all available tasks
task -l
Note: a Makefile
is also provided for comparison with Taskfile
.
Run tests with
task test
# or specific folders
task test -- pkg/db
For integration testing consider using docker-compose or task db-up
to get a test db instance.
A github actions workflow is configured to run: go fmt, vet, test
.
The workflow also runs gosec.