What's a good way to clean up my migrations in Rails?

匆匆过客 提交于 2019-12-03 09:11:07

问题


So I've been working on this web app for a year now and I would like to compile to schema into ONE migration, that way my text editor loads faster, git working directory isn't so cluttered.

Search find will be faster.

Any my config/db won't be 4000px long.


回答1:


One way to go is to take a blank database and run all the migrations. Now you've got all the template data which you can save to a yaml. The yaml plus the schema should be enough to bring the DB back without running any of your previously existing migrations.

However, other answers should mention an existing tool or gem for doing this.




回答2:


Remove the migration files once you've migrated your servers. If you ever want to start with a fresh deployment, run rake db:schema:load or rake db:setup. You shouldn't be re-running all your migrations as explained here.




回答3:


You don't need to keep your migrations around forever, you are free to delete them as soon as you're sure you don't need them anymore. Just go into your db/migrate/ directory and delete the migrations that are older than, say, a couple months.

As long as all the migrations that you want to delete have been applied everywhere (i.e. development and production) then you don't need them anymore (unless you want to go backwards). Really, migrations aren't meant to be permanent files, they're just around to get you from A to B and then they're just baggage.




回答4:


Given that none of the answers mention it, this is the gem that does the job: https://github.com/jalkoby/squasher

It basically reruns the migrations from scratch until the date you specify, and then loads the resulting db/schema.rb into an initial migration that replaces the old ones. It can also cleanup the schema_migrations table so you don't get those

up     <timestamp>  ********** NO FILE **********

entries when running rake db:migrate:status.



来源:https://stackoverflow.com/questions/6824608/whats-a-good-way-to-clean-up-my-migrations-in-rails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!