Tuesday, February 17, 2009

If We Love What We Do, Why Are We Trying To Not Do It?

Seems like the Universe wants me to talk about something today, as at least 4 different sources have all touched on the same topic – namely, what is the essence of the “hacker” programmer.  You know what I mean if you read my blog – the passionate, lives and breathes for it, if you didn’t pay him (and even if you do) he’ll do it anyway sort of guy who was born to this stuff, you can see it in his eyes when he talks about it.  Know the type?  Heck, you might be the type.  I’d like to think I am, when the stars align.

Now, here’s the question – we love what we do, right?  We wake up in the middle of the night because we just thought of the fix for a bug, and we have to get up at 2:30 to code it.  If we don’t have enough work at the day job, we come up with projects for ourselves at night.  True?  Or at least, true enough for discussion?

How come we’re also so busy trying to stop doing it?  One of the core principals for many (Ruby on Rails fanatics in particular) is, Don’t Repeat Yourself.  Said another way, “Programmers are lazy, and that’s a good thing.”  We are always on the lookout to refactor some code, or build a tool to get around a job we were doing by hand.  Asked to solve one problem, we imagine 10 variations of the problem and write a program once that will solve all of them, just in case we’re going to be asked.

See the paradox?  On the one hand we love it, but on the other it seems like we spend our days trying to find ways to get around doing it.

So what it is, exactly, that we’re after?  It’s obviously not writing code.  When you refactor, chances are very good that you’re refactoring your own code, so it’s not like you can say “I wrote better code in place of code that sucked.”  Maybe you can, but that’d be because your perception changed, you probably didn’t think the code sucked the first time you wrote it.

If the perfect IDE existed where it wrote the code for you, would you use it?  Or would you want to hack the code generator to see how it worked?  What is the part that we love so much, and how do we know it when we’ve found it?

Thursday, February 12, 2009

Cult Of iPhoneality

There’s a battle going on at my house around my iPhone.  I say that I’m a proud, wired geek who optimizes his use of the technology around him to improve his life.  My wife says I’m addicted to the damned thing.  (Coincidentally, I just got my wife a Netbook for Christmas and she’s recently discovered Facebook….)

So, last night around 2am, one of the kids woke us up.  Wandering back to bed, I tap my iPhone screen to look at the email icon, and I’ve got a message, so I pick up the iPhone to read it.  Just then my wife walks in.  “Talk about addicted!” she says, “It’s 2am, why are you reading your email?”

“Why wouldn’t I?” I counter, as we get into bed.  “It’s information, it’s one finger tap away, now I know what’s waiting for me in the morning.  It’s not like I’m writing back.”

In the dark, there’s a pause, and then I try an experiment.  “You awake?”

“Yes,” she says.

“Want some music?”

“Got any Chicago on that thing?”

I don’t.  But I do have Pandora, so I go to that and fire up a Chicago channel.  A few minutes later she was asleep.

I’ll convert her yet. :)

Tuesday, February 10, 2009

Rails / ActiveRecord : Created_At and GMT (or is it UTC?)

I’ve got a production Rails app (yay!) running side by side with a legacy PHP app, and the model is mixed – my new User object has_many of the old php Account object.    This is fine, they’ve agreed to behave, but there’s a problem.  The created_at and other timestamp info on my Users table is in GMT(*), while the Accounts table is in local.  I need these to be consistent because there’s logic I have to write relative to the creation times (i.e. how long after the user was created, was the account created…).  Is there a configuration available to me in Rails that will force created_at to be local time instead of GMT(*)?

(*) It is actually my own local time (EST) +5.  I keep calling it GMT, but not sure that’s accurate.

UPDATE:  Ok, if I understand this right, it’s using UTC because I don’t have a default timezone set.  I can live with that.  But why, in irb or script/console, if I do “Time.now.zone” does it say EST?  Doesn’t that mean that either a) I do have it set someplace, or else b) Rails is smart enough to figure it out?  Where then is the “no default, therefore UTC” thing being invoked, that I have to change?

UPDATE:  Never mind, I’m an idiot, it’s right there in config/environment.rb:

# Make Time.zone default to the specified zone, and make Active Record store time values
# in the database in UTC, and return them converted to the specified local zone.
# Run “rake –D time” for a list of tasks for finding time zone names. Comment line to use default local time.
# config.time_zone = ‘UTC’

Friday, February 06, 2009

Ever seen a Linux install modified Windows software?

Ok, this is a new one on me.  At work I have a laptop, lid closed, connected to an external monitor.  I run Windows XP, forced to by The Man.  Today I fire up the Wubi installer to grab myself a couple dozen gig of drive space easily, and make a Linux partition.

Everything goes smoothly, I reboot, get an Ubuntu option, and presto I’m in Linux.  First step, naturally, is the whole “get it updated and customized” thing.  As Linux often does on laptop installs, it tells me “The graphic driver is proprietary for your card, do you really want to install it?”  I say yes, what the heck.

Machine installs something, then tells me I have to reboot, no problem.  Monitor doesn’t come back.  Odd.

I open the lid of the laptop to find that it has booted Windows.  Hmmmm, I think – perhaps the dual boot thingie must be done from the main screen?  I reboot, select Ubuntu.  Ubuntu does not come up – halts on bootup.

I reboot into Windows.  And here’s where it gets odd….my second monitor still won’t come up?  I see that a whole new piece of software – the Intel Graphics For Mobile Manager or something – is installed and running on my machine, and it only likes my main laptop screen.

That’s weird.  I did not install that.  My best guess is that the proprietary driver I installed from Ubuntu somehow also installed this thing!?

I uninstall that, and I’m back to normal – I get my second monitor back.

I have not yet gone into the Ubuntu install because I’m at work and can’t waste the productive time.  But has anybody heard of such a thing?  Is it something in the nature of this Wubi thing that I never really left Windows, and it was really installing Windows software even when it told me I was in Linux?

Tuesday, February 03, 2009

How Rails/Migrations Almost Cost Me My Job (*)

(*) Well, yeah, it’s my own damned fault, but still!

We are not a Rails shop, we’re a typical LAMP house.  But we’re trying to do the right thing and bring more Rails into the mix, with the intention of rearchitecting completely that way.  So I had a project that I was helping somebody launch – just a simple maintenance tool, which would live on an internal machine but point to the production database.  Up until this point the project has been pure Rails, developed with sqlite3 and staged with mysql, all moving around with migrations. There’s really only a single table, it just so happens that once it gets to production, this table will live in a legacy database of 50 or so tables that were not produced by Rails migrations.

So, trying to do everything right, we create a special database user just for this case, who can remotely access the production boxes, just from this IP.  We even limit his rights to nothing but create, so I can run the migration.

Migration errors out – we did not give him SELECT privilege, so it cannot execute “show fields”.  Darnit.  I go in, give him SELECT, and run it again.

Now it fails telling me the table already exists.  I go in as root, drop the table, and try again.

Now it fails wondering where the table went.

A whole bunch of stuff happened here, with me trying to figure out how to get free of the “SQL does not jive with what migrations thinks happened” trap.  In the process I grant my user drop privileges, thinking that perhaps if I unwind the migration, dropping the table, that I will then get everything back in sync.  Still does not work.

At this point I find the db:migrate:reset task, which according to the doc “Resets your database using your migrations” something something, it gets cut off.  This translates in my brain as “Gets my SQL and migrations back in line with each other”, so I run it.

And it promptly drops my entire production database.

I am guessing, now that I have a chance to breathe, that there’s some assumption in there that basically says, “The whole database is controlled with migrations, therefore we will drop it all and start over, executing each migration from the beginning.”  Small problem for those of us who only wanted to use migrations to add new tables, not muck with the legacy stuff!  I thought at best it would drop the one table I had asked it to create.

Like I said, it was my mistake – that user should not have had drop privileges, and I should not have been effing around applying commands against production data that I had never used before.  Luckily it is not the end of the world and we’ve resurrected just about everything that was lost.

I am still confused, however, about how I would have properly solved my problem – what do you do when the state of the database is out of line with what the migrations think should be there?  Does the task I was hoping for, that brings the two in line, exist?  Maybe I could make hand modifications to bring the database up to a certain state and then somehow single to Rails, “Ok, assume you have executed migrations up to version X, whether or not you actually did.”