Migration: Simple tool to log your progress

Daviddelavega
3 min readOct 22, 2020

Are you prone to losing track on what changes you’ve made to your code? Don’t know where you went wrong? Oh boy do I have a treat for you!

Migrations are ActiveRecord tools to help you update your schema in easy and consistent way. You can think of each migration as being a new ‘version’ of the database. A schema starts off with nothing in it, and each migration modifies it to add or remove tables, columns, or entries. ActiveRecord knows how to update your schema along this timeline, bringing it from whatever point it is in the history to the latest version. Active Record will also update your shema file to match the up-to-date structure of your database. I personally think that the most convenient aspect of this is that each migration creates a new folder with a time stamp so you will always know when you made what change.

How do you create a migration you ask? Easy! First make sure you have ‘rake’ in your gemfile, then your terminal write “rake db:create_migration NAME=create_table”. Now you can name your table anything you want, like create_users, if you need a users table. Once you hit enter you will see that this migration automatically created a new folder and file without you having to touch anything. Almost like magic

Now let’s say we needed a products table, just as demonstrated before, (“rake db:create_migration NAME=create_products”) will get you that file, but what does it look like? It’s 4 lines of code.

Now here was something that surprised me, I was so accustomed to writing my macros via attr’s. However, ActiveRecord does that for you! Lets look at what a complete products table class looks like

We only added 5 lines of code and our macros have been written for us, however if you try to test that out now your computer will not know what is going on. You’ll need another migration, an easy one to remember too. Simply put (“rake db:migrate”) and then you’ll see that schema we mentioned earlier. It lays out for you your table and tables attributes in an easy to read way. But what if you need to make changes? How am I supposed to buy that product if there is no price? Just like before you create a migration (be specific in migration names). You get that new file and fill in that table as well

Okay so this may seem a little confusing. Why can’t I just change my table in my class file? You can, but your schema won’t update and that can cause future errors. Now altering your table isn’t inherently reversible, so trying to roll back a migration wont work unless you make sure to make it so. You can do that with your up and down methods. It’s easy to think of it is as decorations. You put “up” your new(or changed) decorations and you take “down” your old ones.

If you’re wondering what I mean by roll back a migration, I mean undoing a change you just made. It is a straightforward (“rake db:rollback”) in your terminal and boom, you went back a migration. This is a great tool when you realize you misspelled something or simply just don’t want that added or changed column anymore. But remember as I stated before, make sure to make those changes reversible before trying to roll back.

I understand this can seem to be a bit daunting but once you get into the hang of it it’s truly a godsend at staying organized and staying error free.

Happy coding!

--

--