在git中管理schema.rb的首选方式是什么?

编程入门 行业动态 更新时间:2024-10-13 14:25:03
本文介绍了在git中管理schema.rb的首选方式是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我不想将 schema.rb 添加到 .gitignore 中,因为我希望能够从该文件加载新的数据库模式。但是,保持登录状态会导致各种各样的虚假冲突,这些冲突可以通过新鲜的 db:migrate:reset 轻松解决。

基本上我想要一种方式:

  • 将schema.rb保存在部署时数据库的存储库中设置
  • 将schema.rb保存在'.gitignore'中用于常规开发
  • 一两个人负责更新 schema.rb 并知道它是正确的。

    有没有办法可以有我的蛋糕,也可以吃吗? 解决方案

    对我来说真正有效的是删除和.gitignore schema.rb ,然后在每个开发者 rake db:migrate 时重新生成。

    通过简单地定期进行迁移汇总,您仍然可以实现自己想要的目标,而不需要从0迁移并冒着多年前破坏迁移的风险。您可以通过以下方式执行此操作:

  • 使用 rake db:migrate 运行所有未完成的迁移。
  • 将 ActiveRecord :: Schema.define中的 schema.rb 的内容
  • 将它粘贴到 def up 内的initial_schema迁移中(覆盖已存在的内容)
  • 删除所有其他迁移
  • 现在您的initial_schema迁移是新系统的起点,您不必担心关于 schema.rb 中可能无法正确解析的冲突。这不是神奇的,但它的工作原理。

    I don't want to add schema.rb to .gitignore, because I want to be able to load a new database schema from that file. However, keeping it checked in is causing all sorts of spurious conflicts that are easily resolved by a fresh db:migrate:reset.

    Basically I want a way to:

  • Keep schema.rb in the repository for deploy-time database setup
  • Keep schema.rb in '.gitignore' for general development
  • There would be one or two people responsible for updating schema.rb and knowing that it was correct.

    Is there a way I can have my cake and eat it, too?

    解决方案

    What has worked really well for me is to delete and .gitignore schema.rb and then have it regenerated for each developer when they rake db:migrate.

    You can still achieve what you wanted without migrating from 0 and risking broken migrations from years ago by simply doing a "roll-up" of the migrations periodically. You can do this by:

  • Run all outstanding migrations with rake db:migrate
  • Taking the contents of your schema.rb in the ActiveRecord::Schema.define block
  • Paste it into your initial_schema migration inside def up (overwriting what's already there)
  • Delete all other migrations
  • Now your initial_schema migration is your starting point for new systems and you don't have to worry about conflicts in schema.rb that may not be resolved correctly. It's not magical, but it works.

    更多推荐

    在git中管理schema.rb的首选方式是什么?

    本文发布于:2023-10-18 04:29:05,感谢您对本站的认可!
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:首选   方式   git   schema   rb

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!