views:

26

answers:

1

I have two Rails applications (lets call them APP-1 and APP-2), each of them has a dependancy on a third Rails application (APP-3).

I would like to be able to run the tests for APP-1 and APP-2 in parallel on my CI server. The problem is, both need to start up APP-3 and write to a DB via the APP-3. This causes conflicts and failures if the tests are run in parallel.

My idea for a solution is for APP-1 and APP-2 to each start their own instance of APP-3 and to have each instance point to a different DB. Is there a way to dynamically set the DB in the database.yml of APP-3 so that it connects to a different DB depending on which APP starts it up?

FYI. APP-1 and APP-2 currently start APP-3 via rake tasks.

+2  A: 

Make two database.yml files for App-3. Call them database_A.yml and database_B.yml or whatever you want.

In your environment.rb file for App-3, add the following toward the end of the Initializer block.

Rails::Initializer.run do |config|
  # ...

  # Decide what database_FOO.yml file you care about.
  #
  db_config_file_name = "database_A.yml"  # Put your logic here for choosing which yml file you want.

  db_config_file_path = File.join("config", db_config_file_name)
  config.database_configuration_file = db_config_file_path if File.exists? db_config_file_path
end

I pulled this from my blog, where I describe how I use a different database.yml file when I launch my app with JRuby. The concept is similar, so hopefully you find this helpful. http://www.workingasintended.com/2010/05/03/choosing-a-different-rails-databaseyml-file-for-jruby/

jdl