Script for Database Patching at Deploy Time

I’ve written before about a simple way of patching database versions and there’s a much more comprehensive article from Harrie on TechPortal as well. I often find though that projects with patching strategies are missing the scripts to apply these automatically when the code is deployed, so I thought I’d share mine.

My current project (BiteStats, a simple report of your google analytics data) uses a basic system where there are numbered patches, and a patch_history table with a row for every patch that was run, showing the version number and a timestamp. When I deploy the code to production, I have a script that runs automatically to apply the patches.

Continue reading

Invalid Protected Resource URL in Pecl_Oauth

I had a funny (funny weird, not funny haha) problem the other day when working with pecl_oauth in PHP to talk to a service. I’d gone through all the handshaking steps, got the acces token and was ready to start talking to the service itself. However when I tried to call OAuth::fetch, I got an error message:

Fatal error: Uncaught exception 'OAuthException' with message 'Invalid protected resource url, unable to generate signature base string'

There are two things to notice about this. The first one is that I should be catching exceptions thrown by this code :) The second is that I could see nothing wrong with my url, http://api.local. It turned out, after some experimentation, that what is missing here is a trailing slash, and if I supply http://api.local/, everything works perfectly nicely! I’m unclear if this is intended functionality or not, but if you see this error message and you’re requesting a URL with no path info, make sure you have a trailing slash.

Downgrading a PECL Module

Recently I saw some weirdness in an existing application when I upgraded a PECL module that the application depended on. To figure out if that really was the problem, I wanted to downgrade the module to its previous version. There is no opposite command to “upgrade” but you can instruct pecl to install a specific version of a module, using the -f switch to force pecl to overwrite newer modules.

Downgrading Pecl_OAuth

For me the problems were caused in the switch between default functionality in pecl_oauth 1.1.0 (this isn’t a bug, but is correct behaviour according to the OAuth 1 spec, I just had code that expected the old functionality), so I wanted to put my version back to 1.0.0

pecl install -f oauth-1.0.0

It was easy once I stopped looking for an option called “downgrade” or something like that :) In fact you can use this trick to install all kinds of pecl versions, simply refer to the package as [package name]-[version]. By default pecl won’t let you install packages that aren’t marked “stable”, but you can install beta packages by putting “beta” in place of [version].

Hopefully now I’ve written this I’ll remember next time how to do it!

Summer PHP Conferences

Conference season approaches and in May I’m on a trip to take in two of the most high-profile events in the PHP conference calendar: DPC in Amsterdam (19-21 May) and php|tek in Chicago (24-27 May). The two events have historically been a few weeks apart and I’ve always complained at having all the fun for the year in such a short space of time – but this year the events are literally back-to-back, there are a small number of speakers attending both and we’re pretty much all on the same flight from Amsterdam to Chicago!

Continue reading

Getting Dates From Week Numbers in PHP

Recently I’ve been building a little project that pulls data from Google Analytics and shows your web statistics in a simple form. One thing I wanted to do was show the data for a quarter, but graphing by day is too chaotic and graphing by month only gives three points, so I wanted to graph by week. This was fine but the data returned by Analytics only gives me the week numbers, and since this project is aimed at demystifying web stats for normal people, it really needs normal dates on it!

Using DateTime::setISODate

I found that the DateTime extension has a method setISODate() which accepts the year and week number, and makes a date you can then use as normal.

$week_start = new DateTime();
$week_start->setISODate($year,$week_no);
echo $week_start->format('d-M-Y');

You can also do this the other way around; if you have a date in PHP, there’s the ‘W’ flag for date() which will return you the week number – very handy!

Github To Jira Bug Migration Script

Recently I mentioned the github API and retrieving issues from it. This is because the joind.in project agreed to move its issue tracking from github to JIRA, since the issue tracker on github is far from feature complete. I migrated only our open issues, and comments (and the comments ended up a bit weirdly formatted on the other end but this was the best they could do). It was nothing pretty or clever but in case it’s useful to someone else, here’s the script:

Continue reading

The PHP Community Conference

Next month, I’ll be heading out to Nashville for the PHP Community Conference. This is wildly exciting for a few reasons; this is the first in what I hope will be many episodes of this event, and I’m speaking in a lineup that blows every other conference schedule I’ve seen out of the water. I have met and hung out with enough of these people to know that I’m going to get smarter just by being there! I love watching the industry leaders discuss technology, I learn so much, and I know that this event will be a fabulous opportunity for that.

The event is entirely community organised and run, rather than being backed by an organisation. I am a great believer in having events come from the community that wants to attend them, and as an organiser (both for community and organisation-backed events) myself, the freedom to do things that will really work, rather than things that can be agreed by a management committee, makes the difference between a good event and a great one. What’s different about this PHP Community Conference is that most of the organisers are speakers and attendees of some of the biggest conferences in the PHP world … and they’ve built the international-level conference *they* want to attend!

The lineup is nothing short of stellar, these guys and gals would be the main feature at most of the other PHP-specific events I’ve been to, in fact three or four of them have been keynotes at other events I’ve attended. I’m speaking myself, which was wildly exciting from the moment I got the acceptance email right up until the rest of the schedule was published … and is now slightly daunting, in the best possible way! I’m giving a half-day tutorial on Web Services, covering all the theory points and showing you how to not only consume but also publish your own services. I work so much with APIs and being able to take the time to properly share my experiences so others can go on to build their own kick-ass services is something really special.

I can’t wait to get out to Nashville on April 21/22 and meet the speakers and the fantastic crowd of attendees that I know an event like this will draw. Which is not to say that there are not other great conferences, but I’m really looking forward to seeing something special in Nashville … I sincerely hope to see you there!

Dealing with MySQL Gone Away in Zend Framework

I wrote recently about having gearman in my application, however I have been seeing problems with the long-running PHP worker scripts. My logs had entries like this:

SQLSTATE[HY000]: General error: 2006 MySQL server has gone away

The worker is a Zend Framework application, run from the CLI, and it seemed like the Zend_Db_Adapter had no way of knowing when MySQL had let go of its end of the connection. I tried a few different things, including Zend_Db_Adapter::getConnection(), but without success – until I dug through the source code (with some help from a friend) and realised that ZF was not reconnecting at all if it thought it already had a connection. So instead, I expressly disconnected and reconnected the database handler. At bootstrap time, I place my database handle into the registry, so I simply added this at the start of the actual function that the gearman worker calls:

$db = Zend_Registry::get('db');
$db->getConnection();

At the end of my script, before it returns to the loop waiting for another gearman job, I just disconnect my database:

$db->closeConnection();

Now Zend_Db_Adapter knows that when I ask it to connect, it needs to go off and make a new connection, and everything works really well! I was seeing the errors because I’m still only testing the system so it can go days between getting any new jobs, and the timeout on MySQL is shorter than that.

A Tale of Two User Groups

This post is probably only relevant if you’re interested in PHP and UK-based. In the next few weeks I’ll be at some user groups that I don’t manage to visit often. On Thursday, 3rd March, I’ll be at PHP East Midlands to talk about Design Patterns. On Tuesday, 12th April, I’ll be at PHP West Midlands, also speaking but this time about OAuth. I may make it to one or other of PHPNW and LeedsPHP user groups in that time as well … and all of those are within 2 hours’ driving of my home! PHP is alive and well where I live, my great respect and thanks goes to all the community leaders who make these groups happen – thank you all :)