Magento Custom Module Upgrade MySQL on Version Update Tutorial

Let us assume you have the module created and it is called GreatModule. Its previous version was 0.1.1 and you are on version 1.4.3 and want to make alterations to a table or a set of tables in the MySQL database during the upgrade process. As part of the original installation of GreatModule, you would have a MySQL setup file called mysql4-install-0.1.1.php in the GreatModule/sql/greatmodule_setup folder. To create the update to the MySQL database follow the steps outlined below.

Step 1: Update the version number inside the config.xml

Open GreatModule/etc/config.xml Find <version>0.1.1</version> and change it to <version>1.4.3</version> Resave the config.xml

Step 2: Make your new MySQL upgrade file

In GreatModule/sql/greatmodule_setup create a file called mysql4-upgrade-0.1.1-1.4.3.php There really are just four elements separated by hyphens, and through this filename it tells Magento to run the upgrade.

Custom module mysql update syntax for Magento E-Commerce

Step 3: Write your MySQL Statement

Inside the file mysql4-upgrade-0.1.1-1.4.3.php write whatever MySQL statements you need to fulfill the requirements of your upgrade.

Step 4: Refresh your cache

And you are done. Your GreatModule now has the ability to upgrade tables inside your database. One aspect which is important to note is the upgrade files will cascade and run one after another. So if later you are on version 1.5.2 and need another MySQL upgrade, you will have three files inside the GreatModule/sql/greatmodule_setup folder as follows: mysql4-install-0.1.1.php mysql4-upgrade-0.1.1-1.4.3.php mysql4-upgrade-1.4.3-1.5.2.php

This really is a simple process. Generally, if you are having problems getting the upgrade to work it could be one of these things:

1 ) You updated the GreatModule/etc/config.xml and refreshed your Magento cache prior to adding the MySQL mysql4-upgrade-0.1.1-1.4.3.php

You can manually check your database to see the installed module version. Using PhpMyAdmin or similar software, open your Magento store database and locate the core_resource table. Inside the core_resource table locate greatmodule_setup and double check the version installed.

2 ) You have a typo in your mysql4-upgrade file. Double check that the version numbers are exactly as they appeared in the previous version and the new version you are upgrading to.

3 ) You forgot to upgrade the version inside GreatModule/etc/config.xml

Hans-Eirik Hanifl

view all posts

Hans-Eirik Hanifl is a forward thinking e-commerce and marketing consultant. As an advocate for the free exchange of knowledge, he founded E-Commerce Gorilla as a place where like-minded individuals can ask questions and share their expertise on practical solutions in the area of e-commerce and marketing. He is the owner of TRM Marketing and an avid supporter of the open source community.

Stay in the loop!

E-Commerce Gorilla Avatar

Join the E-Commerce Gorilla e-mail newsletter. We provide monthly updates on new articles featuring all aspects of online sales and E-Commerce setup. We cover the do's and don'ts of effective online sales, along with great giveaways. As a newsletter subscriber you are automatically entered for any competitions and giveaways.* Join our community of engaged online marketing experts, great content, and great giveaways. You can't go wrong.