
Lesson Description
The "Running Goose Migrations" 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 runs the database migrations. When the Go server is restarted, Goose logs the database migrations, and the current database version is reflected. psql or any other database tool can connect to the Postgres database and verify the migrations.
Transcript from the "Running Goose Migrations" Lesson
[00:00:00]
>> Melkey: We are almost there. All we have to do is kind of route our application to then read the file structure here. That goose up command, we're gonna run it in our application first, and we are gonna run it in the database.go file. So if you go back to your database.go, again, ignore this little error here.
[00:00:18]
I'm gonna have to restart my LSB.
>> Melkey: Underneath our open function, we're gonna create a few more functions, or two more functions. The first one is gonna be called migrate. So we're going to do func migrate. This migrate function is going to take two arguments. So dg, so we're gonna pointer sequel.db.
[00:00:44]
>> Melkey: Okay, it's also gonna take a directory, which can be a string, and it's going to return an error.
>> Melkey: Like so.
>> Melkey: So this migrate function is gonna have a very simple purpose. It's gonna tell goose what database we're using, and it's also going to tell it exactly what we want to do, which is run goose up.
[00:01:08]
So here we can do error, is going to be goose.setdialect, like so. It should bring in the goose package for you, okay? I have to make sure mine is v3. We do actually have to bring it into a project, because we installed it to have the goose command, but we also to do a Go get on the command now.
[00:01:32]
So if you do go back to goose, instead of doing Go install, let's also run a Go get. Okay, cool. Now you can see it's imported as goose and not used. So now I know there's no import issue, it's just, the compiler is yelling at me, which I'm fine with.
[00:01:51]
So here we'll do goose.SelectDialect. And this is basically telling goose what we're using, which is going to be Postgres. Like so, we'll check if error does not equal to nil. We're going to return fmt.Errorf, okay? I'm gonna say issue with migrates. We're just gonna say, migrate,
>> Melkey: Modulo w, them and pass in the error.
[00:02:23]
Okay, next we're gonna do error. I'm going to equal this to goose.up. We'll do goose.up, I'll pass in the database and then the directory.
>> Melkey: If error does not equal to nil here, all we're gonna do is return fmt.Errorf. All right, I'm gonna say, goose up module W, pass in the error.
[00:02:56]
Like so, and then we'll just return nil. There's no errors at this point. Okay, now, the last part we gotta do is be fairly quick. We have to now bind in that FS, that file structure. So all we've done so far is tell goose what we're using and what command to run, which is goose.up.
[00:03:15]
The last part is to bind that migrations package we have. So here we'll just call this MigrateFS(), okay? It's going to take sql.db. It will take MigrationsFS(), which is going to be of type.fs, we can bring this into our library using IO/FS. At the top there in the import statements.
[00:03:38]
And I'm goinna bring in our directory string. And this is gonna return an error for us.
>> Melkey: Okay, so here we're gonna set a function. We're gonna set the base file structure, and then we're actually gonna call the Migrate function we wrote below. So here we'll do goose.SetBaseFS.
[00:04:02]
We'll put in the MigrationsFS, like so. And then we're gonna create a defer func, an anonymous function. We'll do defer, which we've already talked about, and then we'll create our anonymous function func, right? The body of our function here, which is gonna be just goose.Set Base back to nil.
[00:04:25]
This does not require any additional arguments, which is what these parentheses will mean, okay? And then, we'll return Migrate like so, I'm gonna pass in our DB and our directory. Cool, okay, so now we have MigrateFS. We have Migrate. The last part is to hook this all up into our app.go.
[00:04:47]
So if we go into app.go,
>> Melkey: Okay, the only part that I think would make sense to add this is is right above the logger. And here we can do, error is going to be store.MigrateFS, when it passed in our database, pgdb. We're also going to bring in our migrations package, so migrations.fs, that variable we declared.
[00:05:14]
So, .fs, okay? And then the directory is going to be just dot. So we're gonna be running this from the base of our directory. That's where we can find the migrations package for goose here.
>> Melkey: Now, if error does not equal to nil, we can just panic. Cuz if our database doesn't work, application definitely should not work as well.
[00:05:41]
>> Melkey: There's one more thing I want to do, and that's gonna be to run this. So we have all these migration files. We've set them all, and they look like they work. Now, if we go back, we should still have, let me see. Did I bring it down?
[00:05:54]
I did. So here, let's bring everything down. And what I want to do is, again, let's go docker compose build up.
>> Melkey: It's gonna run our workout DB container. And then here, if you run Go, run main.go. You should see we have a few things. Connect it to our database and we have these three okays, okay?
[00:06:20]
For users, workouts, and our entries. And you should see now, successfully migrated database to version three, because we have three SQL files, all with the goose statement begin and goose up command. One more thing that we can do is now we can actually use PSQL, right? So this is the thing, you can use any kind of DBUI that you'd like, but PSQL is the one I use.
[00:06:42]
Here's the command- usual be Postgres. The host is localhost, and the port is 54 through d2. So if I run this, it should ask me for the password for Postgres. That's just Postgres. If I run this, you can see, PSQL version 17.4, server 12.4. That's important because that's the image that we have.
[00:07:05]
And now if you do forward slash DT, you can see that we have our users table, our workout entries table, our workouts table, and the actual GooseDB version. So we've kind of completed the loop here of now having our database spun up, our back and connected to our database, and us actually inserting new SQL schemas into the database itself.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops