(Relatively) Hassle-Free Rails DB Configuration

05 Jul 2006, 02:52 »

(While this has been covered elsewhere fairly recently, I'm posting this here so I don't forget it. The principle is basically the same as JDD's, except neatened up slightly)

With all the different systems that I potter around with Rails on, it can be a pain having to tweak database.yml each time you check your app out onto a different development system. It's time to be a proper programmer, end the drudgery1 and solve this issue:

common: &common
  adapter: mysql
  username: root
  password:
<% socket =  ['/opt/local/var/run/mysql5/mysqld.sock', # darwinports
              '/opt/local/var/run/mysqld/mysqld.sock', # darwinports, again
              '/var/run/mysqld/mysqld.sock',           # ubuntu/debian
              '/tmp/mysql.sock'
             ].select { |socket_location|
                File.exist? socket_location
             }[0] 
   if !socket.nil? %>
  socket: <%= socket %>
<% end %>

development:
  <<: *common
  database: engines_development

test:
  <<: *common
  database: engines_test

Some notes:

  • On your production database, your login details better not be root/empty_password. Common sense, really.
  • I've tried to cover the socket locations that I bump into normally, but you might have a different one. Obviously, just add that to the list.
  • I wonder if there's some way to transparently hack the app generator to use this...

1: Yes, I'll admit it. Most of my programming stems from the fact that I'm lazy and don't want to repeat things. Computers are our slaves! Huzzah!

© All content is available under the creative commons, except where I didn't write the stuff, in which case you'll need to track down the original author. I'm notoriously bad at citing references. Needless to say the text here is not guaranteed to reflect my opinions, or indeed the opinions of any sane person. Developed on a Mac.. Powered by PURE EVIL.