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

Should we include schema.rb during the execution of the GIT command? or should we ignore him? What is the right approach?

+36
git ruby-on-rails
Jun 23 2018-11-11T00:
source share
4 answers

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

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

The Rails 3 schema.rb , which I kick, says the same thing. I think the comment says it all.




Update in response to comments: Yes, errors can be made, and you can get conflicting changes and errors that distort your schema.rb , but that is why you want it to be under version control, version control allows you to track everything and, if necessary make backups. There is only one thing in the entire source tree that indicates your database schema, which is schema.rb . Your database schema is absolutely a critical artifact, and all that matters is to be monitored in version control.

Any issues with updating / merging with schema.rb should be sorted simply by sorting your conflicting migrations, so schema.rb will be fixed as a side effect to fix the real problem.

Yes, schema.rb is a generated file, but it is generated only in the sense that your text editor generates your pancakes.rb model file or an unedited archive file is created.

Yes, you can restore your schema.rb file by creating a new database and then doing all your migrations. But you should clean your old migrations from time to time to avoid having to check hundreds of migration files every time you rake db:migrate , so "rebuild and run all migrations" is often not an option in a very active project.

+55
Jun 23 2018-11-11T00:
source share

Well, it is not included in .gitignore by default. So, I think that you would not have problems, including it (I do in my projects without any problems).

+1
Jun 23 2018-11-11T00:
source share

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 lead to the creation of a new schema file.

+1
Jun 23 2018-11-11T00:
source share

I do not attach this file to Git because it is created when rake db:migrate is run.

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

0
Jun 23 '11 at 10:22
source share



All Articles