21

I realize, you can use setup scripts for your own module by declaring them in the etc/config.xml file of your module within the <global> space like this:

<resources>
    <catalog_setup>
        <setup>
            <module>Mage_Catalog</module>
            <class>Mage_Catalog_Model_Resource_Setup</class>
        </setup>
    </catalog_setup>
</resources>

Some modules then use the file mysql4-install-0.0.1.php whereas others use install-0.0.1.php. So when do I use the mysql4 in front of the file name and when do I not use it?

Also, what can I do when my setup script is not running to find out why it is not?

  • 1
    Make sure you delete your entry in the core_resource table to have your setup run again. – Rick Kuipers Jan 25 '13 at 7:49
  • In addition to the other answers it could be interesing to read magentocommerce.com/images/uploads/RDBMS_Guide2.pdf It is from the Core Team and describes the thoughts behind the changes made with 1.6 I think it's always good not only to know what has changed but also why. – Celldweller Jan 25 '13 at 11:55
20

From Magento 1.6 and upwards you don't need to use mysql4 anymore because of added multi-RDBMS support.

To find out why it's not running, you can check the exception.log or system.log, maybe Magento coudn't find your setup class. Creating an install script is pretty straightforward though.

Also check Alan Storm's guide: http://alanstorm.com/magento_setup_resources

10

As @erfan saif, magento got multi-rdbms support since 1.6. But in the real world, I know only mysql backends.

It is important to understand, that magento can have different install/upgrade/data scripts for different backends. If you want a special Index-Type which is supported by mysql but not by standard-SQL you can implement a mysql4-install-1.0.0.php script. If your script is generic, use install-1.0.0.php

If you take a look into Mage_Core_Model_Resource_Setup I can find two interessting things:

  1. You can name your files (%s-)%s-VERSION.(php|sql)
  2. If you have two install scripts (but with data scripts, it is the same (app/code/core/Mage/Core/Model/Resource/Setup.php:520)) magento prefers the specialised script over the generic script (as one would expect)

app/code/core/Mage/Core/Model/Resource/Setup.php:488

$regExpDb   = sprintf('#^%s-(.*)\.(php|sql)$#i', $actionType);
$regExpType = sprintf('#^%s-%s-(.*)\.(php|sql)$#i', $resModel, $actionType);

while (false !== ($file = $handlerDir->read())) {
    $matches = array();
    if (preg_match($regExpDb, $file, $matches)) {
        $dbFiles[$matches[1]] = $filesDir . DS . $file;
    } else if (preg_match($regExpType, $file, $matches)) {
        $typeFiles[$matches[1]] = $filesDir . DS . $file;
    }
}
[...]
foreach ($typeFiles as $version => $file) {
    $dbFiles[$version] = $file;
}

Be careful, if you name your script .sql it is invoked directly into the database:

// app/code/core/Mage/Core/Model/Resource/Setup.php:621
switch ($fileType) {
    case 'php':
        $conn   = $this->getConnection();
        $result = include $fileName;
        break;
    case 'sql':
        $sql = file_get_contents($fileName);
        if (!empty($sql)) {

            $result = $this->run($sql);

Also, what can I do when my setup script is not running to find out why it is not?

I prefer a die('sadf') at the beginning of my install/upgrade file, because I can run it multiple times if it is called, so I can check, wether all the variables I set are correct, before anything is changed in the database. If I see the 'sadf' on the screen, I know, the script is running.

I magento loads (instead of the sadf), it is time to debug, my two standard errors are:

  1. I have forgotton to add the script to the config
  2. I have the directory in sql/ forgotten, e.g. sql/install-1.0.0.php instead of sql/my_module_setup/install-1.0.0.php

And because I think it fits here, be careful with your variable names: http://blog.fabian-blechschmidt.de/articles/file-kills-setup-script.html

UPDATE @rouven-rieker added via twitter that data- and missing mysql4- was added in magento 1.6. If you need backwards compatibility be careful!

3

If your script isn't running you should also check if your modules version matches the version of your setup-script.

<modules>
    <Your_Module>
        <version>0.0.1</version>
    </Your_Module>
</modules> 
1

If your script is not running, reset version number in core_resource and flush cache. Magento caches versions in advance it seems and misses to run setup scripts when all caches are enabled. Because we all work with cache disabled, we may not notice this, but installing on Live site via upload will not work if cache is enabled.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.