Virtual hosts with MAMP on Leopard

Assumptions (Kind of major assumptions):

  • You are running Leopard (and therefore cannot use “Netinfo Manager” to edit your hosts).
  • You already have MAMP up and running and MAMP is using the Apache config file located here: /Applications/MAMP/conf/appache/httpd.conf
  1. Set up a directory for your dev website. I created: /Applications/MAMP/htdocs/csimmons
  2. Create a “host” for your site. In Leopard this will require you to edit the hosts file manually. Be sure to use sudo or you will not be able to save the file. Type the following and enter the root password when prompted:
    $ sudo pico /etc/hosts
    Add the following at the bottom

    # VIRTUAL HOST START
    127.0.0.1 csimmons.dev
    # VIRTUAL HOST STOP

    CTRL+O to write the file (then hit ENTER)
    CTRL+X to exit pico
  3. Flush the DNS cache
    $ dscacheutil -flushcache
  4. Edit the Apache config file:
    $ pico /Applications/MAMP/conf/apache/httpd.confChange the following:
    OLD:
    ServerName localhost:8888
    NEW:
    ServerName 127.0.0.1:8888

    Continue to the very bottom of the file (use CTRL+V to page down faster) and you will find “Section 3: Virtual Hosts”. Add the following at the very end:

    NameVirtualHost 127.0.0.1
    <virtualhost 127.0.0.1>
    DocumentRoot /Applications/MAMP/htdocs
    ServerName localhost
    </virtualhost>
    # DEVELOPMENT HOSTS START
    <virtualhost 127.0.0.1>
    DocumentRoot /Users/username/Sites/csimmons
    ServerName csimmons.dev
    </virtualhost>
    # DEVELOPMENT HOSTS STOP

    CTRL+O to write the file (then hit ENTER)
    CTRL+X to exit pico

  5. Restart the MAMP servers. I just click “Stop Servers” and then “Start Servers” on the MAMP widget.
  6. Try it out. Point your browser to the following (no www in the address and don’t forget the port[8888]): http://csimmons.dev:8888
  7. Rinse. Repeat for each of your dev sites. Multiple sites would look like this:

    hosts file:

    # VIRTUAL HOST START
    127.0.0.1 csimmons.dev
    127.0.0.1 site2.dev
    # VIRTUAL HOST STOP

    Apache config file:

    # DEVELOPMENT HOSTS START
    <virtualhost 127.0.0.1>
    DocumentRoot /Users/username/Sites/site2
    ServerName csimmons.dev
    </virtualhost>
    <virtualhost 127.0.0.1>
    DocumentRoot /Users/username/Sites/site2
    ServerName site2.dev
    </virtualhost>
    # DEVELOPMENT HOSTS STOP

Subversion backup of multiple repositories via DOS .bat file

I recently had to implement Subversion at work to manage our source code. Part of that implementation was coming up with a way to automate the backup process. Since developers could be accessing code repositories at any time there is a special command (called hotcopy) in Subversion for copying a repository to another location, which you can then backup to disk, tape, etc.

I started out just doing a simple DOS .bat file to run the hotcopy. My plan was to schedule this to run in the Windows scheduler prior to the nightly backup. As I started working on the script though I found myself trying to improve it to be as generic and hands off as possible. I also wanted to backup multiple repositories.

Here are some “features” of the script:

  • It will hotcopy multiple repositories in a specified directory.
  • It can be run attended or unattended
  • It has some basic log functionality

Here is a look at the actual hotcopy command in the .bat file:
ECHO Starting SVN backup for %%G... >> %repolog% & ^
svnadmin hotcopy %repodir%%%G %repodirhot%%%G --clean-logs >> %repolog% & ^

Download it here and rename it from svn_backup.txt to svn_backup.bat.

A few SQL queries

I had to do some basic analysis of email addresses at my company the other day.  Here are a few useful SQL queries.

Show 10 records with the most data in field ’email’ (10 longest email addresses)

SELECT TOP 10 email, LEN(email) AS fieldLength
FROM person
ORDER BY fieldLength DESC

Count all records with field ’email’ over 20 chars

SELECT COUNT(*) AS emailOver20
FROM person
WHERE LEN(email) > 20

Display the average length of data (in characters) of field ’email’

SELECT AVG(LEN(email)) AS emailAvgLength
FROM person

CFGRID DateFormat

For some reason when you have a datetime field in a cfgrid column it displays in a long format like this: Nov 17 2006 01:00:00

Fig 1 (Below) A date displayed in cfgrid

CFGRID DateFormat Before

If we work a little SQL magic (using “CONVERT”) in the cfquery though we can do the formatting we want, which is: 11/17/2006. Refer to the “styles” listed in Figure 3 to see what formatting is available.

Coldfusion Query:

SELECT note_id,note_text,
CONVERT(varchar,note_date,101) as note_date
FROM tbl_notes
ORDER BY note_date

Fig 2 (Below) A date displayed in cfgrid after using CONVERT in cfquery

CFGRID DateFormat After

Fig 3 (Below) A helpful list of “styles” to use with the SQL CONVERT function
Style ID Style Type
0 or 100 mon dd yyyy hh:miAM (or PM)
101 mm/dd/yy
102 yy.mm.dd
103 dd/mm/yy
104 dd.mm.yy
105 dd-mm-yy
106 dd mon yy
107 Mon dd, yy
108 hh:mm:ss
9 or 109 mon dd yyyy hh:mi:ss:mmmAM (or PM)
110 mm-dd-yy
111 yy/mm/dd
112 yymmdd
13 or 113 dd mon yyyy hh:mm:ss:mmm(24h)
114 hh:mi:ss:mmm(24h)
20 or 120 yyyy-mm-dd hh:mi:ss(24h)
21 or 121 yyyy-mm-dd hh:mi:ss.mmm(24h)
126 yyyy-mm-dd Thh:mm:ss.mmm(no spaces)
130 dd mon yyyy hh:mi:ss:mmmAM
131 dd/mm/yy hh:mi:ss:mmmAM

Prepend existing data in SQL

Disclaimer: This tip will probably be really basic for most SQL folks.

The task: The company I work for has a web application that does some basic tracking of grants. The system feeding information to this application just had an across the board change to the numbering scheme of the grants. All grants must have a prefix of “999-“. Therefore, any grant not beginning with “999-” must be updated. Example: a grant with the current number 8789966 needs to be 999-8789966.

The solution: The SQL below does two things. It updates all the grants to prepend the 999- prefix while also skipping any grants that are already correctly prefixed.


UPDATE tbl_grants
SET grant_no = '999-' + grant_no
WHERE grant_no NOT LIKE '999-%'

Subversion on Ubuntu (Feisty) with a Mac Client

Subversion gets my ducks in a row

As part of a continuous effort to improve my organization (a.k.a. not lose stuff) I have finally set up Subversion at home. If you are not familiar with Subversion it is an open source version control system used (mostly) by developers to keep up with changes to their codebase.

I’ve spent most of my career as kind of a “one man team” where I have been responsible for all phases of development and maintenance of code. However, I was exposed to Subversion when I contracted briefly last year in a multi-developer environment. I made a mental note at that time to revisit Subversion at a later date.

At last that date has come…

Subversion is now running at home on an old Gateway pc that I recently “upgraded” from XP to Ubuntu. I’m a total newbie with Linux, but I’ll be posting some on it in the future. Thanks to this tutorial the process was ridiculously easy (less than 5 minutes).

The next step was to get an SVN client. My previous exposure to Subversion was in the Windows world so we used TortoiseSVN. I have a PowerBook and an iMac at home though so I hunted down scplugin which integrates with Mac Finder the way TortoiseSVN integrates with Windows Explorer.

Now I just have to get all my code checked in.

Comment yer code with myRev Dreamweaver Extension

myRev logo

It’s March and amazingly I have stuck to one of my New Year’s Resolutions for work. What is that resolution you may or may not be asking? …Comment my frigging code.

Several years ago I fooled around with making a few little Dreamweaver Extensions. I pulled out an old one, dusted if off, pimped it up, and am providing it here for your benefit. It’s called myRev and it will insert a comment header for you (ideally you would insert it at the top of the page you are developing). Here is a sample of the output:


<!---
============================================================
File: SomeFile.cfm
Author: Christopher C. Simmons (CCS)
Date: 3.4.2008
Purpose: To perform some calculations
History: 0.1 Initial Release
============================================================
--->

You can use it with Coldfusion, PHP, CSS, JavaScript, or HTML. The header knows what comment style to use based on your choice in the Extension’s UI.

Here’s the fine print…

csimmons.net, LLC supplies this software AS IS and makes no guarantees for your use of it. csimmons.net, LLC is not responsible for any damage or pain the use of this product may cause you.

GET IT HERE (or by clicking the myRev icon above). To install it just unzip it and double click the myRev.mxp file. The Dreamweaver Extension manager will then install it.

I just took a huge cfdump

cfdump top

Har har. Scatological humor and programming.

So I learned something today that I didn’t know even though I have been using Coldfusion for years. It’s kind of embarrassing actually. Everyone has used cfdump a million times to debug query output. Sometimes a query might return many records though and you don’t really need to see them all. Apparently there is a “top” attribute you can use to limit the number of rows returned. Nice.