Thursday, 7 November 2013

CakePHP New application database config (never forget the enconding)

Today I will put down the way to begin a new web application project using CakePHP.

The first part is database creation. Database charset must be utf-8 and collation: utf8_unicode_ci. The difference between general and unicode collations is explained in an excellent manner in this post on

Next, when you start a new CakePHP project the database config class looks like this

public $default = array(
  'datasource' => 'Database/Mysql',
  'persistent' => false,
  'host' => 'localhost',
  'login' => 'user',
  'password' => 'password',
  'database' => 'test_database_name',
  'prefix' => '',
  // 'encoding' => 'utf8',

My lesson today is: Before changing anything else, uncomment the last line!.

... in case you do not then Unicode (Greek in my case) text will still appear correctly in the web application, but phpMyAdmin and sqldump will display garbage. In addition searches with non-latin text will always fail. To make matters worse when I discovered the case of the problem, I realised that I would have to re-enter all my test data.

Friday, 1 November 2013

SchemaSync: An effective and simple way to synchronize MySQL databases

I have spend the last couple of days trying to find out a solution for migrating changes from a development database to the productive. I was looking for a clean, simple and reliable solution that (ideally) runs on Linux.

My first attempt was to to use the official MySQL mysqldiff command. It turned out that in my case, the only suggestion I got was to recreate indexes and drop primary keys every time there was a record number mismatch between tables. I also run into Windows GUI's with a 30 day evaluation period and a cost ranging from $90-$150.

My lack changes when I run into Schema Sync written in Python by Mitch Matuson. The page has everything you need and the setup (if you already have python) takes less that a minute.

Quoting from the utility's examples page, Sync the production db with the changes from the development db

 schemasync mysql://user:pass@dev-host:3306/dev_db mysql://user:pass@prod-host:3306/production_db

This will give you two files in your home directory, one to do the patching and one to revert back the changes if requested. A excellent piece of work, that I am very happy to use. Thumbs up for mr Matuson.