1

I would appreciate help on this, I'm having trouble bringing these two together.

I am basically familiar with the mysql upgrade process in magento.

  1. In a folder's etc/config.xml file you update the version

    <Mycompany_Myextension>
        <version>0.1.4</version><!-- update from 0.1.3 -->
    ..
    
  2. Write a file called `mysql4-upgrade-0.1.3-0.1.4.php

  3. in that file have something like this:

$installer = $this;

$installer->run("Some SQL query here");

I want to run that outside of the Magento App, however...

So I have created a bootstrap file in /shell, but how do I instantiate $installer? My bootstrap.php file is outside the same instantiation path as the mysql upgrade file.

Just for full docs, here is the shell file code (I have noted "throws fatal error" where the problem is):

<?php
/*
 * COMPANY-9047
 * @author: me
 * @date: 2016-04-29
 * Used to run a MySQL upgrade script outside of Magento
 */

require_once 'abstract.php';

class Company_Shell_Genericscript extends Mage_Shell_Abstract
{
    protected $_argname = array();

    public function __construct() {
        parent::__construct();

        // Time limit to infinity
        set_time_limit(0);

        // Get command line argument named "argname"
        // Accepts multiple values (comma separated)
        if($this->getArg('argname')) {
            $this->_argname = array_merge(
                $this->_argname,
                array_map(
                    'trim',
                    explode(',', $this->getArg('argname'))
                )
            );
        }
    }

    protected function _getConnection($type = 'core_read'){

        if ($this->blnUseMyEnvironment != false){
            return Mage::getSingleton('core/resource')->getConnection($type);
        }

        if (!$objConnection = Mage::getConfig()->getNode('global/resources/replica/connection') ) {
            Mage::throwException('Replica connection info is missing.');
        }

        try {
            $arrConnection = $objConnection->asArray();

            $objReplicaDb = new Zend_Db_Adapter_Pdo_Mysql(
                array(
                    'host' => $arrConnection['host'],
                    'username' => $arrConnection['username'],
                    'password' => $arrConnection['password'],
                    'dbname' => $arrConnection['dbname']
                )
            );

            return $objReplicaDb;

        }catch(Exception $e){
            echo $e->getMessage().", in ".__FILE__." on line ".$e->getLine()."\n\n";
        }

    }

    // Shell script point of entry
    public function run() {
        $db = Mage::getSingleton('core/resource')->getConnection('core_write');

        $this->startSetup(); //throws fatal error
        exit('end');

    }

    // Usage instructions
    public function usageHelp()
    {
        return <<<USAGE
Usage:  php -f scriptname.php -- [options]

  --argname <argvalue>       Argument description

  help                   This help

USAGE;
    }
}
// Instantiate
$shell = new Company_Shell_Genericscript();

// Initiate script
$shell->run();

1 Answer 1

2

An install script within Magento will be ran as soon as somebody visits the webshop. So moving the installer script "call" to a separate shell script doesn't work, because it might run the installer twice or will not run your actual script at all.

If I were you I'd go for the default approach where you put the webshop in maintenance mode before you put the new version of the extension live. Then you can use n98-magerun from the command line (if available) and run n98-magerun sys:setup:run to run all upgrade scripts.

When that is done, you can do a reindex if that's required by your extension and then you can remove the maintenance mode and you're done.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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