2017-05-04 54 views
4

以前的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

回答

7

this提交中有意改變了。

因爲我每天至少查看schema.rb 50次以查看此類信息,我將在今晚稍後編寫一個工具,以便在保留正面更改的同時很好地對其進行格式化。

一旦準備好了,我會在這裏發佈一個鏈接。如果我忘記發佈,請在此處填寫錯誤。

編輯:

我創建的腳本,但它很脆。我正在將字符串輸出轉換爲新的字符串輸出,而我並不確定自己已經擊中了所有的edgecase。 如果你想冒險接觸到我,我會給你當前的耙子任務的工作版本,但它不是很好。

EDIT2:

我一直在使用my hacky script有一段時間了,現在沒有問題。如果您想將其添加到您的項目中,請隨意將其複製到耙子任務中。

+0

您可能還對https://github.com/jakeonrails/fix-db-schema-conflicts感興趣,我們已經使用了很長時間來按字母順序對模式進行排序並標準化縮進。 –

相關問題