How to specify working directory in config/database.yml?

Hello folks,

I'd like to use postgresql with unix domain sockets for testinng/development to avoid mismatches when tests from multiple working copies are run concurrently.

I can almost get what I want with the following entry in config/database.yml:

  test:     adapter: postgresql     encoding: unicode     database: testapp_test     username:     password:     host: /home/ruby/rails/testapp/db/pgdata

Unfortunately, the path is specified absolutely here. So when I check out two working copies of my application and run the tests in parallel on both copies, the database will get mangled.

So I am looking for a possibility to specify the directory relatively. Maybe

    host: `pwd`/db/pgdata

or

    host: #{RAILS_DIR}/db/pgdata

or something.

Can this (or something similar) be done? Any suggestions?

Josef Wolf wrote:

Hello folks,

I'd like to use postgresql with unix domain sockets for testinng/development to avoid mismatches when tests from multiple working copies are run concurrently.

I can almost get what I want with the following entry in config/database.yml:

  test:     adapter: postgresql     encoding: unicode     database: testapp_test     username:     password:     host: /home/ruby/rails/testapp/db/pgdata

Unfortunately, the path is specified absolutely here. So when I check out two working copies of my application and run the tests in parallel on both copies, the database will get mangled.

So I am looking for a possibility to specify the directory relatively. Maybe

    host: `pwd`/db/pgdata

or

    host: #{RAILS_DIR}/db/pgdata

or something.

Can this (or something similar) be done? Any suggestions?

Since config/database.yml is read through ERB, you can do stuff like

host: <%= RAILS_DIR %>/db/pgdata

or

host: <%= `hostname` %>

Check vendor/rails/railties/lib/initializer.rb, this is from Rails 2.3:

    # Loads and returns the contents of the #database_configuration_file. The     # contents of the file are processed via ERB before being sent through     # YAML::load.     def database_configuration       require 'erb'       YAML::load(ERB.new(IO.read(database_configuration_file)).result)     end

Josef Wolf wrote:

Thanks for your help, Stephan!

> So I am looking for a possibility to specify the directory relatively. > host: `pwd`/db/pgdata > or > host: #{RAILS_DIR}/db/pgdata > or something.

Since config/database.yml is read through ERB, you can do stuff like

host: <%= RAILS_DIR %>/db/pgdata

Yeah, this works fine when I use RAILS_ROOT instead of RAILS_DIR.

host: <%= `hostname` %>

Backticks don't seem to work here. I get a syntax error when I try it:

** Invoke db:create (first_time)
** Invoke environment (first_time)
** Execute environment
rake aborted!
syntax error on line 22, col 9: ` # port: 5432'
/usr/lib64/ruby/1.8/yaml.rb:133:in `load'
/usr/lib64/ruby/1.8/yaml.rb:133:in `load'
/usr/lib64/ruby/gems/1.8/gems/rails-2.1.1/lib/initializer.rb:716:in ** `database_configuration'

Where line 22 is the first line after the backtick that does _not_ start with a comment.