在 Rails 中清理迁移的好方法是什么?
所以我已经在这个 Web 应用程序上工作了一年,我想将架构编译为一个迁移,这样我的文本编辑器加载速度更快,git 工作目录也不会那么混乱。
搜索查找会更快。
我的任何配置/数据库都不会长 4000px。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您不需要永远保留迁移,只要您确定不再需要它们,就可以删除它们。只需进入您的 db/migrate/ 目录并删除几个月前的迁移即可。
只要您要删除的所有迁移已应用到各处(即开发和生产),那么您就不再需要它们了(除非您想倒退)。实际上,迁移并不意味着是永久文件,它们只是将您从 A 带到 B,然后它们就只是行李了。
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.
迁移服务器后,请删除迁移文件。如果您想开始全新部署,请运行
rake db:schema:load
或rake db:setup
。您不应该按照解释重新运行所有迁移 这里。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
orrake db:setup
. You shouldn't be re-running all your migrations as explained here.一种方法是使用空白数据库并运行所有迁移。现在您已经获得了可以保存到 yaml 的所有模板数据。 yaml 加上架构应该足以恢复数据库,而无需运行任何以前存在的迁移。
但是,其他答案应该提到用于执行此操作的现有工具或 gem。
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.
鉴于没有一个答案提到它,这是完成这项工作的宝石: https://github.com/jalkoby /squasher
它基本上从头开始重新运行迁移,直到您指定的日期为止,然后将生成的
db/schema.rb
加载到初始迁移中以替换旧的迁移。它还可以清理schema_migrations
表,这样您在运行
rake db:migrate:status
时就不会获取这些条目。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 theschema_migrations
table so you don't get thoseentries when running
rake db:migrate:status
.