为什么 schema.rb 在 rails 5.1.0 中的格式不正确?



以前的schema.rb是快速查看哪些列默认值以及它们是否可以无效的好地方,但现在很混乱。例如,这是一个用户表:

create_table "users", force: :cascade do |t|
  t.string   "name",                              null: false
  t.string   "email",                             null: false
  t.string   "locale",          default: "en-ca", null: false
  t.string   "password_digest",                   null: false
  t.datetime "created_at",                        null: false
  t.datetime "updated_at",                        null: false
  t.index ["email"], name: "index_users_on_email", unique: true, using: :btree
end

现在看起来很恐怖:

create_table "users", id: :serial, force: :cascade do |t|
  t.string "name", null: false
  t.string "email", null: false
  t.string "locale", default: "en-ca", null: false
  t.string "password_digest", null: false
  t.datetime "created_at", null: false
  t.datetime "updated_at", null: false
  t.index ["email"], name: "index_users_on_email", unique: true
end

为什么会发生这种情况?如何在保持良好更改的同时进行修复,例如id: :serial和隐式btree

它在此提交中故意更改。

因为我每天至少看架构架。

准备就绪后,我将在此处发布一个链接。如果我忘记发布它,请在这里烦我。

编辑:

我创建了脚本,但是很脆。我正在将字符串输出转换为新的字符串输出,但我不相信我已经击中了所有EDGECASE。如果您想冒险,它可以向我伸出援手,我会给您当前的耙子任务,但这并不是很棒。

edit2:

我已经使用了一段时间了,我已经使用了hacky脚本了一段时间。如果要将其添加到项目中,请随时将其复制到耙子任务中。

edit3:

我已经切换到SQL模式,因为它更好地涵盖了我们在生产/测试中工作时通常想要的东西。我仍然想在开发过程中阅读schema.rb,所以我所做的是以下内容,它的效果很好,风险较小:

# In config/application.rb
config.active_record.schema_format = :sql

# In config/environments/development.rb
config.active_record.schema_format = :ruby
# In the rake task
namespace :db do
  def cleanup_schema
    # Other code goes here
  end
  task :migrate do
    if Rails.env.development?
      cleanup_schema
    end
  end
  task :rollback do
    if Rails.env.development?
      cleanup_schema
    end
  end
  task :cleanup_schema do
    cleanup_schema
  end
end

最新更新