Separate databases for each git branch in a Rails project

January 29, 2013 by alex


Git makes it easy to branch your code. I love this. But when a branch needs migrations, switching back to master can be difficult. Just checking out the master codebase isn't enough - you need to also spin up a database with the schema expected by master.

If you have no data worth keeping, simply running rake db:schema:load after checking out a different branch is fine. If that doesn't work for you, read on.

Here's the solution I've been trying out and which seems to be working quite well.

# config/database.yml
development:
  adapter: mysql2
  encoding: utf8
  reconnect: false
  database: project_development_<%= Git.open('.').current_branch %>
  pool: 5
  username: username
  password: userpassword
  host: 127.0.0.1

database.yml is automatically parsed for ERB snippets, so this just works without any extra setup. I'm using the git gem to figure out what branch I'm currently in, and select the right database accordingly.

You can see the databases I've set up to follow this convention in mysql.

mysql> show databases;
+-----------------------------------+
| Database                          |
+-----------------------------------+
| information_schema                |
| project_development_master        |
| project_development_experiment    |
| project_development_bugfix        |
+-----------------------------------+
4 rows in set (0.00 sec)

I think I want to add a feature/convention to allow me to keep using the master db on certain branches. (This will save setup time for branches which I know aren't going to have any migrations.) Otherwise, that's about it.

☙ ☙ ☙

I'm a published author.

December 10, 2012 by alex


I contributed to a few chapters in O'Reilly's recently-published book "Monitoring With Ganglia". This is a big first for me, and I'm pretty proud of it. Anyone who's interested in Ganglia (and really, who isn't?) should pick up a copy.

All proceeds are being donated to Scholarship America, which is a charity selected by the lead authors.

☙ ☙ ☙

Learning a little R

October 10, 2012 by alex

What's a pirate's favorite programming language? R! (Groan all you want. It only encourages me.)

Over the past few years, I've made a few half-hearted attempts to learn the R programming language. I made a little progress, but never really felt like I understood what I was doing. The language very alien compared to other languages I was used to, and its problem domain (statistics) was one I didn't have much familiarity with.

I still don't really know any statistics, but thanks to a few years with Ruby I understand functional programming a bit better than I used to. This definitely has made a big difference with R.

I decided to try a Coursera class on R, both to see how Coursera works and (hopefully) to get over the hump and really learn enough R that I can use it daily. Since I work with lots of data, I expect I'll find plenty of places where it's helpful.

Coursera seems well suited for this kind of learning. I have a clear goal in mind, and I'm motivated enough to do the work without anyone hounding me. Please note that neither of these things were true for 18-year-old Alex, so I don't see Coursera as a replacement for a normal college in any way, shape or form. But, the range of things you can learn on Coursera is pretty impressive, as are the instructors.

So far the R class is living up to expectations. One recent programming assignment:

Write a function that takes a directory of data files and a threshold for complete cases and calculates the correlation between sulfate and nitrate for monitor locations where the number of completely observed cases (on all variables) is greater than the threshold. The function should return a vector of correlations for the monitors that meet the threshold requirement. If no monitors meet the threshold requirement, then the function should return a numeric vector of length 0.

Whew, that's a mouthful. I had to read that over several times before it started to make any sense, but it did eventually. The most impressive thing is that the solution is something like 4 lines of code. And, after a few hours of hacking, I think I actually understand the code I wrote. :)

☙ ☙ ☙

Animating geo data using ruby-processing

September 28, 2012 by alex

A few weeks I wrote about experimenting with Processing for the first time. I'm pretty happy about how that project has progressed since then.

I synthesized some TED talk views data and an IP address geolocation database to produce this:

It was fun to do, and I'm pretty happy with the end result. Switching from processing to ruby-processing was a huge win - I'm far more comfortable with Ruby and got a lot more done with less hassle. I kept dreaming up features & adding them without too much trouble. (The sparkline in the corner took less than an hour to add.)

This thing got featured on the TED blog, which is very nice. The code is on GitHub if you want to see how it works under the hood.

☙ ☙ ☙

What is TED?

September 24, 2012 by alex


Us remote TED employees were asked to make a short video answering the question "What is TED?" for the yearly company meeting.

I had a little fun with my Makey Makey. Here's what I came up with.

☙ ☙ ☙