DRYing up your database config written by: Lee Richmond posted on: 11 January 2010

One of the things that I love about Ruby on Rails is that it allows you to implement the DRY andREST principles with a lot of ease.

As anyone who has looked at Ruby on Rails knows the database config file (database.yml) is one place that could do with DRYing up for instance here is how a standard database.yml might look:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

development:
  adapter: mysql
  encoding: utf8
  database: myapp_development
  username: root
  password: mypassword
  pool: 5
  socket: /var/run/mysqld/mysqld.sock

test:
  adapter: mysql
  encoding: utf8
  database: myapp_test
  username: root
  password: mypassword
  pool: 5
  socket: /var/run/mysqld/mysqld.sock

production:
  adapter: mysql
  encoding: utf8
  database: myapp_production
  username: root
  password: mypassword
  pool: 5
  socket: /var/run/mysqld/mysqld.sock

As you can see this isn't exactly DRY, so here is my solution to this problem:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

defaults: &defaults
  adapter: mysql
  encoding: utf8
  username: root
  password: mypassword
  pool: 5
  socket: /var/run/mysqld/mysqld.sock

development:
  database: myapp_development
  <<: *defaults

test:
  database: myapp_test
  <<: *defaults

production:
  database: myapp_production
  <<: *defaults

as you can see this solution is much DRYer and in my opinion much easier to work with.

comments

Be the first to comment on this article
this will not be published (include http://)

Rss RSS Atom ATOM

<<back to articles

latest articles

categories

Follow_me Recommend Me Geekup-logo