Magento – Magento 2 module “data_version – not upgrading”

magento2setup-script

In Magento 2.0 I have written UpgradeData and UpgradeSchema and I have changed the setup_version in module.xml. After that I run php bin/magento setup:upgrade After this in the database, schema_version is upgrading but data_version not upgrading. Any Ideas?

UpgradeData.php

namespace Vendor\Pawan\Setup;
use Magento\Framework\Setup\UpgradeDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;

/**
 * Upgrade data for SImple Google Shopping
 */
class UpgradeData implements UpgradeDataInterface
{

    /**
     * @param ModuleDataSetupInterface $setup
     * @param ModuleContextInterface   $context
     */
    public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {

        // $context->getVersion() = version du module actuelle
        // 10.0.0 = version en cours d'installation

        if (version_compare($context->getVersion(), '10.0.0') < 0) {
            $installer = $setup;
            $installer->startSetup();
            // do what you have to do

            $installer->endSetup();
        }
    }
}

Module.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
    <module name="Vendor_Pawan" setup_version="2.1.1">

    </module>
</config>

Best Answer

$setup->startSetup() and $setup->endSetup() should be outside if clause.

namespace Vendor\Pawan\Setup;
use Magento\Framework\Setup\UpgradeDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;

/**
 * Upgrade data for SImple Google Shopping
 */
class UpgradeData implements UpgradeDataInterface
{

    /**
     * @param ModuleDataSetupInterface $setup
     * @param ModuleContextInterface   $context
     */
    public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {

        $setup->startSetup();
        // $context->getVersion() = version du module actuelle
        // 10.0.0 = version en cours d'installation

        if (version_compare($context->getVersion(), '10.0.0') < 0) {
            // do what you have to do

        }
        $setup->endSetup();
    }
}
Related Topic