ruby db:migrate

51 篇文章 0 订阅

 

 

 

命令列表

 

rake db:migrate

命令作用:将所有未实施的迁移任务都实施到目标数据库上。

 

rake db:migrate VERSION=20080906120000

命令作用:撤销迁移任务到指定版本

迁移版本信息可以在所创建库(如elvuel_deployment数据库)下schema_info表中查看到相关记录信息。

 

rake db:migrate VERSION=0

命令作用:将数据库返回到最初状态(即,只有schema_migrations表,其他表都删除)

 

rake db:migrate VERBOSE=false  

命令作用:屏蔽migration的输出  

 

rake db:rollback

命令作用:回滚到最近的一次migration执行的状态  

 

rake db:rollback STEP=3  

命令作用:回滚最近的3次的迁移任务

 

 

一个在开发阶段很常用的命令redo:

rake db:migrate:redo

命令作用:重做迁移

 

rake db:migrate:redo STEP=3

rake db:migrate:down VERSION=9999999999

 

数据库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

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值