Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to correctly deal with rails schema source control branching?

I have a versionated schema.rb that reflects the master branch migrations. Whenever I go to another branch and run a new migration in that branch, my database structure is changed.

If I go back to master branch and run db:migrate, that migration changes from the another branch is added to the schema, despite it not belonging to the master branch.

I know this is the way rails works, but how do you devs deal with this?

I heard some people just schema:load to have local database synched with schema, but since it erases all data, it's not a good solution for me because I use a heavy sql dump that helps the development (it is very painful and prone to risks to develop without this data)

like image 616
Hugo Mitsumori Avatar asked Nov 30 '25 16:11

Hugo Mitsumori


1 Answers

Having a production dump for development (using the browser/rails server) is great, but you should probably have a test environment setup that uses a minimal seeded database.

You can get this by doing RAILS_ENV=test rake db:schema:load on an empty (new) DB. What I'd normally do is something like:

git checkout db/schema.rb
RAILS_ENV=test rake db:reset
RAILS_ENV=test rake db:migrate

and load fixtures as needed. That will leave you with a schema that should just have your migration changes and a changed timestamp. (Unless you have stuff like setting changes hardcoded into your schema, in which case they'll be lost - we monkeypatched ActiveRecord::SchemaDumper to avoid this).

After you've made your migration and done this, commit the schema.rb and migration file before you apply the migration to development. You can then discard the schema.rb with the unwanted changes.

like image 146
Rich Avatar answered Dec 02 '25 05:12

Rich



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!