Add column Upgrade Schema Magento 2
namespace Test\TestAgain\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.0", "<")) {
//Your upgrade script
}
if (version_compare($context->getVersion(), '1.0.1', '<')) {
$installer->getConnection()->addColumn(
$installer->getTable('lime_eleveniacategory'),
'category_depth',
[
'type' => \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
'length' => 10,
'nullable' => true,
'comment' => 'Category Depth'
]
);
}
$installer->endSetup();
}
}
You can get more details here also, Upgrade Database table
One more thing to do here. Update module.xml
version. And upgrade setup, do reindexing and delete cache. It will work.
To Add multiples columns
if (version_compare($context->getVersion(), '0.1.1', '<')) {
$installer->getConnection()->addColumn(
$installer->getTable('reply_newsletter_subscriber'),
'field_1',
[
'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
'length' => 255,
'nullable' => true,
'comment' => 'Field_1'
]
);
$installer->getConnection()->addColumn(
$installer->getTable('reply_newsletter_subscriber'),
'field_2',
[
'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
'length' => 255,
'nullable' => true,
'comment' => 'Field_2'
]
);
}
$installer->endSetup();
}