ruby-on-rails – 为什么在rails 5.1.0中没有正确格式化schema.r
发布时间:2020-12-17 02:49:45 所属栏目:百科 来源:网络整理
导读:以前schema.rb是一个很好的地方,可以快速查看列默认值是什么,以及它们是否可以为空,但现在它很乱.例如,这是一个用户表: create_table "users",force: :cascade do |t| t.string "name",null: false t.string "email",null: false t.string "locale",default
以前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",null: false t.string "password_digest",unique: true end 为什么会发生这种情况,如何在保持良好变化的同时修复它,如id :: serial和implicit btree? 解决方法
它在
this提交中故意更改.
因为我每天至少看50次schema.rb才能看到这种类型的信息,我将在今晚晚些时候编写一个工具来很好地格式化它,同时保留积极的变化. 一旦准备就绪,我会在这里发布链接.如果我忘记发布它,请在这里给我带来麻烦 编辑: 我创建了脚本,但它非常脆弱.我正在将字符串输出转换为新的字符串输出,我不相信我已经击中了所有的边框.如果你想冒险与我联系,我会给你当前工作版本的rake任务,但它并不是很好. EDIT2: 我一直在使用my hacky script一段时间没有问题.如果要将其添加到项目中,请随意将其复制到rake任务中. 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 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |