0

Today I'm learning Upgrading schema and Upgrading data, for this I am created simple table with version0.0.1 and it's working fine. Later I written code for UpgradeSchema. But it is not working for me. For this stuff I have written below code.

UpgradeSchema.php

<?php

namespace Training\HelloWorld\Setup;

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

class UpgradeSchema implements \Magento\Framework\Setup\UpgradeSchemaInterface
{

    public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context){

        $installer = $setup;

        $installer->startSetup();
        if (version_compare($context->getVersion(), "0.0.1", "<")) {
            //Your upgrade script
        }
        if (version_compare($context->getVersion(), '0.0.1', '>')) {
            $installer->getConnection()->addColumn(
                $installer->getTable('helloworld'),
                'long_description',
                [
                    'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                    'length' => 255,
                    'nullable' => false,
                    'comment' => 'Long Description'
                ]
            );
        }
        $installer->endSetup();

    }

}

after this I run the php bin/magento setup:upgrade CLI Command. it's working fine and setup_module table also converted versions as below.

  1. Previously Training_HelloWorld 0.0.1 0.0.1
  2. After upgrade command Training_HelloWorld 0.1.1 0.1.1 converted successfully.

but My table is not added new column.

Could you please suggest me where I went wrong?

1

Replace below code to your version condition

if (version_compare($context->getVersion(), '0.0.1') < 0) {
        $installer->getConnection()->addColumn(
            $installer->getTable('helloworld'),
            'long_description',
            [
                'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                'length' => 255,
                'nullable' => false,
                'comment' => 'Long Description'
            ]
        );
    }

You need to add previous version value (0.0.1) to setup_module table.

Run setup:upgrade command and check.

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.