Categories


Archives


Recent Posts


Categories


Magento Structure Updates vs. Data Updates

astorm

Frustrated by Magento? Then you’ll love Commerce Bug, the must have debugging extension for anyone using Magento. Whether you’re just starting out or you’re a seasoned pro, Commerce Bug will save you and your team hours everyday. Grab a copy and start working with Magento instead of against it.

Updated for Magento 2! No Frills Magento Layout is the only Magento front end book you'll ever need. Get your copy today!

Around CE 1.4, Magento extended their Setup Resource Migration system with the ability to have data updates as well as structure updates. That is, a setup resource upgrade/install script named like this

mysql4-install-0.7.0.php
mysql4-data-upgrade-0.7.63-0.7.64.php

should contain structural changes to Magento’s database tables, whereas updates named like this (with data in the name)

mysql4-data-upgrade-0.7.57-0.7.58.php

should contain changes to a table’s data rows. Modern versions of Magento require data updates to be named without the mysql4 resource type.

    data-upgrade-0.7.57-0.7.58.php

The concept, however, is the same.

What many developers don’t realize is this is more than just an organizational “nice to have”. Structure changes and data changes are applied at different points in the Magento bootstrap process, and this has implications on how they run.

Specifically, Magento applies structure changes with a call to

Mage_Core_Model_Resource_Setup::applyAllUpdates();

During normal system operations, this call happens before any event area has been loaded in Magento. This means if a setup resource script dispatches an event (either directly, or by doing something like calling save or load on a model), Magento will not call any configured observers.

Magento applies data changes with a call to

Mage_Core_Model_Resource_Setup::applyAllDataUpdates()

Before Magento calls applyAllDataUpdates, the global event area is loaded. This means during data upgrades Magento will call any observers configured under the global node, not not call any observers configured under the frontend or adminhtml nodes. This means you can, in theory, use Magento models in your data upgrade scripts with the assurance that important pre/post/load/save observers will be called.

It’s also worth pointing out the above is what I’ve observed in more modern versions of Magento (1.7, 1.8). Changes and bug fixes to the event and setup resource systems, as well as the general non-obviousness of these lower level systems, means the behavior may be different in older versions of Magento

Copyright © Alan Storm 1975 – 2019 All Rights Reserved

Originally Posted: 13th December 2013