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!

Today we’re going to talk about pestle’s new magento2:generate:schema-upgrade command. To do that, we need to talk a little bit about Magento 1, as well as “migrations” in information systems more generally.

Information Systems

An information system (IS) is any organized system for the collection, organization, storage and communication of information. More specifically, it is the study of complementary networks that people and organizations use to collect, filter, process, create and distribute data.

All of these systems have some form of data-persistence. Most of them primarily use an ANSI-SQL database to store structured data (MySQL, PostgreSQL, etc.). Newer systems might incorporate a modern key/value object store (Redis, MongoDB)

Regardless of how these information systems store their data, these information systems are software. This means the creators/maintainers of these information systems will inevitably need to update their software. All successful software needs to fix bugs and improve features if it wants to remain successful.

When it comes to data persistence, this presents a few challenges:

How do we make updates to our user’s database schemas?

How do we ensure the default data in the data-persistence layer is present?

How do we do this in a way that creates a record of how/when these changes happened?

How do we tie these changes to a particular version of our software?

How do we do this in a way that either succeeds, or clearly falls back to the previous schema/data version?

Broadly speaking, in the realm of “MVC software systems used to build information systems software”, the world has settled on using schema/data migrations to achieve this. Migrations

Contain instructions for upgrading, and sometimes downgrading, a database schema

Contain instructions for adding, removing, or changing, the default data needed to make a system run (i.e. NOT the data the system is managing)

Are versioned

Can be run in such a way that the default schema/data at any version of an information system’s life can be recreated

While the concept surely predates this, the first time I encountered schema and data migrations in my career was via Ruby on Rails. The first PHP system I worked with that had a formalized migration system was Magento 1.

Magento 1 Setup Resource Scripts

Magento 1’s migration system was usually referred to as the Setup Resource system — named for the base PHP class that controlled the system.

When properly configured, each Magento module could have a data and sql folder. Each folder could contain scripts named via Magento module version numbers

The intricacies of these naming conventions are beyond the scope of this article, but encoded in the file names are instructions for when (based on module version) these scripts should run. Inside each script are instructions for updating the database.

Via this mechanism, Magento was able to alter the SQL database such that it matched what the module developers needed it to be.

Problems with MySQL, Problems with Magento

Magento 1’s migration system, while not perfect, did an OK job over the years. Both the core team, and Magento’s ecosystem of module developers were able to use it to distribute schema and data changes to their users. However, there were a few places it fell down. Sometimes this was due to design issues. Other times this was due to the limitations of Magento’s primary database — MySQL.

While PHP, and Magento 1, are capable of talking to most relational database management systems, most information systems built in PHP are biased towards MySQL. With regards to migrations, MySQL throws a sticky wicket when presented with the question:

How do we do this in a way that either succeeds, or clearly falls back to the previous schema/data version?

A good migration system will wrap their migration code in a single database transaction. If a problem occurs while the script is running, the transaction rolls the database back to the previous version.

these include data definition language (DDL) statements, such as those that create or drop databases, those that create, drop, or alter tables or stored routines.

In other words, one of the important things a migration system needs to do is not possible with MySQL databases. If you wanted transaction support you’d need to implement it yourself in PHP code — a non-trivial task at best.

Magento 1’s Setup Resource scripts do not have transaction support. — The why of this is probably lost to history, but it’s safe to say that MySQL’s lack of support for DDL transactions (and the relative inexperience of the still-pretty-smart original Magento 1 core engineers) played a role.

While not great, this is a problem anyone (Laravel, Rails, etc.) targeting MySQL needs to live with. What made things extra frustrating for Magento 1 developers was the fact that

Setup Resource scripts ran automatically in the background of an HTTP page load

Magento Connect, the official package manager for Magento 1, was incredibly buggy and routinely left systems half updated (while reporting no errors)

There was no way to manually trigger the setup resource scripts

While more experienced developers would know to use something like the n98-magerun sys:setup:incremental command to run their scripts incrementally, the average Magento user was left at the mercy of an opaque, buggy, system. The problems of half-complete updates got so bad that Magento needed to introduce a database repair tool that tries to “patch up” databases after a bad migration run.

All this has left most experienced Magento developers curious as to how Magento 2 would tackle these problems.

Magento 2 Setup Install/Upgrade Classes

Whether you call it a replacement, or a refactoring so thorough the system is unrecognizable, the old Magento 1 setup resource system is no longer present in Magento 2. In its place, Magento gives each module a set of Install and Upgrade classes. When a user runs the

php bin/magento setup:upgrade

command, Magento will look for these specifically named classes in each module. If this is the first time Magento sees a module, Magento will instantiate objects from the Packagename\Modulename\Setup\InstallSchema and Packagename\Modulename\Setup\InstallData classes, and call their install methods.

If Magento notices the module’s version number has changed, Magento will instantiate objects from the Packagename\Modulename\Setup\UpgradeSchema and Packagename\Modulename\Setup\UpgradeData classes and call their upgrade methods.

The first thing you’ll notice is Magento 2, similar to Magento 1, still separates out module installation from module upgrades. However, Magento 2 does not have any built-in support for module versioning. Regardless of which version of the module you’re upgrading to, Magento will always instantiate objects from the Packagename\Modulename\Setup\UpgradeSchema and Packagename\Modulename\Setup\UpgradeData class.

The result? Each module programmer needs to do version sniffing themselves. You can see this in the Magento core in various places.

This is — problematic? — for a number of reasons. While it works, as Magento 2 continues to release new versions, these upgrade classes will quickly grown unwieldy in size. Also, there’s a not-insignificant chance a programmer will inadvertently change an older version_compareif block while editing the file for the latest version. Also — while useful, PHP’s version_compare function can be a little ambiguous as to how it works, forcing the module developer to think about something that should be automatic. Finally, even if you copy Magento’s pattern, it’s possible to introduce a code branch that indicates a newer version but will still run if the code’s run against an older version. Consider this code

If the user is upgrading the module to version 2.1.1, but for some reason the 2.2.0 branch is present in code, the branch will still run. Again, developers are forced to think about something the previous system handled automatically.

I’m hesitant to speculate as to why Magento 2 created a seemingly inferior migration system for Magento 2. However, lacking other evidence, this sure looks like a feature implemented by a team that wasn’t taking the long view on their platform, and by a developer who had little experience writing these sorts of systems and lacked a supportive and candid peer review. It’s particularly incongruous when you consider it’s led to giant if/then blocks from a team that so heavily trumpeted the need for massively abstract class-based-oop systems.

Using Pestle to Create Upgrade Classes

Regardless of what we may think of it, this is the system we have in Magento 2. This brings us to the magento2:generate:schema-upgrade command. This command will

Populate a module with an UpdateSchema and UpgradeData class

Provide an optional default implementation for both classes that uses versioned scripts.

To use this command, just run the following (replacing Pulsestorm_Helloworld with your own module name)

and pestle will increment the version in your module.xml file to match the version you specified. The class names and paths above are based on our passing the command Pulsestorm_Helloworld — your classes will be named based on the module name you pass to the command.

This default implementation calls the generated Pulsestorm\Helloworld\Setup\Scripts object’s run method. The Pulsestorm\Helloworld\Setup\Scripts object implements a simple, traditional setup resource script system. With the above in place, when Magento first sees version 0.0.2 of this module, the Pulsestorm\Helloworld\Setup\Scripts class will include in the 0.0.2.php include files in the upgrade_scripts/data and upgrade_scripts/schema folders — as well as any other versioned files that exist between the old module version and the new, current module version.

It’s not necessary to use the Pulsestorm\Helloworld\Setup\Scripts helper (and future versions of pestle will give you options to omit it), but for developers who plan on releasing multiple versions of their modules, separating each upgrade out into its own include file seems like a saner approach than what Magento’s doing in their core UpgradeSchema classes.

As always, if you run into problems using this command, or have ideas on how it could be better, we’re active and responsive to all GitHub issues.