It is considered safe to manually edit schema.rb in rails

I ran into a problem when I was working on two branches in a rails project, and each project has a migration to add a column. At that time, rake db:migrate:reset caused a problem, and I only relied on my schema.rb to correctly represent the state of my database. At some point, I ran into a problem when the column added by branch A got into the scheme of branch B. Since migrate:reset not an option, I resorted to manually editing the scheme file. I made this change, which basically removed the column from branch A, which I don't need in branch B. schema.rb.

The problem arose after I merged branch A into the master. When I tried to translate the B branch to master, I still had a commit in B to remove the column (which has now become relevant because it is in master) in the schema file. Git saw no conflict for this and automatically merged it. At the end of my call forwarding, I found that my circuit does not match what I have with the master.

My fix is ​​to edit the schema file again and manually add the previously deleted column back to the schema file. My question is: is this considered unconventional? dangerous? Hacky?

Now it includes one column, but if it involves deleting / adding multiple columns, then (dangerous?) Solution can lead to more problems and db / schema.rb mismatch.

+7
source share
1 answer

It is usually considered bad practice to edit your schema.rb file.

According to the Rails Guide on Migrations :

Migrations, as powerful as they can be, are not an authoritative source for your database schema. This role applies either to db / schema.rb, or to the SQL file that Active Record generates when examining the database. They are not meant to be edited; they simply represent the current state of the database.

schema.rb updated every time you start a new migration:

Note that running db: migrate also calls the db: schema: dump task, which will update your db / schema.rb file according to the structure of your database.

I would recommend just spending some time schema.rb out and schema.rb file schema.rb on track and fixing it until the last set of migrations.

+6
source

All Articles