[Rake-devel] redefine rake task broken in 0.8.x for a rails app

John Dell spovich at gmail.com
Thu Jan 3 14:07:58 EST 2008

----- "Jim Weirich" <jim.weirich at gmail.com> wrote:
> To fix that, change the calling sequence to *args and then the  
> resolve_args argument can go back to being just plain args (rather  
> than [args]).

Seems like it should work, but it isn't.  No errors reported, but tasks are not redefined.

> Since redefine/clear_task functionally is something several people  
> have asked for, I'm thinking about adding something like that to rake 
> directly.  So I'm trying to understand what people need out of this  
> feature.

Awesome!  I was really hoping you would say that! Thanks!

> Are you trying to ...
> (1) Clear existing prerequisites and actions and introduce new ones,
> or

Yes, this is what we are doing.  This makes it easy to pop into the right point in the large rails task list.

For example, the default rails 'rake test' does all kinds of stuff to prepare the test database.  To allow our custom test db build, we just redefine the task for prepare environment to call our new task:

namespace :db do
  namespace :test do
    desc 'Drop/recreate test database, migrate schema, and load fixtures in order'
    Rake::Task.redefine_task :prepare => :environment do
    desc 'Drop/recreate test database, migrate schema, and load fixtures in order'
    task :custom_db_setup do
      `script/setup-db test`

> (2) Completely delete the existing task and replace it with a  
> different task (of a possibly different task type)?

Probably useful for somebody.  Given the choice, more flexibility and options the better.

> If you have use cases for this, I would like to hear them.

For us, the main scenario use case is we are redefining 'rake test' for our rails app to make tests run in a reasonable amount of time.  Because we use foreign key constraints in Postgres during development, the unit test method for loading/cleaning up fixtures is not useable.  To get around this, we have a custom script that drops/recreates test db, loads fixtures only once (and in order to preserve FK constraints), and then tests have 'transactional-fixtures = true' so everything rolls back, and we don't reload fixtures continually with each test.  This simple step cuts test runs down by an order of magnitude for us.

Another use case is the 'engines' plugin which monkey patches rake to add redefine_task. This is to enhance certain rails rake tasks.  From the 'engines' docs:

The engines plugin enhances and adds to the suite of default rake tasks for working with plugins. The doc:plugins task now includes controllers, helpers and models under app, and anything other code found under the plugin's code_paths attribute. New testing tasks have been added to run unit, functional and integration tests from plugins, whilst making it easier to load fixtures from plugins. See Engines::Testing for more details about testing, and run 'rake -T' to see the set of rake tasks available.

Here is the custom rake task engines is using:

More information about the Rake-devel mailing list