Thursday, December 15, 2005

Adding options to rake tasks

I've been wondering how to call a rake task and pass it arguments.... just found it:

the example is in the rails code source code (railties/lib/tasks/databases.rake) for migrations. Normally you just call 'rake migrate' to come up to the current version. But if you want to revert back to another database version, you would add "rake migrate VERSION=12" (or whatever version is appropriate).

A quick look at the rails codebase has the migrate task as:

task :migrate => :environment do
ActiveRecord::Migrator.migrate("db/migrate/", ENV["VERSION"] ? ENV["VERSION"].to_i : nil)
Rake::Task[:db_schema_dump].invoke if ActiveRecord::Base.schema_format == :ruby

It uses
which is just accesses the environment.
So rake just uses the command line to set up environment variables. Good to know.


Post a Comment

Links to this post:

Create a Link

<< Home