SchemaUpgrade issue in Magento 2
-
10-10-2020 - |
Question
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.
- Previously
Training_HelloWorld
0.0.1
0.0.1
- 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?
Solution
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.
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange