JRuby – keeping your database.yml really DRY

I am experimenting in JRuby and use database in virtual machine (VMWare Fusion). This is great because you can have multiple versions of multiple databases ready without polluting the notebook. One downsize though is that IP address of the database will change between the VM restarts.

First important setting in Fusion is to use the ‘NAT’ option rather than ‘Bridged’. With NAT, you database host IP address will not follow the network. It was quite annoying to change it from 192.168.X to 192.168.Y everytime I reconnected at home from work or vice versa.

But even with this setting, the IP may change as you restart the VM and you will have to modify the database.yml. To keep the amount of editing minimal, here is my database.yml for the shoplet example:

<% dbhost = 'jdbc:mysql://' %>
common: &common
adapter: jdbc
driver: com.mysql.jdbc.Driver
password: shoplet

<< : *common
url: <%=dbhost%>/shoplet_dev
username: shoplet_dev

<< : *common
url: <%=dbhost%>/shoplet_test
username: shoplet_test

<< : *common
url: <%=dbhost%>/shoplet_prod
username: shoplet_prod

It will work in Ruby as well of course – with different (non-jdbc based) adapter..


Comments are closed.

%d bloggers like this: