1

I'm not able to add new column to existing table UpgradeSchema.php:

 namespace Company\Module\Setup;

use Magento\Framework\Setup\UpgradeSchemaInterface;

use Magento\Framework\Setup\ModuleContextInterface;

use Magento\Framework\Setup\SchemaSetupInterface;

use Magento\Framework\DB\Ddl\Table;

class UpgradeSchema implements UpgradeSchemaInterface
{

    public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)
    {
        $setup->startSetup();

        //handle all possible upgrade versions
        $tableName = $setup->getTable('test_table');
        if(!$context->getVersion()) {
            //no previous version found
        }

        if (version_compare($context->getVersion(), '1.0.5') < 0) {
            $connection = $setup->getConnection();
            $connection->addColumn(
                $tableName,
                'test_test_1',
                ['type' => Table::TYPE_TEXT,
                    'nullable' => false,
                    'default' => '',
                    'afters' => 'created_at'],
                'Test 1'
            );
        }

        if (version_compare($context->getVersion(), '1.0.6') < 0) {
            $connection = $setup->getConnection();
            $connection->addColumn(
                $tableName,
                'test_test_2',
                ['type' => Table::TYPE_TEXT,
                    'nullable' => false,
                    'default' => '',
                    'afters' => 'created_at'],
                'Test 2'
            );
        }

        $setup->endSetup();
    }
}

My module.xml:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
<module name="Company_Module" setup_version="1.1.5">

</module>

Can anyone give some advice please! Thanks.

  • @Manthan Dave any idea? – Juliano Vargas Feb 20 '18 at 10:09
  • @Manthan Dave Yes I do have setup_version set to '1.1.5' and the table "setu_module" does changes when I run the setup:upgrade but the column doesn't get added! – Juliano Vargas Feb 20 '18 at 10:19
0

I found the issue and it was due to UpgradeData.php don't no why but I have rewritten to InstallData.php and now it worked as I expected, So no deletion entry from setup_module is necessary if you do unless a getVersion() check is done it will duplicate the data.

My setup_module entry has:

schema_version:1.1.5 
data_version:1.1.5

So I changed setup_version My module.xml to:

setup_version="1.1.6"

And My UpgradeSchema version_compare to:

 if (version_compare($context->getVersion(), '1.1.6') < 0) {...}

Now the new column is added.

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.