Magento Module – Order of Execution for Setup Scripts

installmodulesetup-script

1) Can there be multiple install scripts in the custom module? For example:

  • data-install-1.0.0.php
  • data-install-1.0.1.php

2) Assuming I have the following scripts inside app/code/local/Company/Helloworld/data/helloworld_setup:

  • data-install-1.0.0.php
  • data-install-1.0.1.php
  • data-install-1.0.2.php
  • data-install-2.0.0.php
  • data-upgrade-1.0.0-1.0.1.php
  • data-upgrade-1.0.1-1.0.2.php
  • data-upgrade-1.0.2-2.0.0.php
  • data-upgrade-2.0.0-2.0.1.php

Questions:

2.1) I already have version 1.0.0 of this module installed in my Magento:

<config>
    <modules>
        <Company_Helloworld>
            <version>1.0.0</version>
        </Company_Helloworld>
    </modules>
...
</config>

What will be the order of execution of these scripts when:

2.1.a) I install version 1.0.2

2.1.b) I install version 2.0.0

2.2) I don't have any version of this module installed yet.
What will be the order of execution of these scripts when:

2.2.a) I install version 1.0.0

2.2.b) I install version 2.0.0

2.2.c) I install version 3.0.0

3) I've seen in Magento core modules, the sql install scripts are named in two ways:

  • mysql4-install-1.0.0.php
  • install-1.0.0.php

When is mysql4- required in the script name? When can I remove mysql4- from the name? Does it depend on Magento version?

Best Answer

  1. Yes you can have multiple install scripts
  2. (a) If you already have 1.0.0 installed and upgrade to 2.0.0 all the scripts that start with install will not be executed. So in the example you gave only data-upgrade-1.0.0-1.0.1.php, data-upgrade-1.0.1-1.0.2.php and data-upgrade-1.0.2-2.0.0.php will be executed.
    (b) the mysql4- prefix was required in versions prior to 1.6 (not including 1.6). From 1.6 and later the scripts should run even if they have or they don't have mysql4- prefix.

And an addition to 2a. If you don't have any version of the module installed and you install it directly with version 2.0.0 then only data-install-2.0.0.php will be executed. If you install it at version 3.0.0 again only data-install-2.0.0.php will be executed. So when installing a module at a certain version only the install script with a number lower (the closest) or equal to the version is executed.

[EDIT]

2.1.a You have 1.0.0 installed and get the 1.0.2 version. Executed scripts:

  • data-upgrade-1.0.0-1.0.1.php
  • data-upgrade-1.0.1-1.0.2.php

2.1.b You have 1.0.0 installed and get the 2.0.0 version. Executed scripts:

  • data-upgrade-1.0.0-1.0.1.php
  • data-upgrade-1.0.1-1.0.2.php
  • data-upgrade-1.0.2-2.0.0.php

2.2.a Nothing installed and get version 1.0.0. Executed scripts

  • data-install-1.0.0.php

2.2.b Nothing installed and get version 2.0.0. Executed scripts

  • data-install-2.0.0.php

2.2.c Nothing installed and get version 3.0.0. Executed scripts

  • data-install-2.0.0.php
  • data-upgrade-2.0.0-2.0.1.php

You can test this by creating a dummy extension where all the files contain only this:

Mage::log(__FILE__, null, 'install-scripts.log', true);

Then test your version combinations and look in var/log/install-scripts.log to see the order

Related Topic