Hoopla!

now with extra whiz-bang!

Hoopla!

Rails Socket Config (Can't connect to local MySQL server through socket)

July 31, 2006 · 2 comments

(Note: this article might be better named “the most complicated database.yml file possible”)

Have you ever accidentally added your database.yml file to subversion or moved a database.yml file to a computer where it doesn’t belong? How about moving your dev server from your laptop to your desktop to your school’s web host to some new install of Suse you’re playing with?

Most of the time this is a big headache. You get errors like the one in the title where the socket was not found. Different flavors of *nix will put the default mysql socket in different places on the machine and it can be a pain to try to keep up with things.

Well, here’s a a database.yml file that can relieve you of ever having to remember which machine you’re on. Many thanks to LazyAtom go gave me the idea for this (who, in turn, got the idea from James Duncan Davidson

Behold – The Code:

login: &login
  adapter:  mysql
  username: user
  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 { |f| File.exist? f }.first %>

development:
  <<: *login
  database: rails_development

test:
  <<: *login
  database: rails_test

production:
  <<: *login
  database: rails

Ta-Da. I just installed this on my dev box and things are working great already.

For those of you who run a staging server to test your app in production, you might want to check out the following setup (I’m seriously considering a staging server setup for my apps. Also, this is good if you’re absolutely dead-set on using the same database.yml file for multiple hosts and multiple configurations (just as long as you know the hostname).

<% host = `uname -n`.strip %>
user: &user
  <% if host.eql? 'staging' %>
  username: staging_user
  password: staging_pass
  <% else %>
  username: user
  password: pass
  <% end %>

login: &login
  <<: *user
  adapter:  mysql
  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 { |f| File.exist? f }.first %>

development:
  <<: *login
  database: rails_development

test:
  <<: *login
  database: rails_test

production:
  <<: *login
  database: rails

If you want to partition the login info for several different users on a large collaborative project you may want to have a special file (ignored by SVN) that holds just the login info for a given user:

login: &login
  adapter:  mysql
  username: user
  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 { |f| File.exist? f }.first %>

# replace the above login details with information from ./config/dblogin.yml
<%= file = File.join(RAILS_ROOT, "config", "dblogin.yml")
    IO.read(file) if File.exist?(file) %>

development:
  <<: *login
  database: rails_development

test:
  <<: *login
  database: rails_test

production:
  <<: *login
  database: rails

→ 2 comments Tags:

Automatically checking for php parse errors

July 27, 2006 · 0 comments

I’ve been running into a problem on one of my multi-developer sites where we’ve been unable to break the habit of occasionally allowing parse errors to show up on one of our php sites. We mean well, we try to avoid it, but sometimes a quick fix is necessary and we don’t always have time to do testing (it should be noted that we don’t have a testing harness of any kind for this site).

Just last weekend I was horrified to find that I’d left a parse error in a small, seldom-visited part of our site for several days. No users encountered it but it prevented some automated tasks from working as they’re supposed to.

It’s become clear that we need more than just to try harder – we need technology to watch our backs. Do accomplish this I’ve developed a bash script that takes directories as arguments and has php parse the contents of every php script in that folder’s structure.

The Code: -
#!/bin/bash

check_dir() {
  for file in $*/*.php
   do
    if [ -d $file ]; then
      echo "going into $file" 
      check_dir $file
    else
      if [ "`php -l $file`" = "No syntax errors detected in $file" ]; then
        echo "valid in $file" 
      else
        echo "$file invalid" 
      fi
    fi
  done
}

for dir in $*
 do
  check_dir $dir
done

It’s very simple – but it’s all that’s required to get started. From here I’m going to replace the echo’ed output with tasks. Specifically, a php file will be run when files are found to be invalid and both an email and an SMS will be sent to the developer’s phones.

We’re setting this script to run every 5 minutes, hopefully no weekends will pass from now on without being error- (at least parse error-) free.

→ 0 comments Tags: