My Magento Extension Install Script Will Not Run

46,787

Solution 1

Work your way through this article to make sure you don't have any misunderstanding of what the setup resources do, how they work, and how you can troubleshoot them.

Once you've done that, from everything you've said on this question thread it sounds like you're getting your resource "installed", but that your install script never runs. My guess is that the version number you used in

//0.0.1 is your version number
mysql4-install-0.0.1.php

didn't match up with the version of your module

<modules>
    <Nie_Nie>
        <version>?.?.?</version>
    </Nie_Nie>
</modules>

Those should match for the script to run. I think Magento is smart enough to run previous versions if it finds them, but the code in the setup resources is the kind that's hard to follow, so I always make sure they match.

Regardless, here's how you can see which file(s) magento is trying to run when it runs your setup resource. Delete any entries from core_resource related to your module. Clear your cache. Then find the following locations in the setup class

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

protected function _modifyResourceDb($actionType, $fromVersion, $toVersion)
{
    ... 

    $sqlFilesDir = Mage::getModuleDir('sql', $modName).DS.$this->_resourceName;        

    if (!is_dir($sqlFilesDir) || !is_readable($sqlFilesDir)) {
        return false;
    }

    ...

    $sqlDir->close();

    if (empty($arrAvailableFiles)) {
        return false;
    }

    ...

    $arrModifyFiles = $this->_getModifySqlFiles($actionType, $fromVersion, $toVersion, $arrAvailableFiles);
    if (empty($arrModifyFiles)) {
        return false;
    }

and then modify them to add some temporary debugging exceptions

    if (!is_dir($sqlFilesDir) || !is_readable($sqlFilesDir)) {
        throw new Exception("$sqlFilesDir not found");
        return false;
    }

    ...

    if (empty($arrAvailableFiles)) {
        throw new Exception("No files found to run");
        return false;
    }

    ...

    $arrModifyFiles = $this->_getModifySqlFiles($actionType, $fromVersion, $toVersion, $arrAvailableFiles);
    if (empty($arrModifyFiles)) {
        throw new Exception("No valid upgrade files found to run for ");
        return false;
    }

    throw new Exception("If you're getting here, we have a file.  Remove your exceptions here and place one in your installer to make sure it's the one you think it is.");

Reload the page and you'll get exception text complaining about whatever Magento can't find. That should be enough to help you track down which installer script Magento is trying to run, but failing to find. Just remember to delete your module's row in core_resource and to clear your cache. (Magento caches which modules need to check for an install/upgrade)

If that doesn't work, start digging into the logic of applyAllDataUpdates and figure out why the class isn't including your installer file.

Solution 2

When I encountered this problem I acually had to disable the cache. Merely flushing it did not help for whatever reason.

Solution 3

The easiest and most informative way to track down this error is to setup your IDE to debug Magento and set a breakpoint in your mysql4-install-0.0.1.php. If the breakpoint doesn't get hit, then you know if the issue is in your XML config. If the breakpoint does get hit, you can trace through the code to find the source of the error.

It may take you half a day to setup, but live debugging of Magento is by far the best way to learn and understand the code. Do yourself a favour, make the investment now.

Solution 4

As per Magento Knowledgebase you could try including a <class> tag in your <setup>. This way you can ensure the correct setup model is used and (if it gets that far) passes the model to your install script negating the need to create a $setup manually.

Check the file permissions of the install script and the directory it is in. I sometimes find deleting the record from core_resources helps kick start the process too.

Solution 5

You can check in Magento whcih modules are loaded and which version of that module is loaded:

  1. Go to app/code/core/Mage/Core/Model/Resource/Setup.php
  2. Go to function __construct()
  3. At the end of function write:

    Mage::log($modName); Mage::log($this->_moduleConfig);

It will log all the modules loaded with there version number. Here you can check if your module is loaded or not.

Share:
46,787
Josh Pennington
Author by

Josh Pennington

Just your average everyday programmer@joshpenningtongooglefacebook

Updated on September 17, 2020

Comments

  • Josh Pennington
    Josh Pennington over 3 years

    I am trying to create an install script for my extension and for some reason it will not the install script. The extension will show up in the core_resource table, but the attributes I am trying to create will not create.

    I am pretty sure that the script is not even being called because I put an exit() at the beginning and the site ran just fine.

    Here is what I have in my config XML file. This is placed inside global -> resources path:

    <nie_setup>
        <setup>
            <module>Nie_Nie</module>
        </setup>
        <connection>
            <use>core_setup</use>
        </connection>
    </nie_setup>
    

    My install script is as follows:

    $installer = $this;
    $setup = new Mage_Eav_Model_Entity_Setup('core_setup');
    $installer->startSetup();
    
    $setup->addAttribute('customer', 'nie_admin', array(
        'input'                 => 'text',
        'type'                  => 'text',
        'backend'               => '',
        'visible'               => 0,
        'required'          => 0,
        'user_defined'  => 1,
    ));
    
    $installer->endSetup();
    

    Is there something obvious I am missing here that would be the reason the script will not run?

  • Josh Pennington
    Josh Pennington over 13 years
    I tried changing the version number and that did not run the script. It just changed the version number in the core_resources table.
  • Eugene Tulika
    Eugene Tulika over 13 years
    Some more obvious questions: is your upgrade script inside sql/nie_setup dir? is your module dir is app/code/local/Nie/Nie?
  • Josh Pennington
    Josh Pennington over 13 years
    Your debugging methods told me that I had my script in the wrong directory. Thank you so much! I get to continue being employed for one more day!
  • kalenjordan
    kalenjordan over 11 years
    Great answer, Alan. Don't you think that there should be like a strict mode where you can tell Magento to complain at you if you attempt to set something up and it isn't setup right? Upgrade scripts, routes, themes, there are all sorts of things that are tough to setup your first time around and it would be great to be able to get Magento to just complain at you about specifically what the problem is. I think a module that did this would be awesome.
  • B00MER
    B00MER over 11 years
    I would agree with sparcksoft about Magento failing such silently, even an entry to a log file would be worth the effort. I still don't like how Magento will blindly install anything that matches without some sort of checksum. Also just a tip most don't realize you can use .sql instead of .php for your install files if your just doing raw SQL on updates/installs.
  • Dilhan Maduranga
    Dilhan Maduranga over 11 years
    Don't forget to clear the Magento cache.. Even if we know we should, it is usual to forget it!
  • Alan Storm
    Alan Storm almost 9 years
    @PratikCJoshi This article has information on how to trace the full path in more modern versions of Magento. vinaikopp.com/2014/11/03/magento-setup-scripts
  • Luke A. Leber
    Luke A. Leber over 8 years
    Should be noted that live debugging is not possible if the code-base is using the rubbish IonCube zend module (that many poorly designed third party modules require).
  • tread
    tread over 7 years
    spot on mateyyy
  • Black
    Black over 5 years
    How can I even run the install script? By executing it in the browser??! I tried it with magerun, but im not sure
  • Alan Storm
    Alan Storm over 5 years
    @Black In Magento 1 the scripts are meant to run automatically on the first Magento/PHP execution after you clear your cache. Tools like magerun let you run scripts manually. It sounds like you might be in a position where you need to ask a new question and not comment on an old one.