Let's Go Further User activation › Setting up the tokens database table
Previous · Contents · Next
Chapter 14.1.

Setting up the tokens database table

Let’s begin by creating a new tokens table in our database to store the activation tokens for our users. If you’re following along, run the following command to create a new pair of migration files:

$ migrate create -seq -ext .sql -dir ./migrations create_tokens_table
/home/alex/Projects/greenlight/migrations/000005_create_tokens_table.up.sql
/home/alex/Projects/greenlight/migrations/000005_create_tokens_table.down.sql

And then add the following SQL statements to the ‘up’ and ‘down’ migration files respectively:

File: migrations/000005_create_tokens_table.up.sql
CREATE TABLE IF NOT EXISTS tokens (
    hash bytea PRIMARY KEY,
    user_id bigint NOT NULL REFERENCES users ON DELETE CASCADE,
    expiry timestamp(0) with time zone NOT NULL,
    scope text NOT NULL
);
File: migrations/000005_create_tokens_table.down.sql
DROP TABLE IF EXISTS tokens;

Let’s quickly step through the columns in this new tokens table and explain their purpose.

OK, with those explanations out of the way, you should be able to execute the ‘up’ migration with the following command:

$ migrate -path=./migrations -database=$GREENLIGHT_DB_DSN up
5/u create_tokens_table (21.568194ms)