Ruby on Rails: Short Migration Summary
Migrations in Ruby on Rails are handled fairly easily. In the def self.up/self.down regions (the first is what is executed when you run the migration, the second what is used when migrations get undone) you have basically access to following options:
create_table "tablename", :options (adds a table)
drop_table "tablename" (drops/deletes a table)
rename_table "oldtablename", "newtablename" (renames a table)
add_column "tablename", :columnname, :type, :options (adds a column)
rename_column "tablename", :oldcolumname, :newcolumname (renames column)
change_column "tablename", :columname, :type, :options (changes column settings)
remove_column "tablename", :columname (deletes column)
add_index "tablename", :columname, :typeofindex (adds an index to a column)
remove_index "tablename", :columname (removes index from column)
So the migration file will look something like this
(you can create a new migration with "ruby script/generate migration MigrationName"):
class CreateStudents < ActiveRecord::Migration
def self.up
add_column "students", :firstname, :string
add_column "students", :remarks, :text
end
def self.down
remove_column "students", :firstname
remove_column "students", :remarks
end
end
You can then migrate to the most recent migration via rake:
rake db:migrate
...or, if you want to migrate (backwards) to a specific version
rake db:migrate VERSION=5
(5 being the version number of the migration you want to revert to)





Post new comment