开发者

'rake' "in order to" 'rake'

开发者 https://www.devze.com 2023-02-08 00:08 出处:网络
To prepare database for my Ruby on Rails 3 application I need to run the following steps in the Terminal:

To prepare database for my Ruby on Rails 3 application I need to run the following steps in the Terminal:

rake db:create
rake db:migrate
rake db:seed

Is it possible to do all those steps in one? Maybe it is possible running a 'rake' command that will "fire" another 'rake' command... but开发者_高级运维 how?!


You can define your own rake tasks which call other tasks as prerequisites:

# lib/tasks/my_tasks.rake
namespace :db do
  desc "create, migrate and seed"
  task :do_all => [:create,:migrate,:seed] do
  end
end

Normally the body of the task would contain Ruby code to do something, but in this case we are just invoking the three prerequisite tasks in turn (db:create,db:migrate,db:seed).


The empty do-end blocks are not needed, e.g. (for zetetic's answer)

$ cat lib/tasks/my_tasks.rake
# lib/tasks/my_tasks.rake
namespace :db do
  desc "create, migrate and seed"
  task :do_all => [:create,:migrate,:seed] 
end


rake db:create db:migrate db:seed will do all that.


zeteitic got it right, but in the event you don't want to namespace this task under "db", you'd want something more like this:

desc "Bootstrap database."
task :bootstrap => ["db:create", "db:migrate", "db:seed"] do; end

And on the command line:

rake bootstrap
# => create, migrate and seed db
0

精彩评论

暂无评论...
验证码 换一张
取 消