在git中管理schema.rb的首选方式是什么? [英] What is the preferred way to manage schema.rb in git?

查看:150
本文介绍了在git中管理schema.rb的首选方式是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



基本上我想要一种方式:


  1. 将schema.rb保存在部署时数据库的存储库中设置

  2. 将schema.rb保存在'.gitignore'中用于常规开发

一两个人负责更新 schema.rb 并知道它是正确的。



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

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



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


  1. 使用 rake db:migrate 运行所有未完成的迁移。

  2. ActiveRecord :: Schema.define中的 schema.rb 的内容
  3. 将它粘贴到 def up 内的initial_schema迁移中(覆盖已存在的内容)
  4. 删除所有其他迁移

现在您的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:

  1. Keep schema.rb in the repository for deploy-time database setup
  2. 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:

  1. Run all outstanding migrations with rake db:migrate
  2. Taking the contents of your schema.rb in the ActiveRecord::Schema.define block
  3. Paste it into your initial_schema migration inside def up (overwriting what's already there)
  4. 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的首选方式是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆