Rails application testing in Damage Control

October 25, 2005 Link to post  Permalink

The Problem:

I’m setting up a Damage Control server to run tests for my Rails app when the code gets checked in. The default rake task causes the test database to be cloned from the development database. My Damage Control server has no development database, so the whole thing is broken.

Searching the web, this problem is mentioned on Patrick Lenz’s blog but I can find nothing on how to make this work.

Delving into the Rails tasks, the simplest hook place I can see is in the :clone_structure_to_test and :clone_schema_to_test tasks. Each of these has a pre-requisite to perform a dump of the development database before loading that dump file into the test database. If only I could stop the dump from taking place, and use the checked in schema files, my problem would be solved.

It seems that Rake doesn’t have methods to remove pre-requisites from an existing task. Luckily, in ruby-land, I can re-open the required class and add that functionality!

1odule Rake
2  class Task
3    def remove_prerequisite(task_name)
4          name = task_name.to_s
5          @prerequisites.delete(name)
6    end
7  end
8end

To ensure that the rake command (with no parameters) still works on my dev machines, I leave the default task alone and add a new one called dc_build. I’ll tell my Damage Control server to use the command ‘rake dc_build’ and all will be well.

Here’s my dc_build task:

 1esc "DamageControl build task"
 2task :dc_build do
 3
 4  # For the DC build, adjust the pre-requisites to stop the development db dump
 5  Rake::Task.lookup(:clone_structure_to_test).remove_prerequisite(:db_structure_dump)
 6  Rake::Task.lookup(:clone_schema_to_test).remove_prerequisite(:db_schema_dump)
 7  
 8  Rake::Task[:default].invoke
 9end

I put this all in a file called dc.rake in my lib/tasks directory and now I can run my Damage Control without needing a development database.