Keep Rolling, Rolling, Rolling, Roll(back)

Courtney Wilson
2 min readSep 6, 2021

I was trying to think of a good way to segue into this article with a Limp Bizkit lyric from the above song (I hope you heard it in your head, too). But unfortunately, that would not be very proper or professional. So we will just get started..

When I first started using ActiveRecord, all of the rake commands were confusing to me. Check them out below to see what I mean -

Run “rake -T” without the “ “ in your console to see a list of rake commands.

One command that was elusive to me was the rake db:rollback. But before we get into what it does, let’s mention a couple of things about rake.

According to Wikipedia, rake is — “a software task management and build automation tool created by Jim Weirich. It allows the user to specify tasks and describe dependencies as well as to group tasks in a namespace.” So rake is the command used to get ActiveRecord’s attention. In the list pictured above, you can see the description of the dependencies (this information following the # symbol), and you can see the namespace groups. The namespace grouping does just that, it groups related tasks together. For instance, all of the rake db:_____ commands group together tasks that relate to the database, such as create, migrate, drop, and rollback.

When you use rake db:create_migration NAME=create_shows, it will create a migration that looks like this -

From here, you’ll fill your create_table or add_column or whatever action you need to do. After you fill in your data, you’ll run rake db:migrate to get your schema going. But what happens if you check your schema and realize you misspelled something? Instead of dropping the database and starting from scratch, you can simply rollback the database = rake db:rollback.

Once you perform that action, you can go in and fix any mistakes you made. However, it’s important to note that you can only fix them after you’ve rolled back the db. It won’t work if you fix it, then rollback. Remember to rollback first, fix your data, then run your migration again.

The rake db:rollback command gives you the tool to keep rolling on smoothly.

--

--