Do you want to add some custom tables or attributes for your custom Magento extension? Let’s understand the process with an extension named ”Insync_Test” to run our installation script.

I hope you are familiar with magento extension development, if not please read our previous blog

Let’s create the directory structure according to the following image.
Add registration file “Insync_Test.xml” in “app\etc\modules” to activate custom module.

		<!-- declare Insync_Test module -->
			<!-- this is an active module -->
		<!-- this module will be located in app/code/local code pool -->

Now, we make sure that Magento knows about the module

  • Clear cache in Magento Admin, if cache is enabled.
  • Go to System->Configuration
  • Click Advanced
  • In the “Disable modules output” setting box, look for your new module named “Insync_Test”

Now we will create configuration file “config.xml” in path “app\code\local\Insync\Test\etc\” for our module. Add below content.

<!--?xml version="1.0"?-->
			<!-- Version of module tells the latest install script -->
&lt;!— This class needed to create new attribute in Magento --&gt;                            


Here version number is important as it decides latest script version to run.

Now we will create the script file with same version declared in “config.xml”.

Create “mysql4-install-” in path “app\code\local\Insync\Test\sql\insync_test_setup\”

Make sure your script file version (in this case, should match version defined in “config.xml”.

Add below content to your script file

<!--?php /* @var $installer Mage_Core_Model_Resource_Setup */ $installer = $this; $installer-&gt;startSetup(); /// Enter your script here $table = $installer-&gt;getTable('Insync_Test'); $installer-&gt;run( "CREATE TABLE IF NOT EXISTS `$table` ( `entity_id` int(11) NOT NULL AUTO_INCREMENT, `website_id` int(11) NOT NULL, `order_id` int(11) NOT NULL, PRIMARY KEY (entity_id))" ); /// End of script $installer-&gt;endSetup(); &lt;/pre&gt; &lt;p&gt;You can change the sql query or script section according to your Extension requirement, here we are going to create a table “insync_test” in Magento database. We have used a variable $table to get Table Name with prefixes, if any.&lt;/p&gt; &lt;p&gt;e.g. if you have installed Magento instance with table prefixes &lt;strong&gt;“Mage_”&lt;/strong&gt;,then&lt;/p&gt; &lt;pre&gt; $installer-&gt;getTable('Insync_Test')&lt;/pre&gt; &lt;p&gt;will return “Mage_Insync_Test”. So this script will create table using Magento table prefixes.&lt;/p&gt; &lt;p&gt;Save all files, make sure your extension directory structure should look like the first image shown here.&lt;br ?-->
Now copy your extension into your Magento root folder.

Open Magento admin and clear cache.

If script ran successfully, you can find the table “insync_Test” in Magento Database.

Each time you clear Magento cache, Magento compares for Version number for “insync_test” listed in “core_resource” table and version number in “config.xml” file, if version mismatches Magento look for appropriate script file.

For up-gradation process, suppose if you want to add new functionality into your Magento Extension. You can create an upgrade file in the same folder where script file exists.

Let’s say you want to add a new customer attribute in Magento.

Create a new file “mysql4-upgrade-” in “app\code\local\Insync\Test\sql\insync_test_setup”. This means extension will upgrade from to We need to change the version number in “config.xml” to, otherwise upgrade script will not run.

Add below content to upgrade file

<!--?php $installer = $this; $installer-&gt;startSetup(); /** Add attribute to cutomer entity **/ $installer-&gt;addAttribute("customer", "cust_no", array( "type" =&gt; "varchar", "backend" =&gt; "", "label" =&gt; "Customer No.", "input" =&gt; "text", "source" =&gt; "", "visible" =&gt; true, "required" =&gt; false, "default" =&gt; "", "frontend" =&gt; "", "unique" =&gt; false, "note" =&gt; "" )); $attribute = Mage::getSingleton("eav/config")-&gt;getAttribute("customer", "cust_no"); /** add forms where Customer number needs to display **/ $used_in_forms=array(); $used_in_forms[]="adminhtml_customer"; $used_in_forms[]="customer_account_edit"; $used_in_forms[]="adminhtml_checkout"; $attribute-&gt;setData("used_in_forms", $used_in_forms) -&gt;setData("is_used_for_customer_segment", true) -&gt;setData("is_system", 0) -&gt;setData("is_user_defined", 1) -&gt;setData("is_visible", 0) -&gt;setData("sort_order", 0) ; $attribute-&gt;save(); $installer-&gt;endSetup(); &lt;/pre&gt; &lt;p&gt;Clear cache again from Magento Admin, if cache is disabled refresh any Magento URL. You will see the custom customer attribute in the backend.&lt;br ?-->
<a href=""><img class="aligncenter size-large wp-image-7322" src="" alt="images 12" width="721" height="286" /></a>
And also if you check “core_resource” table in Magento Database. You will see latest version number for the extension.
<a href=""><img class="aligncenter size-full wp-image-7323" src="" alt="2" width="721" height="35" /></a>
Upgrade file like this can be created as many times as required.
Cheers, Now you can create install scripts for your extension.

Let me know after comment below if you have any problem.

You may also like it.

Important Things to Remember When Pricing a New Product

How to ensure After Sales satisfaction to Customers

Creating Tax Rules in Magento

Integrate NAV with eCommerce, Marketplace and CRM