Ruby on Rails - Migrations

Rails Migration allows you to use Ruby to define changes to your database schema, making it possible to use a version control system to keep things synchronized with the actual code.
This has many uses, including −
Teams of developers − If one person makes a schema change, the other developers just need to update, and run "rake migrate".
Teams of developers − If one person makes a schema change, the other developers just need to update, and run "rake migrate".
Production servers − Run "rake migrate" when you roll out a new release to bring the database up to date as well.
Production servers − Run "rake migrate" when you roll out a new release to bring the database up to date as well.
Multiple machines − If you develop on both a desktop and a laptop, or in more than one location, migrations can help you keep them all synchronized.
Multiple machines − If you develop on both a desktop and a laptop, or in more than one location, migrations can help you keep them all synchronized.
create_table(name, options)
drop_table(name)
rename_table(old_name, new_name)
add_column(table_name, column_name, type, options)
rename_column(table_name, column_name, new_column_name)
change_column(table_name, column_name, type, options)
remove_column(table_name, column_name)
add_index(table_name, column_name, index_type)
remove_index(table_name, column_name)
Migrations support all the basic data types − The following is the list of data types that migration supports −
string − for small data types such as a title.
string − for small data types such as a title.
text − for longer pieces of textual data, such as the description.
text − for longer pieces of textual data, such as the description.
integer − for whole numbers.
integer − for whole numbers.
float − for decimals.
float − for decimals.
datetime and timestamp − store the date and time into a column.
datetime and timestamp − store the date and time into a column.
date and time − store either the date only or time only.
date and time − store either the date only or time only.
binary − for storing data such as images, audio, or movies.
binary − for storing data such as images, audio, or movies.
Boolean − for storing true or false values.
Boolean − for storing true or false values.
Valid column options are − The following is the list of valid column options.
limit ( :limit => “50” )
limit ( :limit => “50” )
default (:default => “blah” )
default (:default => “blah” )
null (:null => false implies NOT NULL)
null (:null => false implies NOT NULL)
NOTE − The activities done by Rails Migration can be done using any front-end GUI or directly on SQL prompt, but Rails Migration makes all those activities very easy.
See the Rails API for details on these.
Here is the generic syntax for creating a migration −
application_dir> rails generate migration table_name

This will create the file db/migrate/001_table_name.rb. A migration file contains the basic Ruby syntax that describes the data structure of a database table.
NOTE − Before running the migration generator, it is recommended to clean the existing migrations generated by model generators.
We will create two migrations corresponding to our three tables − books and subjects.
Books migration should be as follows −
tp> cd library
library> rails generate migration books

Above command generates the following code.
subject migration should be as follows −
tp> cd library
library> rails generate migration subjects

Above command generates the following code.
Notice that you are using lower case for book and subject and plural form while creating migrations. This is a Rails paradigm that you should follow each time you create a Migration.
Go to db/migrate subdirectory of your application and edit each file one by one using any simple text editor.
Modify 001_books.rb as follows −
The ID column will be created automatically, so don't do it here as well.
class Books < ActiveRecord::Migration
   
   def self.up
      create_table :books do |t|
         t.column :title, :string, :limit => 32, :null => false
         t.column :price, :float
         t.column :subject_id, :integer
         t.column :description, :text
         t.column :created_at, :timestamp
      end
   end

   def self.down
      drop_table :books
   end
end

The method self.up is used when migrating to a new version, self.down is used to roll back any changes if needed. At this moment, the above script will be used to create books table.
Modify 002_subjects.rb as follows −
class Subjects < ActiveRecord::Migration
   def self.up
      
      create_table :subjects do |t|
         t.column :name, :string
      end
	
      Subject.create :name => "Physics"
      Subject.create :name => "Mathematics"
      Subject.create :name => "Chemistry"
      Subject.create :name => "Psychology"
      Subject.create :name => "Geography"
   end

   def self.down
      drop_table :subjects
   end
end

The above script will be used to create subjects table and will create five records in the subjects table.
Now that you have created all the required migration files. It is time to execute them against the database. To do this, go to a command prompt and go to the library directory in which the application is located, and then type rake migrate as follows −
library> rake db:migrate

This will create a "schema_info" table if it doesn't exist, which tracks the current version of the database - each new migration will be a new version, and any new migrations will be run until your database is at the current version.
Rake is a Ruby build program similar to Unix make program that Rails takes advantage of, to simplify the execution of complex tasks such as updating a database's structure etc.
If you would like to specify what Rails environment to use for the migration, use the RAILS_ENV shell variable.
For example −
library> export RAILS_ENV = production
library> rake db:migrate
library> export RAILS_ENV = test
library> rake db:migrate
library> export RAILS_ENV = development
library> rake db:migrate

NOTE − In Windows, use "set RAILS_ENV = production" instead of export command.
Now we have our database and the required tables available. In the two subsequent chapters, we will explore two important components called Controller (ActionController) and View (ActionView).
Creating Controllers (Action Controller).
Creating Views (Action View).
 Print
 Add Notes
 Bookmark this page