Adding Magento Install and Upgrade Scripts

This article will demonstrate how to go about adding Magento install and upgrade scripts. Assuming you have read how to add models, resource models and collection, you should be in a good position to add the setup scripts.

Let’s continue with our example store information previously and add data to that.

First of all, the install script will be added in the following directory.

app/code/local/Custom/Storeinfo/sql/custom_storeinfo_setup

This is because the naming convention of custom_storeinfo_setup matches that of the node within the config.xml that we set up previously.


<?xml version="1.0"?>
<config>
    <global>
        ....
        <resources>
            <custom_storeinfo_setup>
                <setup>
                    <module>Custom_Storeinfo</module>
                </setup>
            </custom_storeinfo_setup>
        </resources>
        ....
    </global>
</config>

The install script is responsible for adding columns to your database table. The file naming convention is as follows:

install-[version_number].php

Where [version_number] is the version specified in the config.xml file.


<?xml version="1.0"?>
<config>
    <modules>
        <Custom_Storeinfo>
            <version>1.0.0</version>
        </Custom_Storeinfo>
    </modules>
    ....
</config>

So in our example, it would be install-1.0.0.php. So to start with, add the following code to start the installer setup.

$installer = $this;
$installer->startSetup();

It’s worth noting that although the class used for the install script has not been specified, Magento uses the Mage_Core_Model_Resource_Setup class.

Magento makes use to DDL tables which ensures the setup scripts are cross-database compatible. So to add the columns, we should be using Magento’s Varien_Db_Ddl_Table class.


$table = $installer->getConnection()
    ->newTable($installer->getTable('storeinfo/store'))
    ->addColumn('store_id', Varien_Db_Ddl_Table::TYPE_INTEGER, null, array(
        'identity'  => true,
        'unsigned'  => true,
        'nullable'  => false,
        'primary'   => true,
        ), 'Store ID')
    ->addColumn('name', Varien_Db_Ddl_Table::TYPE_VARCHAR, null, array(
        'nullable'  => false,
        ), 'Name')
    ->addColumn('description', Varien_Db_Ddl_Table::TYPE_VARCHAR, null, array(
        'nullable'  => false,
        ), 'Description');

$installer->getConnection()->createTable($table);

What if in future, we wanted to add another, or modify an existing column to our module? Magento gives us the ability to use upgrade scripts. Similar to install scripts, they belong in the [namespace]_[module]_setup directory within the sql directory.

If we want to see our upgrades take effect, Magento requires us to update the version of our module, and it is the difference between these module versions that are used in the naming convention of the upgrade script. So to add a successful upgrade script, we firstly have to modify config.xml and update our module version:


<config>
    <modules>
        <Custom_Storeinfo>
            <version>2.0.0</version>
        </Custom_Storeinfo>
    </modules>
    ....
</config>

We then add our upgrade script, using the naming convention: install-1.0.0-2.0.0.php.


$installer = $this; 
$installer->startSetup();
 
$installer->getConnection()
    ->addColumn($installer->getTable('storeinfo/store'),
    'created_at',
    array(
        'type' => Varien_Db_Ddl_Table::TYPE_TIMESTAMP,
        'nullable' => true,
        'default' => null,
        'comment' => 'Created At'
    )
);
 
$installer->endSetup();

When you refresh the website, Magento will then run the upgrade script. Check the database, and you should notice the extra column added in.

To find out how to add data into a table via the use of data scripts, check out the Magento Data Scripts post.

Note: This article is based on Magento CE version 1.9.