This course has been updated! We now recommend you take the AWS for Front-End Engineers (ft. S3, Cloudfront & Route 53) course.
Transcript from the "Exercise 3 Solution Part 1" Lesson
[00:00:00]
>> [MUSIC]
[00:00:04]
>> Kevin Whinnery: It was a lot of code to bite off but, I think it hopefully gave you a sense of the end process of migrating a database in this way. So it was about half and half. So what I think we'll do is actually live code this solution together, so we understand how to like all the pieces of code that are necessary to make this make this change happen.
[00:00:26] So the first the first bit that you're gonna want to do like if I'm going to want to update my data model. the first thing I want to do is generate a a migration and that that piece of That piece of code there that documentation that I put in the chat kind of takes you through how that works using SQL.
[00:00:47] So I'm gonna use the sequelize command line utility and then it has a it has an option called migration:create.
>> Kevin Whinnery: And that's going to create a new migration file for me in my project, so close some of this stuff down.
>> Kevin Whinnery: All right, so again when we have a migration like this.
[00:01:17] A migration is gonna consist of two operations, the up and the down operation. So when I am migrating my database up, when I am modifying it from it's previous state, all of the logic to do that is gonna be within the up function. And if for some reason I need to roll that database change back.
[00:01:37] All the logic to do that is gonna be in my down function. And one of the mistakes I saw people making was trying to recreate the table for a user within the up function adding in that boolean field that we had before. So totally understandable why you'd think so.
[00:01:57] Like you'd wanna update the definition of this model. But with a database you probably already have a database full of like hundreds of thousands of todos or whatever, and you can't recreate that database table or you'll lose all the data. So what you wanna do is modify it in place.
[00:02:15] And that is the role of code in a migration. So in the up function I wanna add a column to my existing database. So the way that I do that and this is again in the documentation that I put in the chat is using a query interface.addColumn. And this takes a couple of arguments.
[00:02:41] The first is going to be the name of an existing table. Which I believe is Todos. I can double check that actually in my model on a file. To do, actually might be called. So that's gonna be the name of my table.
>> Kevin Whinnery: And then it's going to be the name of the attribute that I want to add, and in this case I'll call it completed.
[00:03:15]
>> Kevin Whinnery: And then finally I need the data type that I want it to be in. In this case, I'm gonna make it a boolean and so I'll say a sequelize is boolean type. So I'm gonna add, add a column to do table called completed and it's gonna be of type of boolean and then in the down.
[00:03:36] I'm gonna do something very,very similar. So rather than add column. I'm going to remove column.
>> Kevin Whinnery: And the two parameters are gonna pass and again are the name of the table, and then the actual just the name of the of the property owner move, so we had to do you.
[00:04:05] And then [INAUDIBLE]. All right, so now I've got my migration logic in a place where I feel like I want it. Now what I want to do is you know that time stamp that gets generated for the migration is important because it lets us see sequalize. No way which migrations have actually been applied because each migration is ordered chronologically.
[00:04:31] So that I don't want to touch but the second part is just for humans to make it more readable. So I'm gonna call this migration add-competed-column or something like that just.
>> Speaker 2: I had to do's for the table name instead of todo.
>> Kevin Whinnery: It is to do's?
>> Speaker 3: If you look at the other migration it has an S on it.
[00:04:53]
>> Kevin Whinnery: Okay. So I'm sure I would have figured that out when my code didn't run, but I appreciate the heads up.
>> Speaker 2: So can you explain again why would the down and the column, wouldn't it remove a column?
>> Kevin Whinnery: I'm sorry that was another typo. It was a copy paste error on my part.
[00:05:08] Thank you guys, appreciate it. You are helping me debug left, right, and center. So yeah, the up is gonna add the column and the down is going to remove, remove the column. So that is my bad. That is definitely the danger of doing it live. So my migration is created.
[00:05:24] I've given it a human readable label. And then I can head out to the command line again. And I'm gonna run sequalize db:migrate.
>> Speaker 2: Do you want to be allowing null as a value for the completed attribute here, or does it not matter in this instance?
>> Kevin Whinnery: So that's a question you have to answer based on your application logics.
[00:05:56] So if it is very important that this value is not null and you want to have that constraint in the database. In your migration, you'll also have to update all of the other rows in the database to have either a true or false value for that. So you'd have to code that into the migration.
[00:06:13] In this case, we are going to allow a null, and we're not gonna mutate the rest of the table. But you can kinda decide based on the needs of the your application. That's definitely something you have to think through. Right, so I've run my migration. So now if I check my database and I this is another little tool that I use a lot when I'm using postgre locally it's called Postico I don't know what exactly, it's supposed to be called.
[00:06:45] But it's a nice Mac GUI over my database and it lets me sort of take a look at my at the current status of my database. And I think actually my database configuration might still be. Yeah, it's actually set up to go to this other thing. So I'm just gonna delete that.
[00:07:10] Cuz I don't care about that. And I'll run my migration again. This will actually target my local database.
>> Kevin Whinnery: And I'll hit refresh and there, now I have my completed column, but it's null because I didn't do any mutation of the existing data. So now my database is in a good state.
[00:07:32] Now, I need to update my model to reflect this new value.
>> Speaker 4: I missed the command for the update them database what was that again?
>> Kevin Whinnery: It was sequelize db:migrate it was the same command that you ran, to run the first migration initially but now it's gonna pick up that there's a new migration that it needs to run against the database.
[00:07:53]
>> Speaker 3: Or npm run migrate.
>> Kevin Whinnery: Exactly, we added that feature earlier today good point. So we run the migrations and now we have this new column. Now we need to update our model declaration in JavaScript to be aware of this new property to make this property persistent. And so add a completed attribute to the model which again is gonna be of type, BOOLEAN