

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Difficulty: 7
click for more info
Not enough gems
Cost: 6 gems
1: Postgres
incomplete
2: Goose Migrations
incomplete
3: SQLC
incomplete
4: Reset
incomplete
5: List Users
incomplete
This lesson's interactive features are locked, please to keep using them
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@latestRun 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.sqlFor 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 key
created_at: a TIMESTAMP that can not be null
updated_at: a TIMESTAMP that can not be null
name: a unique string that can not be null
The -- +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/databaseHere are examples:
macOS (no password, your username): postgres://wagslane:@localhost:5432/gator
Linux (password from last lesson, postgres user): postgres://postgres:postgres@localhost:5432/gator
Test 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' upRun your migration! Reconnect with psql using the same connection string that worked in step 3:
psql '<connection_string>'Then list the tables inside psql to find your newly created users table:
\dtprotocol://username:password@host:port/database?sslmode=disableYour application code needs to know to not try to use SSL locally.
Submit the CLI tests.