All posts by shannah

Steve Hannah is a 28-year-old software developer currently studying and working at Simon Fraser University in beautiful Vancouver British Columbia. He specializes in web information systems and prefers Java, PHP, and Python as programming languages. He is a Christian and worships at Christ Church of China in Vancouver.

Application Versioning & Synchronization with Xataface

One of the more annoying challenges involved with managing production web applications is keeping the development and production versions in sync. Verson control systems such as Subversion and CVS make this task trivial for source code and file system changes, but changes to the database schema between versions still need to be handled with care, as these changes fall outside the watch of any version control system.

For example, suppose I am running an application that stores user profile information, and I want to add a column to the “users” table to store the user’s postal code. I add the column to my development database but I don’t want to add it to the production database until I am finished with the rest of my changes.

The old way: Copy & Paste – text files

The old way managing these changes was to make the change in the development database, then copy and paste the SQL query that I used to perform the update into a text file. I would repeat this process for each change that I made. When it came time to move the changes to the production application, I would just execute these statements manually one by one on the production server.

The down-side of this approach is that it didn’t scale very well. It works OK if I only have one production installation and one development server. But what if I have dozens of production servers all running the same application, and perhaps running different versions. It would become cumbersome if not impossible to keep track of all of these changes and manually apply them across all installations.

The new way: Xataface Application Versioning

Xataface allows you to track the version of your application with a text file named version.txt stored in your application’s directory. This file should contain one line like with two numbers separated by a space:

1.0b1 345

This example means that the application version is 1.0b1, and that the build version is 345. The build version must be an integer that is incremented every time there is a change to the source code. It is used by Xataface to figure out whether the file system version matches the database version. A good practice is to just use the SVN revision number for the build version.

On every page request, Xataface checks the version.txt file to see what version of application is currently in the file system. It compares this with the version of the database. If the database version is lower, it will execute the necessary queries to update the database to the current version.

The conf/Installer.php file

Xataface looks for a class named conf_Installer located in your application’s conf/Installer.php file to find out what it needs to do to update between versions. You can define methods in this class of the form:


function update_##(){}

Where ## is the build number of the update.

Xataface will execute all functions update_XX() to update_YY() in your conf_Installer class automatically if it finds that the database version is XX and the filesystem version is YY. This is where you can place your database updates that need to be performed between versions.

For example, suppose the production server is running build version 345. That means that the version.txt file in your production server might look something like:

0.5.1 345

Now you want to add a postal_code column to the users table in the development version, so you’ll increment the version number on the development server:


0.5.2 346

And add a method to your conf/Installer.php file to perform the database change:

<?php
class conf_Installer {
  function update_346(){
    $sql[] = 'ALTER TABLE `users` ADD `postal_code` VARCHAR(32) AFTER `phone_number`';
    foreach ($sql as $q){
      mysql_query($q, df_db());
    }
  }
}

Then you can just update the source files to the production server using subversion. The first time you run the production app after updating the source files you’ll get a message saying that the application has been updated to version 346.

That’s all it takes. You just keep on adding these methods for each update. Then even if you have an instance that is a couple of versions behind, all you need to do is update to the latest source revisions, and it will automatically update the database to the correct version.

Replacing Scriptaculous/Prototype with jQuery

I have used Scriptaculous in the past to sprinkle little bits of UI magic into Xataface. Specifically, I have used it to add collapsible sections, sortable sections (via drag-and-drop), and sortable tables (also via drag and drop). These worked great! The Scriptaculous library was a bit bulky and it made the initial page load time a little bit longer, but the result was worth it.

Unfortunately I have started to run into problems with Scriptaculous interfering with other scripts on the page. Scriptaculous is built on the Prototype.js library which adds a number of handy methods and attributes to the built-in javascript types, like objects, arrays, DOM Elements, and strings. As a proof of concept, this is great as it shows off the dynamic features of the javascript programming language. However this can cause problems with scripts that count on the results of the default behavior of these built-in types.

For example, I have made use of Kevin van Zonneveld’s php.js library which provides pure javascript implementations of familiar PHP functions. One such function is count() which is supposed to return the number of elements in a PHP array. In Javascript, this function can either take objects or arrays as a parameter in order to provide the closest possible behavior to its PHP counterpart. Essentially, all this function does is count the number of elements in the array (or object) and return the result as an integer. Unfortunately, after including the Prototype.js library, all objects now have a number of default properties and methods whether you want them or not because they are added to Object.prototype. This effectively breaks the count() function and I can’t see a viable way to work around the problem other than removing Prototype.js from the mix.

Why does prototype.js break the count() function?

Take the following example:

var o = {0 : 'a', 1: 'b', 2: 'c'};
count(o); // should return 3 but with Prototype.js installed it returns 25

This returns the wrong result because Prototype.js adds a number of methods and properties to all objects in the system, so the count() function must count these also.

jQuery to the Rescue

Luckily there is another library that does everything that I have been using Scriptaculous/Prototype.js for: jQuery. It is leaner and less intrusive. It doesn’t change any of the underlying types and it still provides the drag-and-drop sorting of sections, and collapsing/expanding of sections. And in most cases it provided a cleaner, faster solution than was required with Scriptaculous.

Americans don’t even know what socialism is

It’s not the Republicans calling Obama a socialist that has my blood boiling. It is the fact that they believe that this is serious accusation that, if proven true, would be damning of Obama. If Obama were a socialist, it would mean he was an enemy of the state deserving prison time.

At least that’s what it sounds like when you listen to Fox News and other Republicans fear mongering about Obama’s supposed “socialism”. Listening to interviews with Republican supporters, I have heard the following misconception preached over and over again:

“They are worried that Barrack Obama will turn the United States into a socialist republic like Sweden, rather than the democracy that they have and currently cherish.”

Where do I begin?

1. Socialism is not the opposite of democracy. It isn’t even concerned with the same subject. Socialism refers to a collection of economic theories that advocate shared ownership and administration of resources, and the creation of an egalitarian regime (i.e. all men equal). Democracy is a form of government where the people hold the power, and there is a free electoral system. There exist socialist democracies. There also exist capitalist dictatorships. There is no connection between socialism and democracy.

2. If you read Obama’s policies, there is no way you can mistake them for socialism. I think that socialists would be quite insulted to be grouped together with a capitalist like Obama.

3. Sweden has a higher standard of living than the US.

I could probably go further.

But it is important for Americans to know how little they know about these topics. All this socialist talk boils down to Republicans trying to scare people with a word that they have been brainwashed to fear their entire lives.

I hope that the next generation of republicans will grow out of these ignorant beliefs.

Indiana Jones Special Features Reveals where it went wrong

I recently purchased the latest Indiana Jones DVD. As expected it contains a couple of documentaries about the making of the movie containing interviews with the cast and crew.

In watching the interviews I spotted two red flags that indicate where the cancer began to eat away at this movies chances to reach its potential.

1. George Lucas said that he wanted to change the genre from the 1930’s serials to the 1950’s alien sci-fi’s. He thought that this would be a nice parallel since the originals took place in the 30’s and this one takes place in the 50’s.

Perhaps the next movie can be a 1960’s musical a la Mary Poppins. This would be the perfect parallel since it will take place in the 1960’s.

2. Stephen Spielberg said that he didn’t want to do anything new with this film as it should be a blood relative of the original trilogy. He just wanted to make a nice movie for the fans to relive the originals.

This apparently flies in the face of what George Lucas was trying to do with the genre switch (and the friction was evident in the interviews), but more importantly it makes it sound like Spielberg handcuffed himself going into the flick. The thing that made the originals great were that they captivated the imagination of the viewer, and this is due, in large part, to the creative genius of Stephen Spielberg. So if he goes into the movie with the mindset that he doesn’t want to do anything new, he is necessarily leaving the creative genius that made the first films great in the storage locker.

Boxing is about drama

I watched Rocky III for the first time in 1983. I was 4 years old. I watched Rocky III for the one hundredth time in 1983. I was 4 years old.

The Rocky series captured the drama potential that exists in the sport of boxing in a way that even a 4-year-old can appreciate. When you transition from the Hollywood into the real-world sport of boxing, however, the drama becomes much more subtle, and scarce. One has to be selective about the fights that he watches, lest he become engulfed in oceans of boring bouts between mediocre fighters. I generally only watch the fights that are broadcast on HBO. This is a form of personal quality control and it raises the likelihood of a fight being exciting from 1% up to about 35%. Still, it is seldom in real boxing to see the level of drama that the Rocky series conveyed.

Despite these low odds, I am still drawn to boxing for the potential of drama. I enjoy the thrill of seeing two undefeated champions go head to head (e.g. Oscar De La Hoya vs Felix Trinidad). I hope for my aging heroes to be able to turn back the clock, if only for a night, and reclaim their former form to defeat a younger rising star. Muhammad Ali’s defeat of a younger, stronger favorite George Foreman is one example of drama that exceeded the manufactured drama of Hollywood.

On Saturday night, Bernard Hopkins brought some more genuine drama to the Ring. At 43 years old it looked like he didn’t have much left to offer, based on his previous couple of fights in which he made his opponents look awkward but didn’t offer much offense himself. On Saturday, against middleweight champion Kelly Pavlik, however, he looked like a finely tuned and youthful boxing master. He completely schooled and dismantled Pavlik, leaving him in bewilderment of what had happened.

Prior to the fight Pavlik was undefeated, and was (and still is) a rising star in the sport. He had recorded two decisive victories over Jermain Taylor, who had defeated Hopkins twice a couple of years ago to take the middleweight title which Hopkins had held for a record ten years. Pavlik was a 4-to-1 favorite to beat Hopkins. The only question was whether he could knock Hopkins out. What actually happened was very different. Hopkins owned every round and, by about the fourth round, the question became whether Hopkins could knock Pavlik out.

Watching this fight reminded me of why I enjoy the sport of boxing. It can be seen as an allegory for life. You can watch a young prospect come of age, gain skill, face challenges and climb to the top of the proverbial mountain. Within a few short years, however, you are forced to watch this same prospect begin to diminish with age and be overtaken by the shadow of what he once was. But once in a while you get to witness a blazingly beautiful sunset to cap a great career and peel away the shadows that must ultimately prevail. I believe I watched such a sunset on Saturday with Hopkins’ brilliant performance.

I hope the sun never completely sets on Hopkins, but knowing that it must, I hope that it lasts at least a few more years.