# README
Wishlist backend
This is a simple Go backend I created with the purpose of deepning my knowledge of Go.
Tools used
- Docker - Containerization for local development
- PostgreSQL - Main database used
- pgx - Go driver for PostgreSQL
- sqlc - SQL Code generation
- migrate - Database migrations
- godotenv - .env file management
Set up
Environment variables
The file .env.example
contains all of the required environment variables used to be able to run this project.
Copy the file .env.example
to a file .env
. And fill them out as necessary.
cp .env.example .env
Running (locally)
Start the local database by running docker-compose
.
docker-compose up -d
When the database is up and running, start the application by running the following Make target.
make run
Your server should now be up and running!
Migrations
When running migrations, make sure you have started the local database use docker-compose
, otherwise the following commands will fail.
Creating new migration
make create-migration name='create_account_table'
Fill in the generated migration files with the required SQL to perform and undo an migration, in the files [...].up.sql
, and [...].down.sql
respectfully.
Migrating your database
Migrating the database is either done when running the application, or by running the following Make target.
# Run all migrations
make migrate
# Run the next 2 migrations
make migrate n=2
Reverting a migration
Reverting a migration a migration is simple, you can either revert all or the last n
migrations.
# Run all the down migrations
make migrate-down
# Run the last 2 down migrations
make migrate-down n=2
Going to a specific version
You can go to a specific migration version, applying the required up and down migrations, with the following command.
make migrate-goto version=1
Queries
This project uses sqlc to generate Go code bindings from raw SQL queries. To create SQL queries, add the desired SQL to file in ./internal/db/queries/
. Prefix the filename with the table that you are operating on.
Each query should have a comment which specifies the desired Go function name of query, and if it return one or multiple rows. E.g.
-- name: GetAccount :one
SELECT * FROM account
WHERE id = $1 LIMIT 1;
For full documentation regarding sqlc, click here.
Generating the code binding is done by running the following command.
sqlc generate
Linting queries
NOTE: Linting queries require the DB to be running and at the latest migration
You can lint the queries you create by running the following SQLC command.
sqlc vet