Wednesday, December 8, 2010

Creating/Deleting local and remote branches with git

In the course of my git-based workflow, more often than not, I use branches like this:

  • Create a new local branch starting from the HEAD of my current working branch.
  • Immediately push it upstream to the remote origin so that it exists in both places.
  • Set the local branch to track the remote branch.
  • Commit/push/repeat
  • Merge the branch back into master (or wherever it started from)
  • Delete the local branch.
  • Push to delete the remote branch.
Since this seems pretty common, and some of the commands to do this are a bit difficult to remember sometimes, I created two simple scripts called git-branch-create and git-branch-delete to help with this. Now my workflow looks like:

  • git branch-create feature_branch
  • Commit/push/repeat.
  • Merge branch back into parent branch.
  • git branch-delete feature_branch

To use these scripts via the above commands, download the scripts above and place them somewhere in your path. I even created short aliases for them like so:


Saturday, December 4, 2010

Installing curb gem on Mac OS X 10.6 (Snow Leopard)

If you have previously installed curl with something like:

    sudo port install curl +ssl

and then try to install the curb gem like:

    sudo gem install curb

and you find yourself getting an error that looks something like this:


In file included from /opt/local/include/curl/curl.h:44,
from curb.h:12,
from curb.c:8:
/opt/local/include/curl/curlrules.h:144: error: size of array ‘__curl_rule_01__’ is negative
/opt/local/include/curl/curlrules.h:154: error: size of array ‘__curl_rule_02__’ is negative
lipo: can't open input file: /var/folders/wX/wX64Cb+PGjG-EXuklO+I+k+++TI/-Tmp-//ccKIrqTY.out (No such file or directory)
make: *** [curb.o] Error 1

Then, I think I have a solution for you. Change your curb install command to this instead:

    sudo env ARCHFLAGS="-Os -arch x86_64 -fno-common" gem install curb

It seems a number of people on the web suggest re-installing curl as a universal binary using something like:

    sudo port install zlib +universal
sudo port upgrade --enforce-variants openssl +universal
sudo port install curl +universal

I do not like this approach. The original problem is that you installed an x86_64 version of curl (which is the default, as it should be) but you are trying to install a universal version of curb (which is unfortunately the default). Instead of re-installing curl to be universal, which is wasteful and takes forever, install curb using the x86_64 flags to build it. This is what you want, it works, and it only takes a few seconds.

Tuesday, September 21, 2010

Huge speed gain migrating large tables in Rails

Have you ever needed to make multiple column adds/removes/changes to a large table in Rails in a single migration? If so, you've probably noticed that each of these individual changes issues a single ALTER TABLE command to the database, which can take a long time on a large table. At least with MySQL, combining these all into a single ALTER TABLE dramatically reduces the amount of time it takes for your migration to run.

Rather than write custom SQL for this, the folks at XING have created a very cool Rails plugin called alter_table. Their blog post, Alter Table Rails Plugin, gives more background on this. Check it out, it's a good, quick read.

One thing they didn't mention in their post was what kind of performance gains they observed. I was curious, so I did a quick-and-dirty experiment.

Consider an existing table named "songs" that has 330K records in it. First, I tried just adding a column named "foo" using the normal migration style we're all accustomed to:


On my MacBook Pro, this is what the timings for this looked like:


Now, after rolling that back, installing the alter_table plugin (as per their instructions), I rewrote this migration to use the new alter_table method:


Now, on the same machine, this migration takes roughly half the time because it does all the alterations in a single pass. Here are the new timings:


In real life, I had a table with almost 10 million rows that I needed to do 10 alterations to. Using this plugin, I cut my migration time down by almost 10x! If you find yourself doing more than one alteration on the same table in a single migration, I highly recommend checking out alter_table. You can find their source code at: