What Is A Rake Migration, How To Use It, And How To Not Make The Mistakes I Did

Yisroel Malamud
4 min readJan 23, 2019

While working on my mod 1 project at flatiron I came across a few issues. Some of the issues were rake related, while others (Which are now simple and understandable) were quite difficult and frustrating to say the least.

After following a lecture video step by step, on how to setup my project classes and building out their relationships, I was finally ready to create a rake migration.

What is a rake migration ?

a rake migration is method of adding or altering data to your database. Ok, so now that we know what a migration is, let’s create one and use it.

A few things to know about creating a migration

1. Discover how to create a migration by typing this in your terminal

rake -T 

It will display all of your possible “rake tasks”

rake console                # starts a console
rake db:create # Creates the database from DATABASE_URL or config/database.yml for the current RAILS...
rake db:create_migration # Create a migration (parameters: NAME, VERSION)
rake db:drop # Drops the database from DATABASE_URL or config/database.yml for the current RAILS_E...
rake db:environment:set # Set the environment value for the database
rake db:fixtures:load # Loads fixtures into the current environment's database
rake db:migrate # Migrate the database (options: VERSION=x, VERBOSE=false, SCOPE=blog)
rake db:migrate:status # Display status of migrations
rake db:rollback # Rolls the schema back to the previous version (specify steps w/ STEP=n)
rake db:schema:cache:clear # Clears a db/schema_cache.yml file
rake db:schema:cache:dump # Creates a db/schema_cache.yml file
rake db:schema:dump # Creates a db/schema.rb file that is portable against any DB supported by Active Record
rake db:schema:load # Loads a schema.rb file into the database
rake db:seed # Loads the seed data from db/seeds.rb
rake db:setup # Creates the database, loads the schema, and initializes with the seed data (use db:...
rake db:structure:dump # Dumps the database structure to db/structure.sql
rake db:structure:load # Recreates the databases from the structure.sql file
rake db:version # Retrieves the current schema version number

find whichever task it is that you would like to create, in our case it’s the line of code that point to our “rake db:create_migration” task

rake db:create_migration #with the parameters of NAME, VERSION)#In our example we are only using the NAME parameter so for let's disregard the VERSION 

This is where we want to create a migration with the parameter of NAME.

In our case the proper syntax would be

rake db:create_migration NAME=create_migration_name# migration_name would be whatever migration name we would choose to give it.

Some Things To Be Aware Of And Double Check

  • Your migration names are lower case
  • Your class names are singular
  • Your table migrations are pluralized

2. Putting data into your migration file

class CreatePersonMigration < ActiveRecord::Migration
def change
create_table :persons do |t|
t.string :name
t.integer :age
t.string :favorite_food
t.string :favorite_activity
end
#end of "do" loop
end
#end of "change" method
end
#end of class

Let’s break down our migration…

# We created a migration class called CreatePersonMigration which inherits from ActiveRecord and the Migration module# We defined a method called change
and created a table called persons (table names have to be plural) when we wrote out create_table:persons
# we then looped through our table and created columns with do |t| and made new columns in the person table using t. "string, integer etc"
t.string :name
t.integer :age
t.string :favorite_food
t.string :favorite_activity
end
#end of "do" loop
end
#end of "change" method
end
#end of class

3. How To actually migrate

Once we’ve create our migrations, inputed all of our table data correctly, and double checked that the information we’ve entered is what we’d like.We can move on to migrating our migration with

rake db:migrate

We’ve successfully migrated! (Assuming everything migrated without any errors.)

4. Checking your schema

After running

rake db:migrate

We should see a new file in your db/migrate folder called schema.rb, it should look something like this.

READ THIS CAREFULLY!# This file is auto-generated from the current state of the database. Instead of editing this file, please use the migrations feature of Active Record to incrementally modify your database, and then regenerate this schema definition.

# Note that this schema.rb definition is the authoritative source for your database schema. If you need to create the application database on another system, you should be using db:schema:load, not running all the migrations from scratch. The latter is a flawed and unsustainable approach (the more migrations you'll amass, the slower it'll run and the greater likelihood for issues).

# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 0) docreate_table "persons", force: :cascade do |t|
t.string :name
t.integer :age
t.string :favorite_food
t.string :favorite_activity
end
end

Bam! we have a working schema

make sure the schema file looks how you imagined it to be before seeding your data to the database.

5. Some things to think about while doing this

  1. create model #A class
  2. create migration #migration class
  3. run migration #rake db:migrate
  4. check your schema #look back at step 4.
  5. test! #using pry if you can and know how to

--

--