Goose is a database migration tool written in Go. It runs migrations from a set of SQL files, making it a perfect fit for this project (we wanna stay close to the raw SQL).
A migration is just a set of changes to your database table. You can have as many migrations as needed as your requirements change over time. For example, one migration might create a new table, one might delete a column, and one might add 2 new columns.
An "up" migration moves the state of the database from its current schema to the schema that you want. So, to get a "blank" database to the state it needs to be ready to run your application, you run all the "up" migrations.
If something breaks, you can run one of the "down" migrations to revert the database to a previous state. "Down" migrations are also used if you need to reset a local testing database to a known state.
Goose is just a command line tool that happens to be written in Go. I recommend installing it using go install:
go install github.com/pressly/goose/v3/cmd/goose@latest
Run goose -version to make sure it's installed correctly.
A "migration" in Goose is just a .sql file with some SQL queries and some special comments. Our first migration should just create a users table. The simplest format for these files is:
number_name.sql
For example, I created a file in sql/schema called 001_users.sql with the following contents:
-- +goose Up
CREATE TABLE ...
-- +goose Down
DROP TABLE users;
Write out the CREATE TABLE statement in full, I left it blank for you to fill in. A user should have 4 fields:
id: a UUID that will serve as the primary keycreated_at: a TIMESTAMP that can not be nullupdated_at: a TIMESTAMP that can not be nullname: a unique string that can not be nullThe -- +goose Up and -- +goose Down comments are case sensitive and required. They tell Goose how to run the migration in each direction.
protocol://username:password@host:port/database
Here are examples:
postgres://wagslane:@localhost:5432/gatorpostgres://postgres:postgres@localhost:5432/gatorTest your connection string by running psql, for example:
psql "postgres://wagslane:@localhost:5432/gator"
It should connect you to the gator database directly. If it's working, great. exit out of psql and save the connection string.
cd into the sql/schema directory and run:
goose postgres <connection_string> up
# example:
# goose postgres "postgres://wagslane:@localhost:5432/gator" up
Run your migration! Make sure it works by using psql to find your newly created users table:
psql gator
\dt
protocol://username:password@host:port/database?sslmode=disable
Your application code needs to know to not try to use SSL locally.
Submit the CLI tests.