Question

I always thought it's some kind of epidemic plague that users behave in a high risk manner introducing $installer->startSetup() and $installer->stopSetup() calls in installer scripts.

Many of the example code given (here and on other sites, you also find it in many of the extensions you can download) contains installer scripts starting with:

<?php

$installer = $this;

$installer->startSetup();

# some or many lines of code that do not 
# need $installer->startSetup(); at all for sure
#
# ...
#

$installer->endSetup();

I always was under the impression that this is bad practice and specifically should not be used in example code because it introduces the risk to needlessly loose $installer->endSetup(); resulting in an inconsistent database configuration after setup which can endanger the whole installation.

So my question is, it is so? Is this a crystal clear case to give the advice to not make use of these methods unless you're 100% sure you need them?

I have the feeling that those are always put in because the author does not know what they are for but she thinks it needs to be there, it's cool to use them, it can't hurt and because it works anyway, continues with this risky practice.

I also thought to perhaps ask on meta because when I suggested edits earlier today, from what I see in feedback in review many users seem to be unsure on this point. I would love to see some clarification and reference about it.

Was it helpful?

Solution

You are 100% right. Using $installer->startSetup(); is very dangerous.
Specially when using it in scripts that remove attributes. Learned that the hard way, when my attribute was deleted but the values for the attributes were left as zombie records in the db.
I share your assumption that this is done because "that's how they do it in the core".
It's the same for this as for including

<all>
    <title>Allow everything</title>
</all> 

in the ACL section and adding a version to the layout files <layout version="0.1.0">.

but the startSetup can actually do some damage. the other 2 "traditions" I mentioned are harmless.

Just to make it clear for other people that end up here.
startSetup runs the following queries

SET SQL_MODE='';
SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0
SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO'

This means the FK checks are disabled, and you can insert the value 0 for columns that are PKs. (similar to how it's done to the core_store table in ).

endSetup just reverses the effects of startSetup.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top