Stefan Ernst

Ruby on Rails: Short Migration Summary

Ruby on Rails in Terminal

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)

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <pre>
  • Lines and paragraphs break automatically.

More information about formatting options

CAPTCHA
Are you human?
                                                                                

222222222222222 999999999 222222222222222
2:::::::::::::::22 99:::::::::99 2:::::::::::::::22
2::::::222222:::::2 99:::::::::::::99 2::::::222222:::::2
2222222 2:::::2 9::::::99999::::::9 2222222 2:::::2
2:::::2 ssssssssss 9:::::9 9:::::9 2:::::2
2:::::2 ss::::::::::s 9:::::9 9:::::9 2:::::2
2222::::2 ss:::::::::::::s 9:::::99999::::::9 2222::::2
22222::::::22 s::::::ssss:::::s 99::::::::::::::9 22222::::::22
22::::::::222 s:::::s ssssss 99999::::::::9 22::::::::222
2:::::22222 s::::::s 9::::::9 2:::::22222
2:::::2 s::::::s 9::::::9 2:::::2
2:::::2 ssssss s:::::s 9::::::9 2:::::2
2:::::2 222222 s:::::ssss::::::s 9::::::9 2:::::2 222222
2::::::2222222:::::2 s::::::::::::::s 9::::::9 2::::::2222222:::::2
2::::::::::::::::::2 s:::::::::::ss 9::::::9 2::::::::::::::::::2
22222222222222222222 sssssssssss 99999999 22222222222222222222






Enter the code depicted in ASCII art style.