How to create database migrations in rails

In this tutorial, I’m going to show you how you can create database migrations in rails. In simple terms, database migrations are files that describes the database. Its like a database schema, which we can use to recreate the database even if we do not have a dump file or a backup file for that database.

 

Create a new model

First, you have to navigate to your project folder using the command line:

cdwampwwwRailsProject

Then create a new model. A model is a component of the MVC architecture which is capable of talking to the database. It manages all the things which has something to do with your database. You might want to generate a model which is a noun. Like subjects, users, employees, etc.

rails generate model Subjects

Here’s what it looks like:

image

Navigate to the directories which it says to verify if the files are really generated.

Here’s what the generated model looks like.

class Subjects < ActiveRecord::Base
end

If you look at it, its meaningless. But actually its not.

 

 

Edit the database schema

Now, navigate to the directory where your migration files are. Be sure to change the ‘RailsProject’ to the actual name of your project.

C:wampwwwRailsProjectdbmigrate

The create_subjects.rb file looks like this, the first time you open it up:

  class CreateSubjects < ActiveRecord::Migration
    def self.up
      create_table :subjects do |t|
   
        t.timestamps
      end
    end
   
    def self.down
      drop_table :subjects
    end
  end

If you try to run it, it won’t do anything.

Add some fields inside the create_table function:

t.string "Subject" , :limit=>50, :null=>false 
t.integer "YearLevel", :limit=>40, :null=>false 

Your create_subjects.rb file should now look like this:

class CreateSubjects < ActiveRecord::Migration
  def self.up
    create_table :subjects do |t|
        t.string "Subject" , :limit=>50, :null=>false 
        t.integer "YearLevel", :limit=>40, :null=>false  
        t.timestamps
    end
  end
 
  def self.down
    drop_table :subjects
  end
end

 

Run the migration

To run the migration. Navigate to your project folder again, and type:

rake db:migration VERSION=20110404064413

Just change the version number to match the number which is in your create_subjects.rb file. That number is actually the complete date and time when that migration was created.

Here’s what it looks like:

image

To verify if the change is actually reflected in your mysql database. Open up your mysql console. Then use the database which you have specified in the gemfile.

use NameOfDatabase;

Then show all tables from that database:

show tables;

You can also verify if the fields that you specified are indeed in the subjects table:

show fields from Subject

You’ll notice that it added 3 fields:

  • id
  • created_at
  • updated_at

Those fields are automatically added.  The t.timestamps code created the last 2 fields. The use of those 2 fields is to determine when a record was first created, and when it was last updated.

The id, is the primary key which is used to reference the table subjects. You’ll also notice that its already auto_increment field, and you don’t need to put values in there if you’re trying to add another record.

One thought on “How to create database migrations in rails

  1. Pingback: How to use the rails console « Data Integrated Entity

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s