2

My app has what is effectively a constant database table. It's big -- it contains ~125K rows of 14 fields -- and rebuilding it takes about five minutes.

For development and production work, I rebuild as part of seeding the database. But for testing, I don't want to wait five minutes every time I run the tests.

What technique or workflow would you suggest in this case? (One approach might be to alias development_constant_table as test_constant_table for the duration of the tests, if that's possible.)

update

I should mention that db/seeds.rb file looks something like this:

# file: db/seeds.rb
require "#{Rails.root}/db/time_dimension_loader"
TimeDimensionLoader.perform_lengthy_table_creation
fearless_fool
  • 33,645
  • 23
  • 135
  • 217
  • Can you clarify what a "rebuild" is? Is the table being generated programatically, or are you reading it from a YML (or whatever) seed file? – Xavier Holt Aug 23 '12 at 22:59
  • 1
    Have a look at this, http://stackoverflow.com/questions/1574797/how-to-load-dbseed-data-into-test-database-automatically looks like you need to seed for production and development and only do it in test if the table is empty. New to this stuff myself, but I'd be interested in a slution as well. – Tony Hopkinson Aug 23 '12 at 23:07
  • The table is built programmatically. (FWIW, it's essentially a calendar that maps DateTime to day of week, holiday, weekend and other useful fields.) – fearless_fool Aug 23 '12 at 23:07
  • @TonyHopkinson: that's exactly the pointer I needed. I've expanded on the answer below. – fearless_fool Aug 23 '12 at 23:28

1 Answers1

3

Okay, Tony Hopkinson's pointer to stackoverflow.com/questions/1574797/ pointed the way.

First, create a rake file:

# lib/tasks/test_seed.rake
namespace :db do 
  namespace :test do
    task :prepare => :environment do
      Rake::Task["db:seed"].invoke
    end
  end
end

Then when you invoke the standard rake db:test:prepare, your new rake task gets executed after the standard db:test:prepare runs. So yes, it takes a while to run the rake task, but thereafter the table is present (and persists) so you don't have to rebuild the table every time you run RSpec or autotest.

fearless_fool
  • 33,645
  • 23
  • 135
  • 217
  • Have a plus, useful thing to know that – Tony Hopkinson Aug 24 '12 at 01:37
  • @TonyHopkinson: it took a bit of digging to figure out what was really going on in the SO post that you referenced, but ultimately it was the answer. You deserve the credit. – fearless_fool Aug 24 '12 at 16:30
  • You did the work, so accept your answer. As well getting the points, others with a similar issue (like almost certainly me at some point) will get straight to known working solution. – Tony Hopkinson Aug 24 '12 at 23:47