Rails Migration 文件是不是可以 “用后即焚”?

fenprace · 2012年08月30日 · 最后由 chentianwen 回复于 2012年08月31日 · 3634 次阅读

rake db:migrate 一遍后,就会生成 schema.rb,这时 Migration 文件存在还有什么意义?

你对这两个文件有点误解..migrate 可以说是数据库的版本控制。schema 是反映当前数据库结构的

想要后悔药吗?

#2 楼 @leekelby 指的是借助 Migration 可以 rollback 吗? 只要知道当前数据库结构,再写一个 Migration 不就行了?

#3 楼 @fenprace 有方便干嘛不用,真这样的话像 java 一样一个个字符敲也可以。直接在 schema.rb 里写代码~

#1 楼 @jjym schema.rb 到底有什么用啊,“反映数据库结构”只是给人看的?

看我之前提这个帖子的讨论: http://ruby-china.org/topics/4917

其实 schema.rb 开头的说明已经很清楚了。

# This file is auto-generated from the current state of the database. Instead # of editing this file, please use the migrations feature of Active Record to # incrementally modify your database, and then regenerate this schema definition. # # Note that this schema.rb definition is the authoritative source for your # database schema. If you need to create the application database on another # system, you should be using db:schema:load, not running all the migrations # from scratch. The latter is a flawed and unsustainable approach (the more migrations # you'll amass, the slower it'll run and the greater likelihood for issues). # # It's strongly recommended to check this file into your version control system.

#5 楼 @valentine schema.rb 删掉不会对当前程序运行有任何影响。但有两种情况我感觉 schema.rb 很有用: 查看一个开源代码的数据库结构,如果看 migration 会头大,但看 schema 一目了然。 还有就是对于一些烂尾的项目 migrate 太多可能会执行出错,就直接rake db:schema:load

schema.rb 是用于显示当前数据库结构的,一般会被用于测试环境中,用来创建测试数据库的。最好不要删。

需要 登录 后方可回复, 如果你还没有账号请 注册新账号