How to make breaking changes and not break all the things

The Problem

Incremental feature development against existing systems frequently involves rethinking and rebuilding existing components. If it’s possible, the easiest way to add support for your new feature is to introduce a new, optional attribute on a model (or in the case of a relational database, adding a NULLable column to an existing table, or adding a new table). Sometimes, though, that either isn’t possible, or wouldn’t be the best design going forward, given your new knowledge of the feature set of the product at hand. You’re left with the uncomfortable realization that the best design would be something completely different that isn’t backward-compatible with current systems.

When there are multiple systems that consume a given data model (say, a front-end webserver, an API, tons of cron jobs, and many backend daemons, like in the case of Twitter’s Ads systems), you can’t just “stop the world,” ALTER your tables, and deploy all the new code. That’s fine if it’s you and another nerd in a garage, but when you’ve got paying customers, you don’t want to give them an excuse to spend their money elsewhere.

What to do?

Read more ›

Posted in Technical HOWTOs

Inspecting ActiveRecord-generated queries

For the vast majority of rails CRUD applications, you aren’t going to need exotic SQL queries, but when you need to make assertions on the generated query, there are two options at your disposal.

Assume you have the following model:

Read more ›

Posted in Technical HOWTOs Tagged with: , ,

How to test your rails application with Travis CI on different databases engines

Travis CI is an awesome continuous integration service that’s free for open-source projects.

If you’d like to test your app against multiple database engines, it’s fairly simple.
For these examples, I’m testing my app against SQLite, MySQL, and PostgreSQL.

Read more ›

Posted in Technical HOWTOs

How to deal with Amazon’s “requested Availability Zone is no longer supported” error

In shutting down the AdGrok servers (talk about bittersweet…), I stopped the instances, but then remembered I wanted to shred the files first, so I clicked “start,” and was greeted by the following error:

The requested Availability Zone is no longer supported. Please retry your request by not specifying an Availability Zone or choosing us-west-1b, us-west-1c.

Read more ›

Posted in Technical HOWTOs Tagged with:

It really is rocket science!

This last Friday I taught my “it really is rocket science” class to another hundred children (Kindergarten through 5th grade). Last year PTO Today wrote an article about Arts and Science day, and interviewed me. This year, the kids were great, I had tons of help, and the new rocket launcher designs let the kids experiment with different pressures and different launch angles. Good stuff!

Posted in Technical HOWTOs Tagged with: ,

How to fix “rake/rdoctask is deprecated. use rdoc/task instead”

Seeing this?

Edit your Rakefile and change these lines:

to look like this:

You may need to add gem 'rdoc' to your Gemfile, too. While you’re at it, you might want to add rdoc/ to your .gitignore, too

Posted in Technical HOWTOs Tagged with: ,

How to wrangle the pg (postgresql) gem with macports and rvm

Seeing this?

Wondering how or where to get this mythical pg_config? It’s part of the PostgreSQL package. If you’re running MacPorts, it’s easy:

Then, following the instructions on the RVM website, run

Posted in Technical HOWTOs Tagged with: , , , ,

HOWTO: Fix git-gui’s “Spell checking is unavailable” dialog

Launching git gui from the command line, when built with MacPorts, causes an irritating dialog to pop up:

The solution is simple:

Posted in Technical HOWTOs Tagged with: ,

Creating acts_as_… gems for Rails 3.1.x

Update January 2012: since originally writing this post, I’ve recanted my love for rails plugins — having a stubbed rails app in your plugin’s test directory is just too funky. If you need an example of a rails 3.2.x plugin that uses rspec 2 and has Travis CI integrated, check out closure_tree. But even that setup feels to heavyweight.

The original post follows.

There are a lot of posts on how to build rubygems. With Rails 3.1, though, they’re all old and busted.

The new hotness is built right into rails now. Just incant

You’ll get:

  • a .gemspec and Gemfile to get started
  • a dummy rails app to integration test your new gem against
  • a (deprecated) RDoc rake task

When you run rake, you’ll see “rake/rdoctask is deprecated. Use rdoc/task instead (in RDoc 2.4.2+)”. To remedy, follow these steps.

The Edge edition of the Ruby on Rails Guides has more information.

Posted in Technical HOWTOs Tagged with: , ,

Hierarchical Tagging with Rails 3 and Closure Trees

In rebuilding PhotoStructure on Rails, I was surprised that one of the most popular gem for trees used a nested set model. Nested sets are performant for reads, but for adding and deleting nodes, it’s extremely expensive — on average, half of the rows for your model acting as a nested set have to be updated for every add or delete. That’s crazy-talk! It requires a table-level lock for something that, at least for PhotoStructure, is a very common task. To top it off, the gem doesn’t even do the lock!

I then found the ancestory gem, which works by materializing the ancestral path as a string and storing that as a column. Bill Karwin’s excellent Models for hierarchical data presentation describes this as the Path Enumeration algorithm, which doesn’t have referential integrity, and relies on performant LIKE selects.

As the tag hierarchies in PhotoStructure are never moved, closure trees should prove to be ideal. It’s an excellent excuse to learn how to build and publish a rails plugin gem, so I’ve built a new gem to support closure trees, and I named it “closure_tree”. Check it out at:


Update, May 24

1.0.0.beta1 released. All public methods have test coverage.

Update, May 25

1.0.0.beta2 and 1.0.0.beta3 were released, which added find_or_create class and instance methods, ancestry_path, and root instance methods. Documentation is on the README and in the rdocs.

Update, May 29

1.0.0.beta5 is released, which cleaned up the ancestor and descendant relationships to use has_and_belongs_to_many, and adds leaves class and instance methods.

Update, Oct 26

2.0.0.beta1 is released, which added the :dependant option, switched from an embedded dummy rails app for testing to rspec, and much better test coverage (including tests and fixes for a couple reported issues) under sqlite, MySQL, and PostgreSQL.

Update, Nov 27

3.0.0 is released, which supports polymorphic trees.

Update, June 2014

BOOM. Lots of stuff since the last update!

  • support for concurrency, 5 rubies, and all current rails versions
  • more than 20 contributing engineers
  • 65 released versions in total
  • more than 80,000 downloads
Posted in Technical HOWTOs Tagged with: , , ,