1.Using Rails Migration
1.1 Rails Migration proprties
Rails Migration is used to feed the database schema. It:
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)
and support all the basic data types:
string, text, integer, float, datetime,
timestamp, time, date, binary and boolean:
1.2 Creating the migrations
a. Create tables:
The related syntax is as follows:
C:\ruby\application> ruby script/generate migration table_name
This order creates the file db/migrate/001_table_name.rb.
Now, we will create two migrations tables "hydrocarbons"
and "groups" for the two set organics or classes Hydrocarbon
and Group. Migrations requires names with lower case and
in plural.
C:\ruby\organics> ruby script/generate migration hydrocarbons
C:\ruby\organics> ruby script/generate migration groups
We get:
C:\ruby\organics>ruby script/generate migration hydrocarbons
exists db/migrate
create db/migrate/001_hydrocarbons.rb
C:\ruby\morganics>ruby script/generate migration groups
exists db/migrate
create db/migrate/002_groups.rb
C:\ruby\organics>
b. Configure the corresponding tables
In db/migrate subdirectory of the related application
"organics"; edit the two fresh Ruby created files:
001_create_hydrocarbons.rb and 002_create_groups.rb
001_create_hydrocarbons.rb
Change this file to become as follows:
class Hydrocarbons < ActiveRecord::Migration
def self.up
create_table :hydrocarbons do |table|
# id is created for automatically.
table.column :compound, :string, :limit => 30, :null => false
table.column :nbCarbons, :integer
table.column :formula, :string, :limit => 25, :null => false
table.column :image, :binary
table.column :group_id, :integer
table.column :description, :text
table.column :created_at, :timestamp
end
end
def self.down
drop_table :hydrocarbons
end
end
def self.down
drop_table :hydrocarbons
end
end
002_create_groups.rb
Change this file to become as follows:
class Groups < ActiveRecord::Migration
def self.up
create_table :groups do |table|
table.column :functional, :string
end
Group.create :functional => "Alcohol"
Group.create :functional => "Esters"
Group.create :functional => "Ethers"
Group.create :functional => "Ketones"
Group.create :functional => "Amines"
Group.create :functional => "Aldehydes"
Group.create :functional => "Carboxylic_Acides"
end
def self.down
drop_table :groups
end
end
2 Run the migrations
We need to configure those two files against the
related set databases. This is done by the following
command line:
C:\ruby\organics> rake db:migrate
The program Rake comes with Ruby. "rake migrate" is an
important Rails property. It
- updates the change in the shema database.
- updates the the database against a new release.
- If it doesn't already exist, creates "schema_info"
file to track and set a current version.
|