Skip to content

Setup PostgreSQL on Mac OS X

MySQL was feeling lonely, so I decided to install PostgreSQL as well:

sudo port install postgresql83 postgresql83-server

Now update your path by adding this line to your .profile:

export PATH=”/opt/local/lib/postgresql83/bin:$PATH”

# the exact order is up to you

I also recommend adding the architecture flag if you use an Intel-based Mac:

export ARCHFLAGS=”-arch i386″

# Don’t forget to restart your terminal or resource .profile

Install the postgres rubygem:

sudo gem install postgres

Create the default database:

sudo mkdir -p /opt/local/var/db/postgresql83/defaultdb

sudo chown postgres:postgres /opt/local/var/db/postgresql83/defaultdb

sudo su postgres -c ‘/opt/local/lib/postgresql83/bin/initdb -D /opt/local/var/db/postgresql83/defaultdb’

To start the server:

sudo su postgres -c ‘/opt/local/lib/postgresql83/bin/postgres -D /opt/local/var/db/postgresql83/defaultdb’

To start the server with a logfile (warning this does not work for me):

sudo su postgres -c ‘/opt/local/lib/postgresql83/bin/pg_ctl -D /opt/local/var/db/postgresql83/defaultdb -l logfile start’

This post was partly inspired by the March 2008 article by ShiftEleven.

GIGO Polling

You’ve heard of the computer science acronym “GIGO”: Garbage In, Garbage Out?  Computers don’t have a monopoly on GIGO; pollsters love it too.  For example, this poll demonstrates the garbage-in principle quite well.

Yes, I am glad that politicians are using online tools to reach out to people. It is high time that issues of energy and transportation are in the public consciousness.

Ok, no more Mr. Rogers. I’m done being nice now. The poll bent me out of shape a little bit. Here’s why:

1. Using a traditional radio button format for a poll is pretty weak in my opinion.  It is a common approach, sure.  But it constrains responses to pre-defined choices. Why not allow free-form text entries? Just an idea, not really a stinging criticism, I admit. No one ever got fired for using a radio-button poll!

2. I don’t like that only one response is allowed. Such a poll reflects a (flawed) mentality that one option is “best” or a “silver bullet.”  We need to wise up and plan for multiple future scenarios.  We need to hedge our bets.

3. The given options are far from comprehensive. Why not also list ideas such as:

  • Implement energy conservation programs
  • Improve our electric infrastructure
  • Drastically reduce our need for gasoline
  • Launch an “apollo program” for solar energy
  • Elect Barack Obama (my favorite)*

4. The poll also presumes that “gas should be affordable.” But that is not what really matters.  What really matters here is making transportation affordable.  We could be more broad — what really matters? Quality of life is what really matters.  Let’s make it affordable and safe to travel between work, home, and third places.  Now we’re talking about urban planning, commuting, mass transit, cycling, and walking.

Conclusion

Energy, economic vitality, and transportation are the core issues here.  Gasoline is only one fuel source; we should not view the debate only the terms of oil.  Let’s put our priorities in the right places. Our leaders should lead by framing questions in ways that elevate the public debate.  The poll above does the wrong thing; it reduces our collective intelligence.

* In case it wasn’t already obvious, let me share my personal biases. I do not see McCain as being a good choice to lead America into the next four years. I want a President who understands problems at their core (i.e. transportation, quality of life, healthcare, and economics) and can apply this understanding to lead and solve the millions of surface problems (i.e. pain caused by the rising cost of oil, lives lost in Iraq, the economic cost of Iraq.)

Understanding TextMate Paths

I recently installed Ruby 1.8.7 in /usr/local. I have Ruby 1.8.6 installed via MacPorts in /opt/local. I couldn’t understand why TextMate’s Ruby bundle was running Ruby 1.8.7 even though my bash .profile was setting PATH correctly; i.e. /opt/local/bin was before /usr/local/bin.

Several people in the #textmate IRC channel helped out, including Allan, the creator of TextMate, the popular text editor for the Mac. These are the must-read resources:

The key thing I learned is that TextMate has two execution contexts: the “bash execution environment” and the “shebang execution environment.” Each context acquires its PATH in a different way.

Examine the PATHs

Here is how you can see the value of PATH for each context:

# bash execution environment
echo “$PATH”
# Highlighting the above line and pressing CTRL-R…
# …will give something like:
/Applications/TextMate.app/Contents/SharedSupport/Support/bin/CocoaDialog.app/Contents/MacOS: /Users/djwonk/bin: /opt/local/bin: /opt/local/sbin: /usr/local/mysql/bin: /usr/local/bin: /Users/djwonk/bin: /opt/local/bin: /opt/local/sbin: /usr/local/mysql/bin: /usr/bin:/bin:/usr/sbin: /sbin: /usr/local/bin: /usr/X11/bin: /Applications/TextMate.app/Contents/SharedSupport/Support/bin

# shebang execution environment
#!/bin/sh
echo “$PATH”
# Highlighting the two above lines and pressing CTRL-R…
# …will give something like:
/usr/local/bin: /Users/djwonk/bin: /opt/local/bin: /opt/local/sbin: /usr/local/mysql/bin: /usr/bin: /bin: /usr/sbin: /sbin: /usr/local/bin: /usr/X11/bin

(Note: I added spaces between : and / purely for blog formatting. That way the lines wrap at sensible places. Your actual results won’t have these spaces, and you shouldn’t have any extra spaces in your PATHs.)

My bash execution environment was fine. However, my shebang execution environment was incorrect: /usr/local/bin was in front of /opt/local/bin.

The Solution

The solution for me was to create the ~/.MacOSX/environment.plist file with the desired path. In my case it was:

{ PATH = “/Users/djwonk/bin: /opt/local/bin: /opt/local/sbin: /usr/local/mysql/bin: /usr/bin: /bin:/usr/sbin: /sbin:/usr/local/bin: /usr/X11/bin”; }

MethodTrails Demo at RailsConf

I just gave a quick talk about MethodTrails at RailsConf 2008.

MethodTrails visualizes the method call graph of Ruby source code. It generates a dot file that can be viewed with Graphviz. It requires Ruby 1.9, which gives you a great excuse to download the latest Ruby and see what it is all about. MethodTrails uses Ruby 1.9’s Ripper to parse any Ruby file into an s-expression. Then it explores the s-expression to find the information it needs to build up the dot file. It does so by using a rule engine, so very little in MethodTrails is hard coded. Tweak it and play around if you want to extract different information.

Here is a sample visualization of Merb’s awesome router (router.rb):

Can Twitter Remain Personal?

One of my favorite things about Twitter is that it is personal.  Sometimes it is funny, sometimes serious. With the people I follow at least, the ethos feels pretty good. However, I’ve recently gotten many twitters in a row (echo chamber style) that are quite clearly all about marketing. Apparently, some of my friends have been hijacked by marketing machines. I hope they come back — I miss them!

I don’t know how other people feel about this, but it seemed like an unwelcome departure from the tone that I’m used to on Twitter. Some of these messages are from people that I respect and like very much. But I don’t like the direction it is going.

The world has, more or less, figured out the norms surrounding email, instant messaging, and IRC. But we have some learning and back-and-forth to do when it comes to Twitter.

I guard my attention. When the noise-to-signal ratio increases, a communication channel becomes less valuable.  When a threshold is crossed, I turn it off. On Twitter, that means that I’m not going to follow people that generate too much noise. Especially if that noise is impersonal marketing.

When Incremental Change Isn’t

At time marker 50:35 in his Git presentation to Google, Linus has a slide that says ”Performance is not secondary… it affects how you work .. and it affects quality.” At 54:50 Linus talks about “the kind of performance that actually changes how you work / it allows you to work in a completely different manner.”

These quotes are in the context of Git, but I think they apply to technology in general. I often think of performance improvements as being linear; it is common to talk about 10% or 20% speed increases; I suspect Git’s performance increases are more on the order of 10x (1000%) for many workflows. These kinds of performance speedups, though impressive, are actually more impressive than they sound!

Linear improvements, in some cases, have non-linear effects on how humans use the technology. The Ruby world has seen a massive migration to Git in the last few months. A significant reason, I believe, is that Git unlocks a new way of working. Another reason is peer pressure (the good kind): Rubyists do not like behind behind the curve.

Using Git for Backup

Sometime last week an idea popped in my head (most likely because I heard it mentioned sometime earlier) — why not use Git for backing up important server files (especially ones that live in /etc). I haven’t tried it out yet, but here are some interesting links that I plan on checking out soon:

 

Improving attr_accessible

In my main project, I have sworn off the use of attr_accessible. It is not that I don’t believe in security. I do. The reason is simple: using attr_accessible makes it hard to use the block forms of new and create with any sort of confidence.

For example, if I use this in my Goal model:

attr_accessible :title, :description

Then it would intercept:

goal = Goal.new(
  :title => “Sample Title”,
  :description => “Sample Description”,
  :uid => “GA-Sample”
)

…and convert goal.uid to nil! This will happen in test code, controller code, model code, anywhere! It seems clear to me that attr_accessible is going too far. I can appreciate how it can prevent a hacker from taking advantage of a situation where you use:

goal = Goal.new(params[:goal])

And that is a good thing. But I don’t want attr_accessible “protecting” me from using the block forms of new and create all the time, especially in tests and models. It seems to me that it should only bust out the security blanket when in controller logic.

This is my main gripe: if I use attr_accessible, I have do mental jumping jacks to remember what attributes I can pass into new and create. If I get it wrong, Rails silently ignores them and oh joy, my cup will soon overfloweth with a veritable cornucopia of hard-to-find errors. If I’m lucky. Thank you sir! — I’ll have another. With friends like that, who needs enemies. No longer and good riddance.

Now, I don’t know about you, but I really like using the block form of new and create. It’s handy and DRY. The alternative is resorting to:

goal = Goal.new
goal.title = “Sample Title”
goal.description = “Sample Description”
goal.uid = “GA-Sample”

That’s just a little ugly and repetitive, not acceptable for a programmer that aspires to laziness.

Could there be a better way that lets us use the block forms of new and create while keeping the benefits of attr_accessible (and attr_protected for that matter)?

A Better API?

Here is a proposed API that I think would preserve the intention of attr_accessible without having it get in your way:

attr_accessible :title, :description

goal = Goal.new_from_params
goal = Goal.create_from_params

Want to call new or create straight from a params hash? Then you would use these methods, which will protect you from stray parameters. But if you want to use the block forms of new and create, you still can, because, in this dreamed up API, attr_accessible won’t get in your way.

In conclusion, I can appreciate how ‘attr_accessible’ intends to make Web applications more secure. I just find that it goes too far, that’s why I suggest this alternative.

(The blog post was inspired by a rant I wrote many months ago at the bottom of some code. I just rediscovered it and found that it still resonated with me. Hopefully the attr_accessible fanboys out there are not too offended.)

Merb’s Style

June 10, 2008 Update: Checkout the merb_param_protection plugin to see a way to handle parameter protection in Merb. Yehuda Katz said that that parameter filtering makes more sense at the controller level instead of the model level. With the plugin, when writing controller code, you can use the regular block forms to instantiate a model, since the controller will filter parameters automatically.

 

 

Larry Page in DC

I heard Larry Page and Michael Calabrese discuss the importance of the wireless spectrum. The New America Foundation organized the event and clearly shares much of Google’s philosophy here.

I find it useful to think about the spectrum allocation issue in terms of famous “tragedy of the commons” parable. If too many devices try to “graze the spectrum” at the same time, they are all made worse off, because they will interfere with each other. Having well-defined property rights is supposed to prevent the over-grazing. Page would say the spectrum is being underutilized: at any one time, only 3% to 5% of the spectrum is being used. [1]

Give the Devices Some Credit

Page said that the FCC’s current style of licensing (by auction) of large blocks of frequencies is a product of the pre-computer days. (Does anybody remember what that was like?) Devices now, he said, are intelligent enough to share the spectrum more efficiently because they can listen before talking. In economic terms, this means that the spectrum can be allocated more efficiently if the FCC moves away from their antiquated system.

In addition to lost efficiencies, Page argued that regulation of various spectrum bands often stifles innovation. He argued that innovation in engineering and computer science happens when these constraints are freed up. I don’t feel that he convincingly argued this point — and I think that other may have agreed, based on a follow up question — Page only gave an anecdotal example of how WiFi got its start on the unlicensed 2.4 GHz spectrum that is shared with microwave ovens.

Page advocated the opening the so-called TV whitespace. He suggested that there is plenty of room there for low-power devices that behave themselves. He credited the FCC as doing a good job in verifying that consumer devices have low interference.

A Jib and A Jab

While plugging Google Android as an example of openness, Page threw a small jab at Sun Microsystems, saying that Google Android is “real” open source, not Sun-style open source.

A larger punch was thrown at the traditional media broadcasters, who he claimed are holding onto their spectrum for their own purposes, not for the interests of Americans in general. In his view, wireless is popular and important; we need to remove barriers that detract from WiFi innovation.

Self-interest?  In business? In DC? No Way. At least Page also admitted that he could be attacked with the same argument, since his position serves the best interests of Google as well! You’ve probably heard the argument — Google benefits from more of almost anything on the Internet — more access, more usage, more documents.

In a sense, Google stands above the fray of some other technology companies (cough, Microsoft) and doesn’t need to lock customers into proprietary standards or operating systems.  In fact proprietary standards mean more work for them; they’d rather have a small number of open, interoperable standards.

Proposed Spectrum Solution

I didn’t see this one coming – Page suggested that government could make a lot of money auctioning off (their unused) spectrum every second. He said to do otherwise means leaving money on the table. Page said that government agencies could still maintain priority for certain frequencies, bumping off others when needed.

The idea of shortening the spectrum ownership periods is interesting; take that to its limit and you would have an auction continuously. Pretty cool! It is not completely surprising that Google, a company that specializes in rapid micro-auctions for their very business model, might suggest a similar idea for provisioning the wireless spectrum. In a discussion afterwards, someone I met speculated that if Google’s $4.6B bid had won, they might have put in place that kind of auction.

Criticism of a Continuous Spectrum Auction

This continuous auction is appealing in theory. However, I can’t help but be skeptical in practice; implementation and enforcement would be tricky. Incentives and enforcement matters. It is one thing to hope that devices cooperate to share the spectrum; it is another thing to setup the carrots and sticks.

With multi-year licenses, it seems relatively more feasible for the FCC to keep tabs. With continuously changing licenses, how would enforcement work? Perhaps the FCC just has to set standards for the device manufacturers. But how much control do manufacturers have over their devices? What is to stop a hacked hand-held device from sneaking into some extra spectrum territory so that it gets faster torrents? The consumers would like that, but it wouldn’t be playing fair, would it?

How do you know when a transgression occurs, and how do you find the guilty party?

My tentative position is this: I don’t see how government, or any organization for that matter, can directly police the spectrum on a year-to-year basis, much less on a second-to-second basis. Other approaches would be needed.  I like to be open minded and so on, but I don’t really see how to get there from here. Help me out if I’m missing something.

Maybe I’m focusing on the wrong thing? I’d like to learn more about some of the proposals being put forward.

So Many Policy Areas, So Little Time

The wireless spectrum (along with Net Neutrality) is another area where I wish I had more time to dig in and become an expert. It aligns well with my engineering and policy backgrounds. I’d be interested to talk more with people on this issue, get in touch if this is an area of interest for you too.

Notes

[1] I don’t have a citation for you.  I’d like to get a clearer explanation of what he means.  I would expect that his baseline is the total available regulated spectrum.) That’s a lot of unused (wasted) spectrum.

Nginx Configuration

The Nginx wiki is helpful and well done. It is the definitive reference, unless you enjoy reading the C source code. Still, I wouldn’t mind finding some more content that explains Nginx. I’m the kind of person that looks for the why behind the how; it just makes me feel more comfortable when I understand the philosophy of a project. I would be stoked to find a detailed write-up that does into some depth about nginx configuration files. Perhaps a chapter in a book, or even just a short article.

Here are some questions that I’d like to get answered about Nginx:

  • Is there a way to turn on debugging to see how nginx proceeds through the location blocks?
  • What is supposed to happen if a location block is nested in another location block?
    • Is this considered a good idea, or is an “if” directive the way to go instead? (I have noticed that I couldn’t do an “include” inside an “if”)

Here are some questions for which I’ve found answers:

  • Nginx gives you great flexibility when it comes to splitting up your configuration files. Deploying Rails Applications by Ezra and co. has some great suggestions on how to do this.
  • Parsing of the configuration file is top down; however, execution of the configuration file depends on some other rules — see the “How nginx Determines Which Path Matches”, part of the explanation of the location directive in the ngx_http_core_module.
  • I have cobbled together some working configurations so that I can run Mint and Wordpress side-by-side with a Rails application, through the use of FastCGI. The Nginx wiki has several FastCGI examples to get you going.