1

I've InstallSchemathat created one table that's worked fine, but later on somehow scope changed and now need to change one specific column's datatype.

I tried with UpgradeSchema but doesn't seems to work. Any idea to implement this ? I know I can change via deleting existing table but I'm working on git setup so....

app/code/Vendor/Module/etc/module.xml:

<module name="Vendor_Module" setup_version="1.0.4" schema_version="1.0.4"/>

app/code/Vendor/Module/Setup/UpgradeSchema.php:

class UpgradeSchema implements UpgradeSchemaInterface
{

    /**
     * {@inheritdoc}
     */
    public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)
    {
        if (version_compare($context->getVersion(), '1.0.4') < 0) {
            $connection = $setup->getConnection();
            $connection->addColumn(
                $setup->getTable('one_table'),
                'serial_number',
                [
                    'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                    'length' => 255,
                    'nullable' => true,
                    'default' => '',
                    'comment' => 'Serial Number'
                ]
            );
        }
    }
}

app/code/Vendor/Module/Setup/InstallSchema.php:.

->addColumn(
                'serial_number',
                \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
                255,
                ['nullable => false'],
                'Serial Number'
            )

3 Answers 3

3

You can apply following code.

$setup->getConnection()->changeColumn(
    $setup->getTable('YOUR_TABLE_NAME'),
    'YOUR_OLD_COLUMN_NAME',
    'YOUR_NEW_COLUMN_NAME',
    [
        'type'     => \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
        'length'   => 255,
        'nullable' => false,
        'comment'  => 'Serial Number'
    ]
);
1

when ever version was changed just change in module.xml

UpgradeSchema.php

class UpgradeSchema implements UpgradeSchemaInterface
{

    /**
     * {@inheritdoc}
     */
    public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)
    {
        $setup->startSetup();
        if (version_compare($context->getVersion(), '1.0.4') < 0) {
            $connection = $setup->getConnection();
            $connection->changeColumn(
                $setup->getTable('one_table'),
                'serial_number',
                [
                    'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                    'length' => 255,
                    'nullable' => true,
                    'default' => '',
                    'comment' => 'Serial Number'
                ]
            );
        }

        $setup->endSetup();
    }
}
1

When you need to changes the datatype and length for Column then use this.. like i modify the length 10.0 to 10.2,

if(version_compare($context->getVersion(), '2.4.0', '<')) {

        $installer->getConnection()->modifyColumn(
            $installer->getTable( 'mconnect_csproduct' ),
           'csp_price',
            [
                'type' => \Magento\Framework\DB\Ddl\Table::TYPE_FLOAT,
                'nullable' => false,
                'unsigned'=> true,
                'primary'=> false,
                'length' => '10,2',
                'comment' => 'Csp Price'
            ]
            
        );
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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