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

Wow, your site is very good!

Wow, your site is very good! I like your site, it looks very cute. And I am happy to read the information you post in this article. I also really like Ruby. Even my brother and I have it at home. Would you also like Ruby? Do you have too?

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?
                                                                             

66666666 66666666
6::::::6 6::::::6
6::::::6 6::::::6
6::::::6 6::::::6
6::::::6 6::::::6 ssssssssss uuuuuu uuuuuu
6::::::6 6::::::6 ss::::::::::s u::::u u::::u
6::::::6 6::::::6 ss:::::::::::::s u::::u u::::u
6::::::::66666 6::::::::66666 s::::::ssss:::::s u::::u u::::u
6::::::::::::::66 6::::::::::::::66 s:::::s ssssss u::::u u::::u
6::::::66666:::::6 6::::::66666:::::6 s::::::s u::::u u::::u
6:::::6 6:::::6 6:::::6 6:::::6 s::::::s u::::u u::::u
6:::::6 6:::::6 6:::::6 6:::::6 ssssss s:::::s u:::::uuuu:::::u
6::::::66666::::::6 6::::::66666::::::6 s:::::ssss::::::s u:::::::::::::::uu
66:::::::::::::66 66:::::::::::::66 s::::::::::::::s u:::::::::::::::u
66:::::::::66 66:::::::::66 s:::::::::::ss uu::::::::uu:::u
666666666 666666666 sssssssssss uuuuuuuu uuuu






Enter the code depicted in ASCII art style.