0

I have make

setup:upgrade with changing module version

here is my code:

<?php namespace Tatva\ProductImport\Setup;

  use Magento\Framework\Setup\UpgradeSchemaInterface;
  use Magento\Framework\Setup\ModuleContextInterface;
  use Magento\Framework\Setup\SchemaSetupInterface;

  class UpgradeSchema implements UpgradeSchemaInterface
      {

/**
 * {@inheritdoc}
 */
public function upgrade(
    SchemaSetupInterface $setup,
    ModuleContextInterface $context
) {
    $installer = $setup;

    $installer->startSetup();
    if (version_compare($context->getVersion(), '1.0.1', '<')) {
      $installer->getConnection()->addColumn(
            $installer->getTable('product_import_details'),
            'product_categories',
            [
                'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                'nullable' => true
            ]
        );
      $installer->getConnection()->addColumn(
            $installer->getTable('product_import_details'),
            'product_url',
            [
                'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                'nullable' => true
            ]
        );
        $installer->getConnection()->addColumn(
            $installer->getTable('product_import_details'),
            'product_download',
            [
                'type' => \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
                'length'=>11,
                'default'=>0,
                'nullable' => true
            ]
        )
      ;
    }
    $installer->endSetup();
 }
}
  • product_import_details is your custom table? Did you create it? – Khoa TruongDinh Dec 10 '18 at 6:37
  • yes i have create using install schema before – Rutvee Sojitra Dec 10 '18 at 6:40
  • Can you please upload your module.xml? – Rohan Hapani Dec 10 '18 at 8:53
1

Make sure your module version should be 1.0.1 :

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Tatva_ProductImport" setup_version="1.0.1" schema_version="1.0.1">      
    </module>
</config>

Replace this below code in your upgradeschema file. I changed if condition format :

<?php
namespace Tatva\ProductImport\Setup;

use Magento\Framework\Setup\UpgradeSchemaInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;

/**
 * @codeCoverageIgnore
 */
class UpgradeSchema implements UpgradeSchemaInterface
{
    /**
     * {@inheritdoc}
     */
    public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)
    {        
        $installer = $setup;
        $installer->startSetup();
        if (version_compare($context->getVersion(), '1.0.1') < 0) {
            $installer->getConnection()->addColumn(
              $installer->getTable('product_import_details'),
                    'product_categories',
                    [
                        'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                        'nullable' => true
                    ]
                );

            $installer->getConnection()->addColumn(
              $installer->getTable('product_import_details'),
                    'product_url',
                    [
                        'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                        'nullable' => true
                    ]
                );

            $installer->getConnection()->addColumn(
              $installer->getTable('product_import_details'),
                    'product_download',
                    [
                        'type' => \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
                        'length'=>11,
                        'default'=>0,
                        'nullable' => true
                    ]
                );
        }
        $installer->endSetup();
    }
}
  • Thanks Rohan. It's working!!!! Can you please tell why module version 1.0.1 require or give reference link for it? – Rutvee Sojitra Dec 10 '18 at 9:10
  • When upgradeschema execute at that time, it will check module version and in this file, we add code like that if module version is greater that 1.0.1, then new column will be add. So, in your code old module version was found. So, before execute upgrade schema we need to change module version in module.xml. – Rohan Hapani Dec 10 '18 at 9:13
  • Ohh... i missed that line to follow.. Btw thanks rohan!! – Rutvee Sojitra Dec 10 '18 at 9:16
  • My Pleasure :) Happy coding !! :) – Rohan Hapani Dec 10 '18 at 9:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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