Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Way to "flatten" Rails migrations?

I'm working on deploying my first Rails application right now, and somewhere along the way, I botched a migration. When I try to push my application to the production server and run rake db:migrate, it fails somewhere with an error.

Now, I am way too lazy to work through my migrations individually to find out what went wrong, so I'm trying to avoid doing that. Given that my current development database works just fine, is there a way to "flatten" the current schema into a single, comprehensive migration?

I understand that this is sloppy, and I understand that I likely did a dumb thing to break the migration chain in the first place. (I probably edited the database schema directly somewhere, which I now understand is a no-no.) This is a pretty small project though, and I'm essentially the only developer involved, so I'm comfortable sweeping this issue under the rug if possible.

Is there a way to do that?

Thanks for lending your expertise.

like image 961
Chris Allen Lane Avatar asked Jun 07 '11 21:06

Chris Allen Lane


2 Answers

This is what the db/schema.rb file is for. If you've only got structural changes in your migrations you will be able to run rake db:schema:load rather than running rake db:migrate to get the absolute structure for your tables.

like image 170
Ryan Bigg Avatar answered Sep 20 '22 05:09

Ryan Bigg


If you edited the schema directly you will need to run:

rake db:schema:dump

This will take whatever is in the database and create a schema.rb file. Then you can run rake db:schema:load anytime you want. However it will mean that your migrations are still bad. You could delete all of them and recreate them from the schema.rb file.

like image 25
Kyle Boon Avatar answered Sep 19 '22 05:09

Kyle Boon