# Database configuration across developers!

**URL:** <https://www.ruby-forum.com/t/database-configuration-across-developers/101221>\
**Category:** Rails\
**Created:** [August 3, 2006, 5:16pm UTC](https://www.ruby-forum.com/t/database-configuration-across-developers/101221 "2006-08-03T17:16:20Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![matthew\_d](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/48db29/32.png) [@matthew\_d](https://www.ruby-forum.com/u/matthew_d)\
**Post date:** [August 3, 2006, 5:16pm UTC](https://www.ruby-forum.com/t/database-configuration-across-developers/101221/1 "2006-08-03T17:16:20Z")

</div>

Hi,

I have what might be a silly question but is there any way to put some  
of the database configuration information into a separate file? For  
instance, in config/database.yml I have:

developer\_database: &developer\_database  
adapter: mysql  
username: me  
password: something  
host: 127.0.0.1

development:  
\<\<: \*developer\_database  
database: foobar\_development

test:  
\<\<: \*developer\_database  
database: foobar\_test

This was done so that the database names are standardised across  
developers but the details of the actual database connection are  
modifiable by the developer (another developer will alter _just_ the  
details in developer\_database).

What I’d like is to have the ‘development’ and ‘test’ entries in  
config/database.yml (which is checked into a SVN repository) and then  
the ‘developer\_database’ defined in a config/developer\_database.yml  
file (which is not checked in); both of these being loaded and  
“merged” to get the appropriate database configurations.

Does that make sense? How else do people deal with this?

Matt

---

<div class="post-metadata">

**Author:** ![matthew\_d](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/48db29/32.png) [@matthew\_d](https://www.ruby-forum.com/u/matthew_d)\
**Post date:** [August 3, 2006, 5:25pm UTC](https://www.ruby-forum.com/t/database-configuration-across-developers/101221/2 "2006-08-03T17:25:22Z")

</div>

Matthew D. wrote:

> host: 127.0.0.1  
> developers but the details of the actual database connection are
> 
> Matt
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

database.yml is parsed by ERb before yaml loads it. You can just do  
this:

\<%= File.read(“dev\_db.yml”) %\>

development:  
\<\<: \*developer\_database  
database: foobar\_development

test:  
\<\<: \*developer\_database  
database: foobar\_test

production:  
adapter: mysql  
username: etc  
password: etcagain  
blah: blah

–  
Ola B. ([http://ola-bini.blogspot.com](http://ola-bini.blogspot.com))  
JvYAML, RbYAML, JRuby and Jatha contributor  
System Developer, Karolinska Institutet ([http://www.ki.se](http://www.ki.se))  
OLogix Consulting ([http://www.ologix.com](http://www.ologix.com))

“Yields falsehood when quined” yields falsehood when quined.

---

<div class="post-metadata">

**Author:** ![matthew\_d](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/48db29/32.png) [@matthew\_d](https://www.ruby-forum.com/u/matthew_d)\
**Post date:** [August 3, 2006, 5:25pm UTC](https://www.ruby-forum.com/t/database-configuration-across-developers/101221/3 "2006-08-03T17:25:22Z")

</div>

You can approach this a couple of ways. The database.yml file can hold  
real ruby just like the other yml files so there are a couple of ways  
to fix your problem:

1. Put a file in a standard directory outside the svn repository and  
have it read in.
2. Use environment variables

I think I like the environment variables approach better since that is  
how other “system” settings are set (like whether or not the system is  
in production, test or development modes).

---

<div class="post-metadata">

**Author:** ![matthew\_d](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/48db29/32.png) [@matthew\_d](https://www.ruby-forum.com/u/matthew_d)\
**Post date:** [August 4, 2006, 5:28am UTC](https://www.ruby-forum.com/t/database-configuration-across-developers/101221/4 "2006-08-04T05:28:42Z")

</div>

On 3-Aug-06, at 8:04 AM, Matthew D. wrote:

> Hi,
> 
> I have what might be a silly question but is there any way to put some  
> of the database configuration information into a separate file? For  
> instance, in config/database.yml I have:

Splitting out the config into separate files seems overly complicated.

In all of my projects I simply have a database.yml-sample file  
checked into svn and an svn:ignore set on database.yml. Each  
developer simply copies database.yml-sample to database.yml and edits  
it for their local environment.

## HTH, Trevor

Trevor S.  
[http://somethinglearned.com](http://somethinglearned.com)

---

<div class="post-metadata">

**Author:** ![matthew\_d](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/48db29/32.png) [@matthew\_d](https://www.ruby-forum.com/u/matthew_d)\
**Post date:** [August 4, 2006, 6:49am UTC](https://www.ruby-forum.com/t/database-configuration-across-developers/101221/5 "2006-08-04T06:49:51Z")

</div>

On Aug 3, 2006, at 11:15 AM, Ola B. wrote:

> > host: 127.0.0.1  
> > What I’d like is to have the ‘development’ and ‘test’ entries in
> 
> \<\<: \*developer\_database  
> JvYAML, RbYAML, JRuby and Jatha contributor  
> System Developer, Karolinska Institutet ([http://www.ki.se](http://www.ki.se))  
> OLogix Consulting ([http://www.ologix.com](http://www.ologix.com))
> 
> “Yields falsehood when quined” yields falsehood when quined.

Or let the defaults stay in the database.yml, but allow an override  
in a separate file that isn’t kept in the repository:

# vvv Copy the following two YAML maps to a file called config/

mydatabase.yml

login: &login  
username: default\_user  
password: default\_password

connection: &connection  
host: 127.0.0.1  
port: 3306

# ^^^ Copy the previous two YAML maps to a file called config/

mydatabase.yml

\<%= file = File.join(RAILS\_ROOT, “config”, “mydatabase.yml”)  
IO.read(file) if File.exist?(file) %\>

development:  
adapter: mysql  
database: app\_development  
\<\<: \*login  
\<\<: \*connection

The I can override the connection from TCP to, say, Unix socket where  
I need to. I haven’t yet had to replace the adapter in this way, but  
the same holds true. You could even leave the production as:

production:  
adapter: mysql  
database: app\_production  
username: production\_user  
password: production\_password  
host: localhost  
socket: /tmp/mysql.sock

But I think it’s a _good thing_ to keep the real user/password out of  
the repository completely.

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![matthew\_d](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/48db29/32.png) [@matthew\_d](https://www.ruby-forum.com/u/matthew_d)\
**Post date:** [August 4, 2006, 3:18pm UTC](https://www.ruby-forum.com/t/database-configuration-across-developers/101221/6 "2006-08-04T15:18:18Z")

</div>

On 8/3/06, Matthew D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I have what might be a silly question but is there any way to put some  
> of the database configuration information into a separate file?

Thanks for all the answers. I answered it myself about 10 minutes  
after sending the email (I use the ERb idea) which just goes to show  
that it should be a “measure twice, cut once” sort of world!

Matt
