Using Magento >= 1.6 data install scripts

Data

As of Magento version 1.6, you can split the database structure and the data insertion with the new data setup scripts. The data scripts will work about the same way as your sql install scripts and have the same versioning conventions like the normal ones you are used to.

I sure hope you switched to the resource scripts as the mysql4 ones are deprecated (you can still use them, but as the fellow Magento developers decided to put one more layer of abstraction so magento can work on other database systems than only mysql, the prefix ‘mysql4’ isn’t the proper naming anymore).

Just to mention, you can do the same things in your normal sql install/upgrade scripts, but if you have for example a upgrade of your module with only data insertion, or just want to split the data from the structure, this one will for sure suit your needs. Oh, one more thing: you can use them right away if you have already configured your module for setup resources, the only thing you need to do is to create the data/{modulename}_setup folder like you created the sql one.

The data scripts reside in the data/{modulename}_setup like the normal scripts reside in the sql/{modulename}_setup. The naming convention of the files has a slight difference than your sql scripts: they have a prefix named ‘data’. For example, you would name a sql install script ‘install-1.0.php’ whereas the data install script would be named ‘data-install-1.0.php’. The same naming convention applies to the data upgrade scripts, so they are named for example ‘data-upgrade-1.0.1-1.0.2.php’.

On every version of your magento module, you can have one sql upgrade script, or one data script, or both. If both, then the data upgrade script will be called right after your sql upgrade script. If you have only the data script, it will execute it, then increment the version like it would do it with the sql ones. (for example if you create a data-upgrade-1.0.1-1.0.2.php script magento will increment the module version to 1.0.2. If you would decide afterwards to create a sql script, the name would be upgrade-1.0.2-1.0.3.php to increment to version 1.0.3).

For the more visual ones, here’s a example of the data and sql folders in a module of mine. As you can see, the data-upgrade increments from version 1.0.0.2 to 1.0.0.3 without the sql upgrade script, and as I didn’t need the data scripts after the v. 1.0.0.3, I just continued with only sql scripts.


6 comments

  1. Thank you for sharing this info. Is it Creating a record in core_resource table in the database ?

  2. Too bad they added the data-install, but didn’t add an uninstall-x.x.x.x.php file (and downgrade-1.6.0.0-1.5.0.2 would be even better), but I guess we have to wait for Magento 2 for that.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <blockquote cite=""> <code> <del datetime=""> <em> <s> <strike> <strong>. You may use following syntax for source code: <pre><code>$current = "Inchoo";</code></pre>.