Ruby-on-rails 在 GIT 中处理 Rails db/schema.rb 文件的正确方法是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6450263/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 01:27:39  来源:igfitidea点击:

What is the right approach to deal with Rails db/schema.rb file in GIT?

ruby-on-railsgit

提问by rubyprince

Should we include schema.rbwhile commiting to GIT? or should we ignore it? what is the right approach?

我们应该schema.rb在提交 GIT 时包含吗?还是我们应该忽略它?什么是正确的方法?

回答by mu is too short

Well the standard schema.rbfile for Rails 2 has this at the end of the comment block at the top of the file:

那么schema.rbRails 2的标准文件在文件顶部的注释块的末尾有这个:

# It's strongly recommended to check this file into your version control system.

The Rails 3 schema.rbthat I have kicking around says the same thing. I think the comment says it all.

schema.rb我玩过的 Rails 3也说了同样的话。我认为评论说明了一切。



Update in response to comments:Yes, mistakes can be made and you can get conflicting changes and bugs mangling your schema.rbbut that's why you want it under revision control, revision control allows you to keep track of everything and backup when needed. There is only one thing in your entire source tree that specifies your database schema and that is schema.rb. Your database schema absolutely is a critical artifact and anything that important needs to be tracked in revision control.

针对评论进行更新:是的,可能会犯错误,并且您可能会遇到冲突的更改和错误,schema.rb但这就是您希望将其置于修订控制之下的原因,修订控制允许您跟踪所有内容并在需要时进行备份。在您的整个源树中只有一件事指定了您的数据库架构,那就是schema.rb. 您的数据库架构绝对是一个关键工件,任何重要的东西都需要在修订控制中进行跟踪。

Any update/merge problems with schema.rbshould be sorted out just by sorting out your conflicting migrations so schema.rbwill get fixed as a side effect of fixing the real problem.

任何更新/合并问题schema.rb都应该通过整理冲突的迁移来解决,因此schema.rb将作为解决实际问题的副作用得到解决。

Yes, schema.rbis a generated file but it is only generated in the sense that your text editor generates your pancake.rbmodel file or an unedited scaffold file is generated.

是的,schema.rb是生成的文件,但它仅在文本编辑器生成pancake.rb模型文件或生成未经编辑的脚手架文件的意义上生成。

Yes, you could rebuild your schema.rbfile by building a new database and then running all of your migrations. But, you should clear out your old migrations now and then to avoid having to check hundreds of migration files every time you rake db:migrateso "rebuild and run all the migrations" often isn't an option in a highly active project.

是的,您可以schema.rb通过构建新数据库然后运行所有迁移来重建文件。但是,您应该不时清除旧的迁移,以避免每次都必须检查数百个迁移文件,rake db:migrate因此“重建并运行所有迁移”在高度活跃的项目中通常不是一个选项。

回答by askegg

Yes. The schema file is used to configure your database when using rake db:reset and other commands. Migrations should only be used when changing the database schema and will always result in a new schema file.

是的。模式文件用于在使用 rake db:reset 和其他命令时配置数据库。迁移仅应在更改数据库架构时使用,并且将始终产生新的架构文件。

回答by Spyros

Well, it's not included on .gitignore by default. So, I think that you would not have a problem including it(I do in my projects, without any problem).

嗯,默认情况下它不包含在 .gitignore 中。所以,我认为包括它在内你不会有问题(我在我的项目中这样做,没有任何问题)。

回答by demas

I don't commit this file to Git, because it create when I launch rake db:migrate.

我不将此文件提交给 Git,因为它在我启动rake db:migrate.

If I will commit this file to Git, I cannot pull new changes from server after each db:migrate.

如果我将这个文件提交到 Git,我不能在每个db:migrate.