Current Store is 1 When Running Upgrade Scripts

install-scriptstore-view

Any idea why Mage::app()->getStore() returns the store view with id 1 when inside the upgrade scripts independent on the store view I'm running the upgrade script in (even admin)?
I mean, I know where the code that does this is. In Mage_Core_Model_App::getStore() there is this:

    if (!Mage::isInstalled() || $this->getUpdateMode()) {
        return $this->_getDefaultStore();
    }

and _getDefaultStore looks like this:

   if (empty($this->_store)) {
        $this->_store = Mage::getModel('core/store')
            ->setId(self::DISTRO_STORE_ID)
            ->setCode(self::DISTRO_STORE_CODE);
    }
    return $this->_store;

$this->_store is always empty when reaching the method above.

I get the same result even if I add this at the top of the upgrade script:

Mage::app()->setCurrentStore(Mage::getModel('core/store')->load(Mage_Core_Model_App::ADMIN_STORE_ID));

I'm curious about the business logic of having this 'feature'.

Best Answer

NB: don't forget that the admin store scope isn't set until dispatching is taking place and a controller extending Mage_Adminhtml_Controller_Action executes (see the adminhtml_controller_action_predispatch_start event and related observer in Mage_Adminhtml_Controller_Action::preDispatch()).

I'm curious about the business logic of having this 'feature'.

You aren't the only one; that said, we may never know unless Moshe or Dima wants to discuss.

Setup scripts execute early in application initialization. The design of this is probably due so that, by the time the rest of the stack is executed, the necessary migrations and other work would be "done" - meaning that the system was ready to use right away even when a module was being installed or upgraded. I'm wondering if the original architects initially ever thought there would be a need for a more initialized system. I'll conjecture that, whereas much of the code assumes that there is a store instance available, the _getDefaultStore() logic ensures that there is a store instance.

Full scope settings are available in 1.4.0.0 and up via data setup scripts.