1:建表选项
:null => true
:limit=> size
:default => value
2:添加 字段
3:修改 数据
4用sql改变 数据
:null => true
:limit=> size
:default => value
ruby 代码
- class CreateUsers < ActiveRecord::Migration
- def self.up
- create_table :users do |t|
- t.column :login, :string
- t.column :email, :string
- t.column :password, :string
- t.column :created_at, :datetime
- t.column :last_login_at, :datetime
- t.column :admin, :boolean
- t.column :posts_count, :integer, :default => 0
- end
- end
- def self.down
- drop_table :users
- end
- end
ruby 代码
- class AddForumDesc < ActiveRecord::Migration
- def self.up
- add_column "forums", "description", :string
- end
- def self.down
- remove_column "forums", "description"
- end
- end
3:修改 数据
ruby 代码
- class SetLastSeenAt < ActiveRecord::Migration
- def self.up
- User.find(:all).each do |user|
- if user.last_seen_at.nil?
- user.last_seen_at=Time.now.utc
- user.save!
- end
- end
- end
- def self.down
- end
- end
4用sql改变 数据
ruby 代码
- class FixRepliedAt < ActiveRecord::Migration
- def self.up
- execute 'update posts set replied_at=created_at where replied_at is null and id=topic_id'
- end
- def self.down
- end
- end