what is magento sql/setup?

While you are writing a new module or customizing existing ones’ you may need to do some tweaking in your db. Magento gives you facility to write sql insaltaller/updater which will run exactly once in its lifetime , do the tweaking for you need and then instantiate you module. In case of complete new module you have to write installer sql and in case of customizing module updater sql.

How does module installer works ?

At the time of instantiating magento looks all Xml files inside the forder /app/etc/module/ to get primiry information about it’s all active module. Then for each module it read the module’s etc/config.xml for is there any sql setup handler available or not. If any sql setup handler declared in config.xml, it looks for the current version number of the module in db core_resource table. The core_resource table contains list of setup handler’s and their corresponding version number.

If no sql handler is listed in the core_resource table then magento runs the installer, if version number in config.xml is greater then the version number in the core_resource table, magento runs the proper updater sql file. For example core_resource table version number is 0.1.0 and in your config.xml you give version number 0.2.0, then magento run the mysql4-upgrade-0.1.0-0.2.0.php.

Writing installer/updater php

Writing an installer/updater is very simple. Get the installer instance, startup installation, run the desired query , and then stop the setup process. For you better understanding I am giving a simple example :-

i)Declare you module and its code pool in

<?xml version="1.0"?>

ii)Define the setup handler in

<?xml version="1.0"?>



      <!-- Declaring module setup handler -->
      <!-- &#91;start&#93; -->
      <!-- &#91;End&#93; -->




iii)Write the installer php


  $installer = $this;



    DROP TABLE IF EXISTS {$this->getTable('securityquestion')};

    CREATE TABLE {$this->getTable('securityquestion')} (
      `securityquestion_id` int(11) unsigned NOT NULL auto_increment,
      `title` varchar(255) NOT NULL default '',
      `status` smallint(6) NOT NULL default '0',
      `created_time` datetime NULL,
      `update_time` datetime NULL,
      PRIMARY KEY (`securityquestion_id`)

    INSERT INTO {$this->getTable('securityquestion')}(
    `securityquestion_id` ,
    `title` ,
    VALUES ('1', 'Mother\'s maiden name', '1') ,
    ('2', 'Father\'s middle name', '1') ,
    ('3', 'My first car', '1') ,
    ('4', 'High School name', '1');



Ok, we are done !!! Please leave your comments here 🙂 .