Lesson Description

The "Creating a Tokens Table" Lesson is part of the full, Complete Go for Professional Developers course featured in this preview video. Here's what you'd learn in this lesson:

Melkey writes a Goose migration for the tokens table. The table includes columns for the hash, user_id, expiry, and scope. The migration is run from the terminal using the Goose CLI.

Preview
Close

Transcript from the "Creating a Tokens Table" Lesson

[00:00:00]
>> Melkey: Let's go into the migrations here. Oops, that's neither of them. And here we're gonna create our new table. So we're gonna do 1234, 4, I'm just gonna call it tokens.sql, like so, okay? Just to make it faster, go ahead and just copy any of the other existing SQLs as a starting point for us.

[00:00:21]
And so we're gonna create table if not exist instead of users, and you can call it tokens. We don't need any of these fields at all, so just delete those like that. The first field is gonna be a hash, like this. It's gonna be byte a format, so this is native to Postgres.

[00:00:41]
Okay, this is gonna be acting as our primary key.
>> Melkey: Okay, next, we're gonna have the user_id. The big int is going to be NOT NULL, and it's going to be REFERENCES, users on the users(id), field and ON DELETE CASCADE. so if we delete a user, we're going to delete all the tokens associated with that user.

[00:01:16]
>> Melkey: Every token has an expiry that we can control.
>> Melkey: So we can put that as a default timestamp, like so. And then with time zone not null. And then we have scope. And this is gonna be text. And this can also not be null. So, not null.

[00:01:37]
Make sure you modify the drop table. So it's no longer just users, but it's tokens. With that, we can actually use goose migration. All right, so we have this new table, but if we go to our psql/4/dt, it's not there. So how can we get it up, right?

[00:01:55]
I don't want to delete the database. Is there any way to just insert it? Yes, we can use goose. So at the root of your project, you can go ahead and run Goose, and put the command here. So this is gonna be the command. Now, it looks pretty nasty.

[00:02:09]
But what's actually happening is you're commanding Goose, where the directory is that has a SQL file, and has all the versions of Goose up so we've run before, and that's in migrations. We're telling it what is the database, so we're using Postgres, and then that is simply the connection string, okay?

[00:02:28]
So it's Postgres, we're using Postgres, it's gonna be the user Postgres, where the local host is, what the database name is, and what the port is. So very similarly, when we were in database.go, and even our test db, we have the connection string, this is just a different way to represent that connection string.

[00:02:46]
Instead of having it nicely separated, we have concatenated into one core connection string for our database. And again, watch the 5432, that's my port. If it's not your port, change that accordingly. And also make sure your database is, in fact, running. So you do still have docker compose up.

[00:03:08]
>> Melkey: So if you click enter, hopefully you should see an OK with this, okay, it notifies that the fourth SQL file in the migrations folder. And it said, successfully migrate database to version 4.
>> Speaker 2: We have the same, ZSH command not found. Take an export again.
>> Melkey: For goose?

[00:03:30]
>> Speaker 2: [CROSSTALK]
>> Melkey: Yeah, okay so what this is like a temporary solution export path you can actually just persist that into your .zshrc,
>> Speaker 3: Yeah, so if you really just type that into your-
>> Melkey: Instead of .zshrc, right?
>> Speaker 3: Yeah.
>> Melkey: Yeah, or .bashrc, whatever you're using, cuz I think this just keeps it alive for the state of the terminal, for the session state.

[00:03:55]
And then when you go open up a new terminal or fail to reload it and resource it, I think it just goes away. Wait, now, if it's in the zsh, it's always gonna be referred to it. And this is also helpful, yeah, I do actually recommend putting it in your .zsh permanently, because this is just a Go bin, right?

[00:04:10]
So everything you install with Go will be in this directory, so then any Go in talk moving forward will be always referred to through .zsh to this path. Nice, that worked. Yep, okay.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now