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.
Asked
Active
Viewed 4,506 times
3 Answers
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.

Pesto
- 23,810
- 2
- 71
- 76
-
I tried this inside of my custom rake task (residing in lib/tasks) but it failed. ("rake aborted Don't know how to build task db:migrate VERSION=0") – fooledbyprimes Mar 04 '09 at 16:12
-
Rake::Task[] does accept params: Rake::Task['my:task'].invoke("123-abc") – Webdevotion Jun 24 '14 at 14:28
2
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']

Mike Breen
- 2,610
- 1
- 19
- 13
-
Okay I like this. It works. This must surely mean that the :environment task loads all the rails rake namespaces. – fooledbyprimes Mar 06 '09 at 00:18
0
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

erik
- 6,406
- 3
- 36
- 36
-
This is interesting but it does not really explain how to run a pre-built rails task inside of a newly created custom rake task. – fooledbyprimes Mar 04 '09 at 16:09
-
Yep, since Pesto got his answer in before mine, I figured it wouldn't hurt to show you an easier way even though it didn't answer the question exactly. – erik Mar 04 '09 at 16:21