Setting up your editor

Nov 14, 2012 • 0 minute read

Setting up your editor correctly can make working with other developers much less painful. Below are some things that I believe every developer should do when editing source code. Any good IDE or editor should have settings to do these things automatically - the points below are paired with their Sublime Text setting.

(Read more)

Non-tech books I should read

Oct 12, 2012 • 0 minute read

This post is to help me keep track of non-tech books that I would like to read.

Breakfast of Champions by Kurt Vonnegut

Slaughterhouse-Five by Kurt Vonnegut

Cat’s Cradle by Kurt Vonnegut

Do Androids Dream Of Electric Sheep by Philip K Dick

Neuromancer by William Gibson

Ham on Rye by Charles Bukowski

(Read more)

Solving "502 Bad Gateway" with nginx & php-fpm

Sep 22, 2012 • 1 minute read

After upgrading php-fpm, my PHP-based sites were returning “502 Bad Gateway” errors. This can happen when the php5-fpm package reconfigures itself to listen on a different socket. Here’s how you can solve it.

Check to make sure that php-fpm is running with ps aux | grep php - if you can’t see any php-fpm processes in the output, then you may need to re-install php-fpm. If php-fpm is running okay, then skip this first step.

sudo apt-get remove php5 php5-cgi php5-fpm
sudo apt-get install php5 php5-cgi php5-fpm

The thing to notice here is that the order in which you install the packages is important. In the past I have found that installing them in the wrong order causes the packages to be configured incorrectly.

Next, get php-fpm to listen on the correct host/port. In /etc/php5/fpm/pool.d/www.conf change the listen value to match the fastcgi_pass location in your Nginx configuration. For example, I changed mine from:

listen = /var/run/php5-fpm.sock

To:

listen = 127.0.0.1:9000

If you are configuring php-fpm to listen on a Unix socket, you should also check that the socket file has the correct owner and permissions. While I wouldn’t recommend it, you can simply give read-write permissions to all with sudo chmod go+rw /var/run/php5-fpm.sock.

Restart php-fpm with sudo service php5-fpm restart and everything should work normally again.

(Read more)

Sublime Text packages for web development

Sep 12, 2012 • 1 minute read

Coming from PhpStorm (a full-featured IDE), I felt that Sublime Text was missing a few useful features. Luckily, one of the great things about Sublime is that it can be easily extended with plugins and packages. Perhaps the most useful package for Sublime is Sublime Package Control, which allows you to easily install and manage packages (it can even uninstall itself - über meta).

Below are some Sublime Text packages that I have found to be useful for web development.

(Read more)

September 4: Looking back on the Christchurch earthquakes

Sep 4, 2012 • 3 minute read

In the early hours of 4 September 2010, I was sound asleep in my house on Mount Pleasant hill in Christchurch, New Zealand. At 4:35am I was woken by a deep rumbling sound. Seconds later, the floor and walls began to shake as my house was rocked back and forth on its foundations. I scrambled out of bed and hid in a doorway until the shaking stopped. After checking that my family was okay, I went upstairs and stepped onto the balcony to look out over Christchurch. The city was silent save for a few car alarms, and it was in this relative quiet that I came to understand the significance of this event. I didn’t realise it then, but that earthquake would become a catalyst for change in my life.

(Read more)

Programming Books and Resources

Aug 28, 2012 • 0 minute read

This post is a list of programming-related books that I intend to read at some point (in no particular order).

And some other resources that aren’t books:

(Read more)

The man who could turn his feet into roller blades

Aug 19, 2012 • 1 minute read

Few people know about my ability to transform my feet into roller blades. Doctors around the world are perplexed, and also impressed. Biologists debate whether such a transformation is even possible.

I casually slide into the biologist debating hall and take a seat near the front. People are staring at my roller-feet, but I don’t care. I put my shades on.

(Read more)

Testing Laravel Controllers

Jul 9, 2012 • 3 minute read

There are a two main things that tripped me up while I was writing functional tests for my Laravel controllers: POST requests, and session state.

Laravel’s Controller class has the call() method, which essentially makes a GET request to a controller method. In order to make POST requests, it’s necessary to inject some extra parameters into the HttpFoundation components. To make this easier, I created a ControllerTestCase class with convenient get() and post() methods:

(Read more)

Bcrypt: Choosing a Work Factor

Jun 30, 2012 • 2 minute read

Bcrypt is a Blowfish-based hashing algorithm which is commonly used for password hashing because of its potentially expensive key setup phase. A Bcrypt hash has the following structure:

$2a$(2 chars work)$(22 chars salt)(31 chars hash)

The reason that the key setup phase can be potentially expensive is because it is run 2 times. As password hashing is usually associated with common tasks like logging a user into a system, it’s important to find the right balance between security and performance. Using a high work factor makes it incredibly difficult to execute a brute-force attack, but can put unnecessary load on the system.

Using Marco Arment’s PHP Bcrypt class, I performed some benchmarks to determine how long it takes to hash a string with various work factors:

(Read more)

Automatic MySQL Backups

May 3, 2012 • 0 minute read

It’s really easy to set up automatic MySQL backups using mysqldump. First, you need to set up a user with SELECT and LOCK TABLES privileges. In this example the user doesn’t have a password.

CREATE USER 'autobackup'@'localhost';
GRANT SELECT, LOCK TABLES ON *.* TO 'autobackup'@'localhost';

Next create the cron job with crontab -e. This job is set to run every day at 5:20am.

20 5 * * * mysqldump --user=autobackup dbname | gzip -c > /var/backups/dbname-$(date +\%Y\%m\%d).sql.gz

Don’t forget to change dbname to the name of the database that you want to backup. And that’s it - you’re done! This cron job will create a backup of your database and save it to /var/backups with a filename based on the current date, e.g. /var/backups/dbname-20120503.sql.gz

(Read more)

MySQL: Specified key was too long; max key length is 767 bytes

Mar 31, 2012 • 1 minute read

MySQL has a prefix limitation of 767 bytes in InnoDB, and 1000 bytes in MYISAM. This has never been a problem for me, until I started using UTF-16 as the character set for one of my databases. UTF-16 can use up to 4 bytes per character which means that in an InnoDB table, you can’t have any keys longer than 191 characters. Take this CREATE statement for example:

CREATE TABLE `user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(32) NOT NULL,
  `password` varchar(64) NOT NULL,
  `email` varchar(255) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `UNIQ_8D93D649F85E0677` (`username`),
  UNIQUE KEY `UNIQ_8D93D649E7927C74` (`email`)
) ENGINE=InnoDB DEFAULT CHARSET=utf16 AUTO_INCREMENT=1 ;

This will fail with an error like Specified key was too long; max key length is 767 bytes, because the UNIQUE INDEX on the email field requires at least 1020 bytes (255 * 4).

Unfortunately there is no real solution to this. Your only options are to either reduce the size of the column, use a different character set (like UTF-8), or use a different engine (like MYISAM). In this case I switched the character set to UTF-8 which raised the maximum key length to 255 characters.

(Read more)

Git: Ignore changes to already-tracked files

Mar 14, 2012 • 0 minute read

There are often times when you want to modify a file but not commit the changes, for example changing the database configuration to run on your local machine.

Adding the file to .gitignore doesn’t work, because the file is already tracked. Luckily, Git will allow you to manually “ignore” changes to a file or directory:

git update-index --assume-unchanged <file>

And if you want to start tracking changes again, you can undo the previous command using:

git update-index --no-assume-unchanged <file>

Easy!

(Read more)

Converting an SVN repository to Git

Feb 8, 2012 • 0 minute read

Today I found out just how easy it is to convert an SVN repository to Git without losing any commit history. Note that you will need git-svn (apt-get install git-svn on Debian/Ubuntu).

git svn clone http://mysvnrepo.com/my-project my-project
cd my-project
git remote add origin git@mygitrepo.com:/my-project.git
git push origin master

Et voilà, my-project.git has the full commit history of the my-project SVN repository.

If anybody knows whether SVN branches can be converted to Git branches, please get in touch!

(Read more)

Simple Nested Sets in Doctrine 2

Nov 16, 2011 • 4 minute read

Unlike Doctrine 1 with it’s NestedSet behaviour, there is no nested set functionality in the core of Doctrine 2. There are a few extensions available that offer nested set support:

I tried all of these extensions, but none of them felt simple or lightweight enough for my application. What I wanted to do was have a Category entity which could have a tree of sub-categories, e.g:

(Read more)

Doctrine 2: Resolving "unknown database type enum requested"

Oct 12, 2011 • 1 minute read

I came across this recently while I was developing a module for PyroCMS. Some of the PyroCMS tables contain ENUM columns, which Doctrine doesn’t support. You would think that this wouldn’t be an issue since these tables are not mapped, but apparently when Doctrine builds the schema it includes all tables in the database - even if they are not mapped. This has been reported as an issue, but the Doctrine team has given it a low priority.

The symptom? When using the SchemaTool to create, update, or drop the schema; an exception is thrown:

**Fatal error**: Uncaught exception 'Doctrine\DBAL\DBALException' with message 'Unknown database type enum requested, Doctrine\DBAL\Platforms\MySqlPlatform may not support it.'

Thankfully, the fix is very easy. There is even a Doctrine Cookbook article about it. All you have to do is register the ENUM type as a Doctrine varchar (string):

/** @var $em \Doctrine\ORM\EntityManager */
$platform = $em->getConnection()->getDatabasePlatform();
$platform->registerDoctrineTypeMapping('enum', 'string');

This fix can be applied to any unsupported data type, for example SET (which is also used in PyroCMS):

$platform->registerDoctrineTypeMapping('set', 'string');
(Read more)

← Older Posts Newer Posts →