Programming & Scripting Tutorials

Ruby on Rails: Migrations



Remember when we made our simple blog, and we had to create a post scaffold and then migrate the datebase?
Well when the scaffold was created a migration .rb file was made, this contained all of the information to edit the database; When we used "rake db:migrate" the migration file took effect on the database.

In this lesson we will be looking closely at these migration files, how to edit them, and how to create new migrations without creating a whole new scaffold.

General migration information


When we create migrations they are stored in "db/migrate/FILENAME.rb", the FILENAME is usually auto-generated. They are usually named in this format: "YYYYMMDDHHMMSS_create_posts.rb"; In this case it's obviously creating our 'posts' columns in the database, Lets take a look inside:

class CreatePosts < ActiveRecord::Migration
  def change
    create_table :posts do |t|
      t.string :name
      t.text :content

      t.timestamps
    end
  end
end


Basically the general structure is:
1) Create the class "CreatePosts" and make it inherit from "ActiveRecord::Migration".
2)When its being migrated, create some columns in our database (and in fact, this data is also used for reverting changes to the database if you wanted to 'undo' the migration)

Editing migrations


As you can probably guess from the example of the blog application we can create and drop tables using "create_table" and "drop_table", but what else can we do?


All of these explain themselves..

For example if we wanted to create a new post column we would use something like this:

add_column :posts, :section_id, :integer


In this case I wanted to add the "section_id" property, and it would be an integer (whole number).

Creating migrations


Creating a migration is pretty simple, we just use a command in terminal and then edit the migration in our db/migrate folder.
To create a migration using terminal you would use something like this (remember windows users to add "ruby" to the start:

rails generate migration AddSectionIdToPosts


In this case I want to have a migration class called "AddSectionIdToPosts", the migrations ".rb" filename will still be auto-generated, however it will base its name around whatever you've called the class.

To edit the ".rb" migration file simply find it in your db/migrate folder and then you can edit it with pretty much any text editor.
When you are ready to execute the changes to the database simply "rake db:migrate" and RoR will notice there is a new file and take action.

A note on migrations


Please note that if you want to migrate the database for production mode (the mode you use when it's actually running online); then you run this:
rake db:migrate RAILS_ENV="production"

If you don't migrate your stuff in production mode, but you do migrate it while testing (in development mode) then your application will work in testing, but the columns or tables will not be edited or created as to do this you must confirm you want to make the migration changes in production mode (using the command shown above).


This Ruby on Rails tutorial was written by


Back to Ruby on Rails

Advertisement: