简体   繁体   中英

Keep Rails' `schema.rb` separated in each git branch?

Using Rails and Git, if I run a migration in one branch, the table is now in the database. Now if I check out a branch in which I don't want that migration, the table is still committed to the schema.rb , because it gets dumped from the database.

Is there a way to prevent the schema from getting dumped from the database? For example is it possible to maybe have the schema be generated by running the migrations instead of by dumping the tables from the DB? It's really annoying to me, and to the other people in my company.

When you switch branches, either create a brand new DB and load an empty schema, or rollback migrations to the common anscestor, then migrate up again once you're in the new branch.

There are some tools and scripts that can help with this, but I haven't used any of them personally so I can't vouch for them. Here's a couple that I found after googling:

The schema file changing is only the symptom of the true problem: rails is dynamically building the properties of your models from the schema of the database. Sweeping the schema.rb file under the rug will be insufficient because rails will still be making assumptions about your models based on a database that has the incorrect schema for the branch you're currently working in.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM