转自:http://www.cslog.cn/Content/ruby_on_rails_migration
def self.up # db schema更新到下一版本
create_table :table, :force => true do |t| #创建表格
t.column :name, :string
t.column :age, :integer, { :default => 42 }
t.column :description, :text
# :string, :text, :integer, :float, :datetime, :timestamp, :time, :date,
# :binary, :boolean
end
add_column :table, :column, :type #添加段
rename_column :table, :old_name, :new_name #修改段名
change_column :table, :column, :new_type #修改段数据类型
execute "SQL语句"
add_index :table, :column, :unique => true, :name => 'some_name' #添加索引
add_index :table, [ :column1, :column2 ]
end
def self.down # 撤消操作
rename_column :table, :new_name, :old_name
remove_column :table, :column
drop_table :table #删除表格
remove_index :table, :column
end