Monthly Archive for January, 2008

[Howto] Installing and using mysqldiff

What happens when you have to compare structures of two databases? Well, first thing - with all the great tools around (watch and learn: ActiveRecord’s Migration) you shouldn’t have let this happen. But since you’re reading this you’re probably already facing this problem.

We have a couple of options: Windows users could try SQLyog MySQL GUI - Enterprise Edition, PHP folks have MySQLdiff, but there’s another thing you might want to try. It’s my favourite since CocoaMySQL and command line client suit me well for every day work and I don’t fell like spending $50 on a piece software that I might (or not) use once a year. I present to you mysqldiff (what a fancy name)!

I’m using OS X 10.5.1 with default Perl installation, so first I had to install the prerequisites - Class-MakeMethods. Download the package and from the command line execute:

tar xzf Class-MakeMethods-*.tar.gz
cd Class-MakeMethods-*
perl Makefile.PL
make test
sudo make install

Now on to the mysqldiff. After downloading we need need to execute the same set of commands:

tar xzf MySQL-Diff-*.tar.gz
cd Class-MakeMethods-*
perl Makefile.PL
make
sudo make install

And you’re done - by default mysqldiff is installed to /Library/Perl/5.8.8/MySQL/mysqldiff.pl.

An example:

mysqldiff.pl --host=host.com --user=user --password=password database_1 database_2

This command will list all the queries required to recreate the exact structure of all the tables of the database_2 in database_1. You can also run mysqldiff.pl with --apply switch, but I always like to check the script before executing it.

That’s it - if you know a better tool - be sure to let me and others know in the comments to this post.

[Hint] ISO 8601 compliant week numbers

Last week one of our customers noticed that the Calendar module in our CRM application shows incorrect week numbers. Since it was a really old module we were pretty sure that they were correct - isn’t the PHP’s date function supposed to do all the dirty work for us?

Later on we came up with Polish government’s norm (suggestion, to be exact) that states that the first week of the year is the first week which has at least 4 days in the year (and since it’s ISO 8601 compliant, our task was quite easy). We had to remove all the occurrences of:

$var = date('W');

and replace them with:

$var = strftime('%V');

since the result of strftime(’%V’) is ISO 8601 compliant.

Up and running!

My blog in English is back - this time it has it’s own domain (http://filiptepper.com/). What it’s gonna be? We’ll see, but surely you can expect a developer’s point of view - not only on development.