MySQL data dumps done right.

April 8th, 2009

To dump data in MySQL command line is one of the easiest things to do.  All you have to do is follow these 3 easy steps in your linux command line:

1.  Dump the schema only.  This will  not contain any data, procedures or triggers, but only create info.  This will allow you to easily import your original schema later on.

mysqldump -uroot -proot -d --skip-triggers myDatabase > myDatabase_schema.sql

The -u specifies your username, and must be followed by your username e.g. -uroot. This is the same with your password, -proot.

The -d tells mysqldump to not dump any data in the schema and the –skip-triggers will skip all triggers and procedures.

The myDabase is the name of your database and the > myDatabase_schema.sql is where you want to dump it.

2. Dump the triggers/procedures. This will only contain the triggers and procedures contained within your database.

mysqldump -uroot -proot -d --no-create-info myDatabase > myDatabase_triggers_procs.sql

Here we have a –no-create-info to tell mysqldump that no create statements must be allowed, thus eliminating any schema sql and again the -d parameter to skip all data.

3. Dump the data only without triggers and create statements.

mysqldump -uroot -proot --skip-triggers --no-create-info myDatabase > myDatabase_base_data.sql

Finally we can dump the data only. The –skip-triggers skips all triggers and procedures, whils the –no-create-info skips sql create statements.

This will allow you to have 3 different sql files:

  • myDatabase_schema.sql (only contains the schema)
  • myDatabase_triggers_procs.sql (only contains triggers/procs)
  • myDatabase_base_data.sql (only contains data)

Now to import.

This is fairly trivial.

All you have to remember is to import your data in a sequence.

First the schema, then the triggers and procedures and then finally the base data.  The schema will create your tables, the triggers and procs can then be added to your tables and finally the tables can be populated with data.

1. Import schema

mysql -uroot -proot myDatabase < myDatabase_schema.sql

2. Import triggers/procs

mysql -uroot -proot myDatabase < myDatabase_triggers.sql

3. Import base data

mysql -uroot -proot myDatabase < myDatabase_base_data.sql

And your done. :-)

Test drive your PHP code

March 30th, 2009

Lately I’ve ventured fully submerged into the world of Test driven development (TDD).

This might start out a bit scary, especially if you have only heard about it, but never done it yourself. There is a couple of good reads on the net, so you might want to start out by familiarising yourself with this exciting way of doing things.

Have a look at http://en.wikipedia.org/wiki/Test-driven_development or Google it.

What is test driven development?

Test driven development (TDD) is more a change in coding philosophy than anything else. Some of us might say: “…yip, I’ve written a couple of unit tests when I had some spare time at the end of my project.”

To be honest, this is a bit of the wrong way round method of doing things, kind of like building a house by putting on the roof first. I think Martin Fowler describes it best when he pointed out these three components.

  • Write a test for the next bit of functionality you want to add.
  • Write the functional code until the test passes.
  • Refactor both new and old code to make it well structured.

TDD is a branch of Agile development which was based upon the first stages of extreme programming, but has really become a coding methodology that changed the way in which a lot of projects are being done.
Read the rest of this entry »

the down-low on mobile device detection

February 19th, 2009

so - you say you want to detect which mobile devices hit your site? - in the past, this has been a bit of an issue, but lately - with really nice projects available out there such as WURFL or DeviceAtlas, you are able to concentrate harder on other issues, instead of having to write a complete library of your own.

so for this, i’ve decided on DeviceAtlas.  just head on over to DeviceAtlas and open a developers account - you will get a one year developer’s license to play around to see how cool it is.

after registering - click on the downloads link - then go to the php example and download the source files.  after de-tar-and-un-zipping the file, just extract the contents.  at the moment all we are really interested in, is the Mobi/Mtld/DA directory and its contents - also create a json directory and drop the Sample.json file inside.

dump everything into a web directory somewhere. :-)

Read the rest of this entry »

game theory pattern code example

February 17th, 2009

so i finally took the time to write some code to better articulate my thoughts on ‘game theory pattern’.

/*
* Abstract Unifier class to select objects from different sources.
*/
 
abstract class childObjectUnifier
{
    protected $currentObjectLocation;    
 
    public function __construct()
    {
        $this->currentObjectLocation = get_class($this);
    }
 
    public function getData()
    {
        switch ($this->currentObjectLocation)
        {
             case 'currentServerLoginObject':
                    return $this->currentServerLoginFunction();
                    break;
 
             case 'remoteServerLoginObject':
                    return $this->remoteServerLoginFunction();
                    break;
 
             default:
                    die('Current user load not available');
        }
    }
}
 
/*
* Login Object which exists on the current server.
* This object could still be hit on the current server if the current user load is not high.
* Better utilizes the current server.
*/
 
class currentServerLoginObject extends childObjectUnifier
{
    public function currentServerLoginFunction()
    {
         return 'Some function on the current server';
    }
}
 
/*
* This login object could exist remotely, for e.g. a restfull/soap/xml-rpc service
* which could be hit if the current server load is climbing.
* Could possibly use a dedicated 'login' server if user load is exceedingly high on current machine.
*/
 
class remoteServerLoginObject extends childObjectUnifier
{
    public function remoteServerLoginFunction()
    {
        return 'Some function on the remote server';
    }
}
 
/*
* The game theory pattern which decides which objects to instantiate based on logical
* decisions made by solid data.
* In this example the current user load.
*/
 
class GameTheoryPattern
{
    public $currentObject;
 
    function __construct($iCurrentUserLoad)
    {
        $this->currentObject = $this->baseObjectUponStrategicData($iCurrentUserLoad);
    }
 
    private function baseObjectUponStrategicData($iCurrentUserLoad)
    {
         switch ($iCurrentUserLoad)
         {
             case $iCurrentUserLoad < 1000:
                    return new currentServerLoginObject();
                    break;
 
             case $iCurrentUserLoad >= 1000 :
                    return new remoteServerLoginObject();
                    break;
 
             default:
                    die('Current user load not available');
        }
    }
}
 
//GetCurrentUserLoad
$iCurrentUserLoad = 1000;
 
//Create Login Object based on the Game Theory Pattern
$oLogin = new GameTheoryPattern($iCurrentUserLoad);
 
//Call an function from the unifier class
echo $oLogin->currentObject->getData();
 
?>

It should be pretty self explanatory - due to the high level comments :-)

feel free to add suggestions or comments - since i’m pretty sure there could be a better way, but for now, just to convey the message it should be sufficient.

applying game theory patterns to development

February 11th, 2009

mobile - that damned device that makes our life so easy, yet sometimes so inheritely difficult.

as a developer, we kind of try and convince ourselves that developing for mobile and developing for a desktop browser is kind of the same thing.  but we all know that this is a stalling technique for the inevitable, since invariably it becomes a whole different field of play.

every little feature you add, every little flow created and every branch of navigational hierarchy is a challenge on its own.

enter the game theory pattern

Read the rest of this entry »

coolest guy on the internet

January 30th, 2009

so - in our office we have a little competition to see who can come up 1st for the term “coolest guy on the internet” in google.  the seo gurus in our midst have taken to this, like a bear to honey.  so hopefully someone will coin the term “coolest guy on the internet“.

who know maybe even me?

coolest guy on the internet

centering mobile web

January 26th, 2009

with all the focus being on the mobile web these days, one must have a basic understanding of how the pieces fit together before even writing a line of code.

there is a great online community which supports mobile development called mobiforge which could be utilised to answer all of your mobile related questions.

but as a start i will point out some important steps/considerations to take/make:

Read the rest of this entry »

crossing the mvc divide, kohana and zend style

January 23rd, 2009

so, i’ve been pretty much a zend framework addict, ever since i coded my first bootstrap.  thinking back to that countless hours trying to understand the beast that is zf, ahhh, what fond memories…  and lately i’ve also been playing around a bit with kohana, which is another web based mvc framework, but definitely a bit more lightweight and easier to use than others.

there is however a bit of an issue with web based mvc frameworks, and in my opinion a large freaking elephant that no one seems to talk about.  how can i call a controller from within a view and assign an action to it - and is it correct to do so?

and before the quiet whispers start, i’d like to note that this does not necessarily go against mvc principles.  if you think about it - the whole point of mvc is to seperate business logic, application logic and the view (presentation logic) - and i’ve confirmed this with a couple of conversations with other developers.  but on the other hand, if you feel that i have overstepped a boundary - please do let me know if there is a better, or more correct way of doing this.

Read the rest of this entry »

we be having web 2.0

January 20th, 2009

so - at our office there has been huge focus on breaking into the lucrative web 2.0 market.

as a short term solution to this i will show you how to make your own web 2.0 social network in no time.

Read the rest of this entry »

the great search balancing act

January 14th, 2009

it’s been a while since my last post - and as interests fade with time, others jump up faster than a beach ball at a nickelback concert.

so i’ve been looking into solr the last couple of days.  solr is relatively new in the arena and probably outshined a bit in popularity by other search engines such as lucene and nutch.  “but why solr?”, you may find yourself asking.

Well solr has a couple of tricks up the sleave - which is likely due to the fact that its a fresher version of the old, dare i call it legacy, search engines.

Read the rest of this entry »