[MUSIC] Database migrations provide a way to abstract away much of the complexity associated with the persistence layer in web applications. This allows you to build, and update web applications without having to spend too much time managing databases, writing SQL queries, and performing other database specific operations. Let's start by taking the ability performed database migrations. Migrations are important part of any Agile development process. So why do we need to migrate? Well if your a part of an Agile development team, then your following an Agile development process, which means your embracing the fact that you've got evolving requirements. And evolving requirements means that your code base is probably evolving. And when your code base is evolving, then your data models are changing, which means you're going to have to eventually change the database structure. Now, migrations for evolving databases work if you're trying to add or delete a column in the database, you also might need to do this if you want to add or delete a table from the database or if you need to add or delete a relationships. So, this might entail adding a join table to your database or adding a foreign key attribute to a given table. So if you're modifying the persistence layer and you've already got data stored in the backend database and you don't want to lose it, well, then you need to migrate the database. You would also need to migrate a database if you switch from one database management system to another, for instance if you switch from SQLite3 to Postgres. So what exactly is a migration? Well, it's the process of updating the schema, creating a new database from the updated schema, moving data from the old database to the new one, and then destroying the old database. Modern web application frameworks provide scripts for doing this. We've already seen that in the case of Rails. And we've already seen that it's in the ActiveRecord module that the scripts are provided. We know that Rails has code generators when you generate a scaffold or a model, that these create migration files and it stores them in the subdirectory db/migrate. You can also generate a migration file directly using the rails generate migration command. And this will generate a file that has this particular format. Notice that it has a year followed by month, followed by day, hours, minutes, seconds, prepending this file. That is, the filename is a UTC timestamp, determined by the time at which the file was created followed by an underscore and then the name of the migration itself. Now UTC stands for Coordinated Universal Time. It's the standard for time that is used around the world. Agencies responsible for time around the world have agreed to keep their clocks synchronized, in other words coordinated, according to this standard so it's a great way to keep track of when a migration was created and distort a set of migrations according to when they were created. Let's talk a bit more about Rails migration files, if you're trying to add or remove an attribute, then the migration name should have this form, either Add, the name of the column you're trying to add to the particular table or Remove, the name of the column you're trying to remove from the particular table. If a list of attribute names and types are provided then some add and remove columns statements well also be added and put into the migration file. Here's an example of I said, rails g migration AddPhoneTypeToPhones, well this is going to add an attribute called phone_type to the phones table and notice that I provided the list afterwards, phone_type : integer, is saying that go ahead an put this in to the migration file, adding the attribute called phone_type and specifying it as an integer. Let's finally fix that misnamed title in the post table. And I'm going to do this by generating a migration called ChangeTiteToTitleInPosts, and this will generate that migration and you'll see that it stored it in the db/migrate directory. Let's take a look. Open up your editor and go to the db > migrate, and you'll see that there's a new migration, it's named exactly how I intended, ChangeTiteToTitleInPosts.rb. If we look inside we'll see that the code, there's nothing there. We need to add the code that actually does the change. So, it's rename_column, the name of the table I want to apply it to, the old column name and the new column name. So this should do the trick. Let's save this and then run rake db:migrate. Back in an operating system shell, we're going to type write rake db:migrate. And now it's going to run this new migration you'll see that it changed the tight to titles. Recall schema dump is run after you do rake db:migrate, so hopefully our schema's updated appropriately now, and sure enough here it is. The title is now correct in the post table. Let's take a look at our app itself to make sure that the data's still there after running this migration. I run rails server from the command line and then I went to localhost:3000 in my browser, and you see that it brought up the app. I want to take a look at posts now to see if they're still okay. And you see that it caused an error. And it tells me exactly what the problem is, post.tite. In my file, which file is this, index.html.erb for posts, it doesn't know what this tite is. That's been changed to title. So, even though we ran this migration, it fixed the model, but it didn't fix the view code. You've gotta go through and do that all by yourself. I'll show you how to do it, for the index. In an editor, go to app > views, and then we want to look under posts, and let's go to index.html. And I'm going to fix this to say title rather than tite as it was before. Let's go ahead and save that and then go back to our app. Here's an example of the power of the development environment. I didn't have to restart the server or anything, I just reload this page and the data is there. So, I've gotta go through and do this for all of the views. You see that everything is misspelled here. There's only seven of them in the post-view subdirectory. I've gotta change every place that says tite to title. Why don't you go ahead and get some coffee, by the time you get back I should have it done.