Created
December 20, 2012 16:49
-
-
Save robworley/4346649 to your computer and use it in GitHub Desktop.
Decrease setup time when using parallel_tests with rails, postgres and rspec. Significant perf gains when using the SQL schema format or heavyweight custom prepare steps.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace :parallel do | |
task(:prepare_pg, [:count] => "db:test:prepare") do |t, args| | |
num_processes = ParallelTests.determine_number_of_processes(args[:count]) | |
original_test_env_number = ENV['TEST_ENV_NUMBER'] | |
test_db_name = ActiveRecord::Base.configurations['test']['database'] | |
Postgresql.drop_connections(test_db_name) | |
db = ActiveRecord::Base.connection | |
(1..num_processes).each do |test_env_number| | |
next if test_env_number == 1 # First process uses the main test database. | |
# Determine the DB name for the parallel process. | |
ENV['TEST_ENV_NUMBER'] = test_env_number.to_s | |
db_config_template = ERB.new(File.read(File.join(Rails.root, 'config', 'database.yml'))) | |
db_config = YAML.load(db_config_template.result) | |
ENV['TEST_ENV_NUMBER'] = original_test_env_number | |
parallel_test_db_name = db_config['test']['database'] | |
# Copy the (already prepared) test database. | |
puts "Copying #{test_db_name} to #{parallel_test_db_name}" | |
db.drop_database(parallel_test_db_name) | |
db.create_database(parallel_test_db_name, :template => test_db_name) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
a nice 1-off solution -> definetely add it to the wiki, but I think it does not look stable enough for general use / other databases