By the end of this, students should be able to:
- Write Rails migrations to alter a database schema
- Migrate and rollback between migrations to change the current state of the schema
- Start a new ruby project called "location_manager"
- Follow below instructions in order
Your boss comes to you and says, "We just got in a new project. I need you to setup the database to make it happen, here's what I need..." and points at a schema drawn out on the wall with names of columns an datatypes.
"Location is everything!" he exclaims and walks off.
Create a single migration called CreateLocations, using the migrations generator, that will have these columns in place on the board.
Run the migrations with rails db:migrate
"I don't even know what city and state these location are in!" says your boss. You wonder why he couldn't have done this right in the first place.
Create a migration called AddCityAndStateToLocations, using the migrations generator, that will add columns of city and state to the schema. City and state should each be of type string.
Run the migrations with rails db:migrate
to apply the migrations.
"I just read that places have colors. All our locations should have a color"
At this point you're pretty sure your boss has lost it, but he signs off on your paycheck so you're going to deal with it.
Create a migration called AddColorToLocations, using the migrations generator, that will add a column called color
to the locations table with the datatype of string.
Run the migrations with rails db:migrate
to apply the migrations.
"Colors are so 2014. Now we need flavors!"
At this point, you're brushing up on your resume and making sure you know where the nearest emergency exit is, but tomorrow's payday and you're hoping for him to sign off on that check.
Create a migration called AddFlavorAndRemoveColorFromLocations, using the migrations generator. Add a column called flavor
with datatype of string, and remove the column called color
in the same migration.
Run the migrations with rails db:migrate
to apply the migrations.
"All things are for sale. Why don't we sell the locations? We need prices!"
Create a migration called AddPricesToLocations, using the migrations generator. Add a column called 'price' with datatype of decimal.
Run the migrations with rails db:migrate
to apply the migrations.
"Maybe that last one was a poor idea, let's talk a step back"
You did your job well, but he just can't make up his mind. Thankfully you've been getting some calls from recruiters and have an interview at Google on Tuesday.
Rollback the last migration.