How do I code a rake task that runs the Rails db:migrate task?

佐手、 提交于 2020-01-04 02:07:10

问题


I would like to run db:migrate VERSION=0 and then db:migrate inside of my own rake task. I am confused about how to do this. Do I need a special require statement? My rake task will reside in the lib/tasks directory of a Rails app. Thanks.


回答1:


Is your task just dependent on having a clean db? If that's the case then you can do:

task :my_task => [:environment, 'db:reset']




回答2:


EDIT: Rake::Task[] won't accept parameters, you have to set it in ENV. In addition, you have to reenable the task to run it multiple times.

ENV['VERSION']= '0'
Rake::Task['db:migrate'].invoke
Rake::Task['db:migrate'].reenable
ENV.delete 'VERSION'
Rake::Task["db:migrate"].invoke

NOTE: Rake::Task.reenable requires Rake 0.8.2 or higher.




回答3:


Check out rake db:reset as that will accomplish what you are trying to do.

To see what all of your rake tasks do, run rake -T



来源:https://stackoverflow.com/questions/611189/how-do-i-code-a-rake-task-that-runs-the-rails-dbmigrate-task

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