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
轉載請註明: 轉自船長日誌, 本文鏈接地址: http://www.cslog.cn/Content/ruby_on_rails_migration/zh-hant/